Memcached服务的安装完成后,就可以调用其客户端对缓存进行操作了,由于它自带客户端程序是弱类型的,所以我们有必要将其重新包装一下,使其成为强类的客户端程序,
类代码如下:
public static class DistributedCache
{
#region " 变量定义 "
//缓存服务器地址列表
private static string[] m_cacheServerList = null;
//缓存客户端操作代理
private static MemcachedClient m_clientProxy = null;
//缓存服务器地址列表分隔符
private const char m_serverListValueDelimiter = ',';
//缓存服务器地址配置文件Key
private const string m_cacheServerListKey = "CacheServerList";
#endregion
#region " 构造函数 "
static DistributedCache()
{
SockIOPool pool = SockIOPool.GetInstance();
if(null == pool)
{
throw new ApplicationException("创建SockIOPool实例失败!");
}
GetCacheServerList();
pool.SetServers(m_cacheServerList);
pool.InitConnections = 3;
pool.MinConnections = 3;
pool.MaxConnections = 10;
pool.SocketConnectTimeout = 1000;
pool.SocketTimeout = 3000;
pool.MaintenanceSleep = 30;
pool.Failover = true;
pool.Nagle = false;
pool.Initialize();
m_clientProxy = new MemcachedClient();
m_clientProxy.EnableCompression = false;
}
#endregion
#region " 函数与过程 "
#region " 添加缓存 "
/// <summary>
/// 添加缓存项(服务器上如果Key存在则不添加,否则添加)
/// </summary>
/// <typeparam name="T">缓存类型</typeparam>
/// <param name="key">缓存键值</param>
/// <param name="value">缓存值</param>
/// <returns>是否添加缓存成功</returns>
public static bool Add<T>(string key,T value)
{
return m_clientProxy.Add(key,value);
}
/// <summary>
/// 添加缓存项(服务器上如果Key存在则不添加,否则添加)
/// </summary>
/// <typeparam name="T">缓存类型</typeparam>
/// <param name="key">缓存键值</param>
/// <param name="value">缓存值</param>
/// <param name="numOfSeconds">缓存过期时间(单位:秒)</param>
/// <returns>是否添加缓存成功</returns>
public static bool Add<T>(string key,T value,long numOfSeconds)
{
return m_clientProxy.Add(key,value,DateTime.Now.AddSeconds(numOfSeconds));
}
#endregion
#region " 设置缓存 "
/// <summary>
/// 设置缓存项(服务器上key存在就替换,不存在就添加)
/// </summary>
/// <typeparam name="T">缓存类型</typeparam>
/// <param name="key">缓存键值</param>
/// <param name="value">缓存值</param>
/// <returns>是否设置缓存成功</returns>
public static bool Set<T>(string key,T value)
{
return m_clientProxy.Set(key,value);
}
/// <summary>
/// 设置缓存项(服务器上key存在就替换,不存在就添加)
/// </summary>
/// <typeparam name="T">缓存类型</typeparam>
/// <param name="key">缓存键值</param>
/// <param name="value">缓存值</param>
/// <param name="numOfSeconds">缓存过期时间(单位:秒)</param>
/// <returns>是否设置缓存成功</returns>
public static bool Set<T>(string key,T value,long numOfSeconds)
{
return m_clientProxy.Set(key,value,DateTime.Now.AddSeconds(numOfSeconds));
}
#endregion
#region " 替换缓存 "
/// <summary>
/// 替换缓存项(服务器上Key存在则替换)
/// </summary>
/// <typeparam name="T">缓存类型</typeparam>
/// <param name="key">缓存键值</param>
/// <param name="value">缓存值</param>
/// <returns>是否替换缓存成功</returns>
public static bool Replace<T>(string key,T value)
{
return m_clientProxy.Replace(key,value);
}
/// <summary>
/// 替换缓存项(服务器上Key存在则替换)
/// </summary>
/// <typeparam name="T">缓存类型</typeparam>
/// <param name="key">缓存键值</param>
/// <param name="value">缓存值</param>
/// <param name="numOfSeconds">缓存过期时间(单位:秒)</param>
/// <returns>是否替换缓存成功</returns>
public static bool Replace<T>(string key,T value,long numOfSeconds)
{
return m_clientProxy.Replace(key,value,DateTime.Now.AddSeconds(numOfSeconds));
}
#endregion
#region " 移除缓存 "
/// <summary>
/// 移除缓存项(服务器上Key存在就移除)
/// </summary>
/// <param name="key">缓存项的键值</param>
/// <returns>是否移除成功</returns>
public static bool Delete(string key)
{
return m_clientProxy.Delete(key);
}
#endregion
#region " 获取缓存 "
/// <summary>
/// 获取缓存项的值
/// </summary>
/// <typeparam name="T">缓存项的类型</typeparam>
/// <param name="key">缓存项的键值</param>
/// <returns>获取到的缓存项</returns>
public static T Get<T>(string key)
{
object obj = null;
obj = m_clientProxy.Get(key);
return (T)obj;
}
/// <summary>
/// 批量获取缓存
/// </summary>
/// <typeparam name="T">缓存类型</typeparam>
/// <param name="keys">缓存键数组</param>
/// <returns></returns>
public static Hashtable GetMultiple(string[] keys)
{
Hashtable htTemp = null;
htTemp = m_clientProxy.GetMultiple(keys);
return htTemp;
}
/// <summary>
/// 批量获取缓存
/// </summary>
/// <typeparam name="T">缓存类型</typeparam>
/// <param name="keys">缓存键数组</param>
/// <returns></returns>
public static object[] GetMultipleArray(string[] keys)
{
object[] obj = null;
obj = m_clientProxy.GetMultipleArray(keys);
return obj;
}
#endregion
#region " 清空缓存 "
/// <summary>
/// 清空缓存
/// </summary>
/// <returns></returns>
public static bool FlushAll()
{
return m_clientProxy.FlushAll();
}
/// <summary>
/// 清空特定服务器的缓存
/// </summary>
/// <param name="flushServerList"></param>
/// <returns></returns>
public static bool FlushAll(ArrayList flushServerList)
{
return m_clientProxy.FlushAll(flushServerList);
}
#endregion
#region " 获取HashCode "
/// <summary>
/// 返回字符串的哈希值
/// </summary>
/// <param name="strValue"></param>
/// <returns></returns>
public static string GetHashCode(string strValue)
{
return strValue.GetHashCode().ToString();
}
#endregion
#region " 获取缓存服务器状态 "
/// <summary>
/// 获取缓存服务器状态信息
/// </summary>
/// <returns></returns>
public static Hashtable GetStatus()
{
return m_clientProxy.Stats();
}
#endregion
#region " 获取缓存服务器列表 "
/// <summary>
/// 获取缓存服务器地址列表
/// </summary>
/// <returns></returns>
private static void GetCacheServerList()
{
string serverList = "";
serverList = ConfigurationManager.AppSettings[m_cacheServerListKey];
if(!string.IsNullOrEmpty(serverList))
{
m_cacheServerList = serverList.Split(new char[] { m_serverListValueDelimiter },StringSplitOptions.RemoveEmptyEntries);
}
if(null == m_cacheServerList || m_cacheServerList.Length == 0)
{
throw new ApplicationException("缓存服务器地址未配置,配置格式应为[<add key=\"CacheServerList\" value=\"192.168.10.1:11211\"/>]!");
}
}
#endregion
#region " 获取SockIOPoolServerList "
/// <summary>
/// 获取SockIOPoolServerList
/// </summary>
/// <returns></returns>
public static ArrayList GetSockIOPoolServerList()
{
SockIOPool pool = SockIOPool.GetInstance();
if(null != pool)
{
return pool.Servers;
}
return null;
}
#endregion
#endregion
}
范例程序请下载附件!