问题描述
在C#
和Python
到Memcached
之间存在一个奇怪的问题。我在服务器上安装了Memcached。 C#应用程序将使用Enyim.Caching
库在Memcached上写入一些数据,然后Python应用程序将使用python-memcached
读取数据(我也尝试过pymemcache
和pylibmc
)。我也想颠倒这个过程,并从Python中读取C#。
我的发现是:
- C#和Python都可以在服务器上编写和读取它们自己编写的变量。
- 当C#编写
string
时,Python可以很好地检索它。 - 其他配置失败并引发以下错误:
- C#
int
对Python的影响是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"
- C#
byte
对Python的影响是ValueError: invalid literal for int() with base 10: b' '
- Python
string
到C#引发System.ArgumentException: 'Destination array is not long enough to copy all the items in the collection. Check array index and length.'
- Python
int
到C#引发An unhandled exception of type 'System.NullReferenceException' occurred in testDatabaseC.dll Object reference not set to an instance of an object.
- Python
byte
到C#引发system.invalidCastException: 'Specified cast is not valid.'
- C#
一个字符串可以从C#到Python进行某种转换的事实让我认为我的代码没有错。此列表中的第一个例外可能表明问题出在所使用的压缩格式中。知道这个Python库使用zlib
,但是我还没有找到任何可行的替代方法。
有什么想法吗? 谢谢!
以下使用了某些代码:
要使用Python编写并使用C#进行读取(在这种情况下为int
,但也可以是byte
或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,10);
mcc.socketPool.ConnectionTimeout = new TimeSpan(0,10);
mcc.socketPool.DeadTimeout = new TimeSpan(0,20);
client = new MemcachedClient(mcc);
int dataToRead;
dataToRead = (int)client.Get("key");
return dataToRead;
}
}
}
要使用C#编写并使用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,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"))
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)