现在发现现在发现一个奇怪的问题
bool isok = strategy.Insert("abc2",“的顶顶顶顶顶”,30);
if (isok)
{
obj = strategy.Get("abc2");
}
如果指定过期时间isok=true 但是 获取时却是null
如果不指定时间,就没问题 可以正常取值
public class CacheStrategy
{
private MemcachedClient mc = null;
public CacheStrategy()
{
MemcachedClientConfiguration config = new MemcachedClientConfiguration();
for (int i = 0; i < 1; i++)
{
string[] str = "127.0.0.1:11211".Split(':');
config.Servers.Add(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(str[0]), Convert.ToInt32((str[1]))));//增加服务节点
}
// config.Protocol = MemcachedProtocol.Binary;
//config.SocketPool.ConnectionTimeout = TimeSpan.FromSeconds(100);
//config.SocketPool.DeadTimeout = TimeSpan.FromSeconds(100);
//config.SocketPool.MaxPoolSize = 100;
//config.SocketPool.MinPoolSize = 100;
//config.SocketPool.QueueTimeout = TimeSpan.FromSeconds((double)JYConfig.MemcachedCacheConfig.QueueTimeOut);
// config.SocketPool.ReceiveTimeout = TimeSpan.FromSeconds(100);
//config.Authentication.Type = typeof(PlainTextAuthenticator);//设置验证模式
//config.Authentication.Parameters["userName"] = "uid";//用户名参数
//config.Authentication.Parameters["password"] = "pwd";//密码参数
mc = new Enyim.Caching.MemcachedClient(config);//创建客户端
//MemcachedClient.Setup("MyCache", new string[] { "127.0.0.1:11211" });
//mc = MemcachedClient.GetInstance("MyCache");
//mc.SendReceiveTimeout = 5000;
//mc.ConnectTimeout = 5000;
//mc.MinPoolSize = 1;
//mc.MaxPoolSize = 5;
}
/// <summary>
/// 获得指定键的缓存值
/// </summary>
/// <param name="key">缓存键</param>
/// <returns>缓存值</returns>
public object Get(string key)
{
return mc.Get(key);
}
/// <summary>
/// 将指定键的对象添加到缓存中
/// </summary>
/// <param name="key">缓存键</param>
/// <param name="data">缓存值</param>
public bool Insert(string key, object data)
{
return mc.Store(StoreMode.Set, key, data);
}
/// <summary>
/// 将指定键的对象添加到缓存中,并指定过期时间
/// </summary>
/// <param name="key">缓存键</param>
/// <param name="data">缓存值</param>
/// <param name="cacheTime">缓存过期时间</param>
public bool Insert(string key, object data, int cacheTime)
{
return mc.Store(StoreMode.Set, key, data, DateTime.Now.AddMinutes(cacheTime));
}
/// <summary>
/// 从缓存中移除指定键的缓存值
/// </summary>
/// <param name="key">缓存键</param>
public void Remove(string key)
{
mc.Remove(key);
}
/// <summary>
/// 清空所有缓存对象
/// </summary>
public void Clear()
{
mc.FlushAll();
}
private int _timeout =180;//单位秒
/// <summary>
/// 缓存过期时间
/// </summary>
/// <value></value>
public int TimeOut
{
get
{
return _timeout;
}
set
{
_timeout = value > 0 ? value : 3600;
}
}
}
}