我试图使用Enyim客户端库从Memcache服务器获取密钥值。
连接到memcache服务器后,我可以编写/替换键值,但在使用get操作时,引发异常:
NotSupportedException是未处理的,服务器不支持操作,或者请求格式错误。如果是后者,请向开发人员报告错误。
请注意,我可以使用telnet/其他memcache库读取此值。
我不确定我是不是漏掉了什么东西,还是在Enyim包里有错误?
这是密码。
using System;
using System.Net;
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
namespace Memcached
{
public class MemcacheClient
{
private MemcachedClient _mc;
private static readonly TimeSpan MinExpirationTimeSpan = new TimeSpan(0, 0, 30);
public MemcacheClient()
{
Startup();
}
private void Startup()
{
var config = new MemcachedClientConfiguration();
config.Servers.Add(new IPEndPoint(IPAddress.Loopback, 11212));
// config.Servers.Add(new IPEndPoint(IPAddress.Loopback, 11213));
config.Protocol = MemcachedProtocol.Text;
_mc = new MemcachedClient(config);
}
public bool SetItem(string key, object value)
{
if (_mc != null)
return _mc.Store(StoreMode.Set, key, value);
return false;
}
public bool SetItem(string key, object value, TimeSpan duration)
{
_ValidateExpirationDuration(duration);
if (_mc != null)
{
var restult = _mc.Store(StoreMode.Set, key, value, DateTime.Now + duration);
_mc.FlushAll();
return restult;
}
return false;
}
private static void _ValidateExpirationDuration(TimeSpan duration)
{
if (duration <= MinExpirationTimeSpan)
throw new ArgumentException("Cache expiration times of less than 30 seconds are ignored", "duration");
}
public object GetItem(string key)
{
if (_mc != null)
return _mc.Get(key); //<----------getting exception here
return null;
}
public T GetItem<T>(string key) where T : class
{
var result = GetItem(key);
if (result != null)
{
var targetObject = result as T;
if (targetObject != null)
return targetObject;
}
return null;
}
public bool Replace(string key, object value, TimeSpan duration)
{
_ValidateExpirationDuration(duration);
if (_mc != null)
return _mc.Store(StoreMode.Replace, key, value, DateTime.Now + duration);
return false;
}
public bool Replace(string key, object value)
{
if (_mc != null)
return _mc.Store(StoreMode.Replace, key, value);
return false;
}
}
}呼叫功能
using System;
using System.Globalization;
namespace TryEnyim
{
class Program
{
static void Main(string[] args)
{
var cache = new Memcached.MemcacheClient();
Console.Write("Writing to cache: " + cache.SetItem("myString", "This is enyim @" + System.DateTime.Now.ToString(CultureInfo.InvariantCulture)));
cache.Replace("myString", "This is enyim again @" + System.DateTime.Now.AddSeconds(10).ToString(CultureInfo.InvariantCulture));
Console.WriteLine("Read key now: " + cache.GetItem<string>("myString"));// <----------getting exception here
Console.ReadLine();
}
}}
我正在使用来自NuGet的Enyim包(版本2.12.0.0)。
任何帮助都是有用的。
谢谢
发布于 2013-07-11 06:44:50
终于来了!我找到解决办法了。
在花费时间进行代码调试之后,我决定将Memcached服务器从1.2.4升级到1.4.5。
在Memcached服务器升级之后,Enyim缓存客户端就像魅力一样工作。
简单解决方案:
用最新版本的.升级Memcached服务器
https://stackoverflow.com/questions/17567752
复制相似问题