使用Memcached将数据从Python传递到C#

问题描述

C#PythonMemcached之间存在一个奇怪的问题。我在服务器上安装了Memcached。 C#应用程序将使用Enyim.Caching库在Memcached上写入一些数据,然后Python应用程序将使用python-memcached读取数据(我也尝试过pymemcachepylibmc)。我也想颠倒这个过程,并从Python中读取C#。

我的发现是:

  1. C#和Python都可以在服务器上编写和读取它们自己编写的变量。
  2. 当C#编写string时,Python可以很好地检索它。
  3. 其他配置失败并引发以下错误
    • 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#到Python进行某种转换的事实让我认为我的代码没有错。此列表中的第一个例外可能表明问题出在所使用的压缩格式中。知道这个Python库使用zlib,但是我还没有找到任何可行的替代方法

有什么想法吗? 谢谢!

以下使用了某些代码

要使用Python编写并使用C#进行读取(在这种情况下为int,但也可以是bytestring): >

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 (将#修改为@)