using System;using System.Collections.Generic;using System.Linq;using System.Text;using Memcached.ClientLibrary;using System.Collections;namespace MemcachedTest{ public class Memcache:ICache { private MemcachedClient client; private static Memcache memcache; /// <summary> /// 构造方法 /// </summary> protected Memcache() { SockIOPool pool = SockIOPool.GetInstance(); string[] servers = { "127.0.0.1:11211" }; pool.SetServers(servers); pool.MinConnections = 3; pool.MaxConnections = 5; pool.InitConnections = 3; pool.SocketConnectTimeout = 5000; pool.Initialize(); this.client = new MemcachedClient(); client.EnableCompression = false; } public static Memcache Instance() { if (memcache == null) { memcache = new Memcache(); } return memcache; } /// <summary> /// 判断是否包含某个键 /// </summary> /// <param name="argKey">键值</param> /// <returns></returns> public bool ContainKey(string argKey) { return client.KeyExists(argKey); } /// <summary> /// 添加缓存数据 /// </summary> /// <param name="argKey">键值</param> /// <param name="argValue">存储值</param> /// <returns></returns> public bool Add(string argKey, object argValue) { return client.Add(argKey,argValue); } /// <summary> /// 添加缓存数据 /// </summary> /// <param name="argKey">键值</param> /// <param name="argValue">存储值</param> /// <param name="argDateExpiration">过期时间</param> /// <returns></returns> public bool Add(string argKey, object argValue, DateTime argDateExpiration) { return client.Add(argKey, argValue, argDateExpiration); } /// <summary> /// 添加缓存数据 /// </summary> /// <typeparam name="T">存储对象类型</typeparam> /// <param name="argKey">键值</param> /// <param name="entity">存储值</param> /// <returns></returns> public bool Add<T>(string argKey, T entity) where T : class { return client.Add(argKey, entity); } /// <summary> /// 添加缓存数据 /// </summary> /// <typeparam name="T">存储对象类型</typeparam> /// <param name="argKey">键值</param> /// <param name="entity">存储值</param> /// <param name="argDateExpiration">过期时间</param> /// <returns></returns> public bool Add<T>(string argKey, T entity, DateTime argDateExpiration) where T : class { return client.Add(argKey, entity, argDateExpiration); } /// <summary> /// 添加缓存数据,如果存在则替换原有数据 /// </summary> /// <param name="argKey">键值</param> /// <param name="argValue">存储值</param> /// <returns></returns> public bool Set(string argKey, object argValue) { if (ContainKey(argKey)) { return false; } return client.Set(argKey, argValue); } /// <summary> /// 添加缓存数据,如果存在则替换原有数据 /// </summary> /// <param name="argKey">键值</param> /// <param name="argValue">存储值</param> /// <param name="argDateExpiration">过期时间</param> /// <returns></returns> public bool Set(string argKey, object argValue, DateTime argDateExpiration) { if (ContainKey(argKey)) { return false; } return client.Set(argKey, argValue, argDateExpiration); } /// <summary> /// 添加缓存数据,如果存在则替换原有数据 /// </summary> /// <typeparam name="T">存储对象类型</typeparam> /// <param name="argKey">键值</param> /// <param name="entity">存储值</param> /// <returns></returns> public bool Set<T>(string argKey, T entity) where T : class { if (ContainKey(argKey)) { return false; } return client.Set(argKey, entity); } /// <summary> /// 添加缓存数据,如果存在则替换原有数据 /// </summary> /// <typeparam name="T">存储对象类型</typeparam> /// <param name="argKey">键值</param> /// <param name="entity">存储值</param> /// <param name="argDateExpiration">过期时间</param> /// <returns></returns> public bool Set<T>(string argKey, T entity, DateTime argDateExpiration) where T : class { if (ContainKey(argKey)) { return false; } return client.Set(argKey, entity, argDateExpiration); } /// <summary> /// 替换原有缓存 /// </summary> /// <param name="argKey">键值</param> /// <param name="argValue">存储值</param> /// <returns></returns> public bool Replace(string argKey, object argValue) { return client.Replace(argKey,argValue); } /// <summary> /// 替换原有缓存 /// </summary> /// <param name="argKey">键值</param> /// <param name="argValue">存储值</param> /// <param name="argDateExpiration">过期时间</param> /// <returns></returns> public bool Replace(string argKey, object argValue, DateTime argDateExpiration) { return client.Replace(argKey,argValue,argDateExpiration); } /// <summary> /// 替换原有缓存 /// </summary> /// <typeparam name="T">存储类型</typeparam> /// <param name="argKey">键值</param> /// <param name="entity">存储值</param> /// <returns></returns> public bool Replace<T>(string argKey, T entity) where T : class { return client.Replace(argKey,entity); } /// <summary> /// 替换原有缓存 /// </summary> /// <typeparam name="T">存储类型</typeparam> /// <param name="argKey">键值</param> /// <param name="entity">存储值</param> /// <param name="argDateExpiration">过期时间</param> /// <returns></returns> public bool Replace<T>(string argKey, T entity, DateTime argDateExpiration) where T : class { return client.Replace(argKey, entity,argDateExpiration); } /// <summary> /// 获得缓存数据 /// </summary> /// <param name="argKey">键值</param> /// <returns></returns> public object Get(string argKey) { return client.Get(argKey); } /// <summary> /// 获得缓存数据 /// </summary> /// <typeparam name="T">返回数据类型</typeparam> /// <param name="argKey">键值</param> /// <returns></returns> public T Get<T>(string argKey) { T entity=default(T); entity = (T)client.Get(argKey); return entity; } /// <summary> /// 移除一个缓存数据 /// </summary> /// <param name="argKey">键值</param> /// <returns></returns> public bool Remove(string argKey) { return client.Delete(argKey); } /// <summary> /// 移除一个缓存数据 /// </summary> /// <param name="argKey">键值</param> /// <param name="argDateExpiration">数据过期时间</param> /// <returns></returns> public bool Remove(string argKey, DateTime argDateExpiration) { return client.Delete(argKey,argDateExpiration); } /// <summary> /// 移除所有缓存数据 /// </summary> /// <returns></returns> public bool Remove() { return client.FlushAll(); } /// <summary> /// 移除所有缓存数据 /// </summary> /// <param name="servers">服务器地址</param> /// <returns></returns> public bool Remove(ArrayList servers) { return client.FlushAll(servers); } }}
评论