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 "  添加缓存  "
        ///
        /// 添加缓存项(服务器上如果Key存在则不添加,否则添加)
        ///

        /// 缓存类型
        /// 缓存键值
        /// 缓存值
        /// 是否添加缓存成功
        public static bool Add(string key,T value)
        {           
           return m_clientProxy.Add(key,value);          
        }
         ///
        /// 添加缓存项(服务器上如果Key存在则不添加,否则添加)
        ///

        /// 缓存类型
        /// 缓存键值
        /// 缓存值
        /// 缓存过期时间(单位:秒)
        /// 是否添加缓存成功      
        public static bool Add(string key,T value,long numOfSeconds)
        {
            return m_clientProxy.Add(key,value,DateTime.Now.AddSeconds(numOfSeconds));          
        }      
        #endregion
        #region "  设置缓存  "
        ///
        /// 设置缓存项(服务器上key存在就替换,不存在就添加)
        ///

        /// 缓存类型
        /// 缓存键值
        /// 缓存值
        /// 是否设置缓存成功
        public static bool Set(string key,T value)
        {          
           return m_clientProxy.Set(key,value);           
        }
        ///
        /// 设置缓存项(服务器上key存在就替换,不存在就添加)
        ///

        /// 缓存类型
        /// 缓存键值
        /// 缓存值
        /// 缓存过期时间(单位:秒)
        /// 是否设置缓存成功
        public static bool Set(string key,T value,long numOfSeconds)
        {
            return m_clientProxy.Set(key,value,DateTime.Now.AddSeconds(numOfSeconds));          
        }      
        #endregion
        #region "  替换缓存  "
        ///
        /// 替换缓存项(服务器上Key存在则替换)
        ///

        /// 缓存类型
        /// 缓存键值
        /// 缓存值
        /// 是否替换缓存成功
        public static bool Replace(string key,T value)
        {           
           return m_clientProxy.Replace(key,value);           
        }
        ///
        /// 替换缓存项(服务器上Key存在则替换)
        ///

        /// 缓存类型
        /// 缓存键值
        /// 缓存值
        /// 缓存过期时间(单位:秒)
        /// 是否替换缓存成功      
        public static bool Replace(string key,T value,long numOfSeconds)
        {
            return m_clientProxy.Replace(key,value,DateTime.Now.AddSeconds(numOfSeconds));        
        }      
        #endregion
        #region "  移除缓存  "
        ///
        /// 移除缓存项(服务器上Key存在就移除)
        ///

        /// 缓存项的键值
        /// 是否移除成功
        public static bool Delete(string key)
        {
           return m_clientProxy.Delete(key);            
        }
        #endregion
        #region "  获取缓存  "
        ///
        /// 获取缓存项的值
        ///

        /// 缓存项的类型
        /// 缓存项的键值
        /// 获取到的缓存项
        public static T Get(string key)
        {
            object obj = null;          
          
            obj = m_clientProxy.Get(key);                  
            return (T)obj;
        }
        ///
        /// 批量获取缓存
        ///

        /// 缓存类型
        /// 缓存键数组
        ///
        public static Hashtable GetMultiple(string[] keys)
        {
            Hashtable htTemp = null;
                        
            htTemp = m_clientProxy.GetMultiple(keys);
            return htTemp;
        }
        ///
        /// 批量获取缓存
        ///

        /// 缓存类型
        /// 缓存键数组
        ///
        public static object[] GetMultipleArray(string[] keys)
        {
            object[] obj = null;
                         
            obj = m_clientProxy.GetMultipleArray(keys);
            return obj;
        }
        #endregion
        #region "  清空缓存  "
        ///
        /// 清空缓存
        ///

        ///
        public static bool FlushAll()
        {          
            return m_clientProxy.FlushAll();          
        }
        ///
        /// 清空特定服务器的缓存
        ///

        ///
        ///
        public static bool FlushAll(ArrayList flushServerList)
        {         
           return m_clientProxy.FlushAll(flushServerList);          
        }
        #endregion                     
        #region "  获取HashCode  "
        ///
        /// 返回字符串的哈希值
        ///

        ///
        ///
        public static string GetHashCode(string strValue)
        {
            return strValue.GetHashCode().ToString();
        }     
        #endregion      
        #region "  获取缓存服务器状态  "
        ///
        /// 获取缓存服务器状态信息
        ///

        ///
        public static Hashtable GetStatus()
        {
            return m_clientProxy.Stats();
        }    
        #endregion      
        #region "  获取缓存服务器列表  "
        ///
        /// 获取缓存服务器地址列表
        ///

        ///
        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("缓存服务器地址未配置,配置格式应为[]!");
            }
        }
        #endregion      
        #region "  获取SockIOPoolServerList  "
        ///
        /// 获取SockIOPoolServerList
        ///

        ///
        public static ArrayList GetSockIOPoolServerList()
        {
            SockIOPool pool = SockIOPool.GetInstance();
            if(null != pool)
            {
                return pool.Servers;
            }
            return null;
        }
        #endregion
        #endregion
    }
 
 
范例程序请下载附件!
 
//深圳E搜科技(搜索引擎技术钻研者!)
//QQ群:15911745
//QQ:448114915;934724029
//Mobile:13168078506;13713628016
//Email:[email protected];[email protected]