Reputation: 1
I have a weird problem interfacing C#
and Python
through Memcached
. I have Memcached installed on a server. A C# application would write some data on Memcached using the Enyim.Caching
library, then a Python application would read it using python-memcached
(I also tried pymemcache
and pylibmc
). I also would like to reverse the process and having C# reading from Python.
My findings are:
string
, Python can retrieve it just fine.int
to Python raises File "C:\Users\...\Anaconda2\envs\py37\lib\site-packages\memcache.py", line 1257, in _recv_value buf = self.decompressor(buf) error: Error -3 while decompressing data: incorrect header check"
byte
to Python raises ValueError: invalid literal for int() with base 10: b' '
string
to C# raises System.ArgumentException: 'Destination array is not long enough to copy all the items in the collection. Check array index and length.'
int
to C# raises An unhandled exception of type 'System.NullReferenceException' occurred in testDatabaseC.dll Object reference not set to an instance of an object.
byte
to C# raises System.InvalidCastException: 'Specified cast is not valid.'
The fact that a string can go one way from C# to Python let me think that there is nothing wrong with my code. The first exception in this list might suggests that the problem is in the compression format used. In know that this Python library uses zlib
, but I haven't found any working alternatives.
Any ideas? Thanks!
Some code used bellow:
To write with Python and read with C# (in that case an int
, but could also be a byte
or a string
):
import memcache
memcachedServerIP = "192.168.1.101"
client = memcache.Client([(memcachedServerIP, 11211)])
dataToWrite = 5 # just a random integer to try
print(client.set("key", dataToWrite, time=10))
using System;
using MySql.Data.MySqlClient;
using Enyim.Caching;
using Enyim.Caching.Configuration;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;
namespace testDatabase
{
public class Test
{
public static int readFromCache()
{
MemcachedClientConfiguration mcc = new MemcachedClientConfiguration();
MemcachedClient client;
mcc.AddServer("192.168.1.101:11211");
mcc.SocketPool.ReceiveTimeout = new TimeSpan(0, 0, 10);
mcc.SocketPool.ConnectionTimeout = new TimeSpan(0, 0, 10);
mcc.SocketPool.DeadTimeout = new TimeSpan(0, 0, 20);
client = new MemcachedClient(mcc);
int dataToRead;
dataToRead = (int)client.Get("key");
return dataToRead;
}
}
}
To write with C# and read with Python:
using System;
using MySql.Data.MySqlClient;
using Enyim.Caching;
using Enyim.Caching.Configuration;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;
namespace testDatabase
{
public class Test
{
public static int writeToCache()
{
MemcachedClientConfiguration mcc = new MemcachedClientConfiguration();
MemcachedClient client;
mcc.AddServer("192.168.1.101:11211");
mcc.SocketPool.ReceiveTimeout = new TimeSpan(0, 0, 10);
mcc.SocketPool.ConnectionTimeout = new TimeSpan(0, 0, 10);
mcc.SocketPool.DeadTimeout = new TimeSpan(0, 0, 20);
client = new MemcachedClient(mcc);
int dataToWrite = 5;
client.Store(Enyim.Caching.Memcached.StoreMode.Set, "key", dataToWrite);
return dataToWrite;
}
}
}
import memcache
memcachedServerIP = "192.168.1.101"
client = memcache.Client([(memcachedServerIP, 11211)])
print(client.get("key"))
Upvotes: 0
Views: 191