Memcached通用类(基于enyim.com Memcached Client)
生活随笔
收集整理的這篇文章主要介紹了
Memcached通用类(基于enyim.com Memcached Client)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一.如果用官方提供的方法,在web.config里面配置好了各個參數(shù)和服務(wù)器IP。如下圖:
<?xml version="1.0"?> <configuration><configSections> <sectionGroup name="enyim.com"><section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching"/></sectionGroup><section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> </configSections><enyim.com> <memcached protocol="Text"><servers> <add address="127.0.0.1" port="11211" /><add address="127.0.0.1" port="11212" /><add address="127.0.0.1" port="11213" /><add address="127.0.0.1" port="11214" /></servers><socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:05" deadTimeout="00:02:00" /></memcached></enyim.com> <system.web><compilation debug="true" targetFramework="4.0"/></system.web> </configuration>則使用該通用類即可,組件自動調(diào)用web.config里面的配置。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Enyim.Caching; using Enyim.Caching.Configuration; using Enyim.Caching.Memcached;/// <summary> /// MemberHelper 的摘要說明 /// </summary> public abstract class MemberHelper {public MemberHelper(){//// TODO: 在此處添加構(gòu)造函數(shù)邏輯//}#region 添加緩存/// <summary>/// 添加緩存(鍵不存在則添加,存在則替換)/// </summary>/// <param name="key">鍵</param>/// <param name="value">值</param>/// <returns></returns>public static bool AddCache(string key, object value){using (MemcachedClient mc = new MemcachedClient()){return mc.Store(StoreMode.Set, key, value);}}#endregion#region 添加緩存/// <summary>/// 添加緩存(鍵不存在則添加,存在則替換)/// </summary>/// <param name="key">鍵</param>/// <param name="value">值</param>/// <param name="minutes">緩存時間(分鐘)</param>/// <returns></returns>public static bool AddCache(string key, object value, int minutes){ using (MemcachedClient mc = new MemcachedClient()){return mc.Store(StoreMode.Set, key, value, DateTime.Now.AddMinutes(minutes));}}#endregion#region 獲取緩存/// <summary>/// 獲取緩存/// </summary>/// <param name="key">鍵</param>/// <returns>返回緩存,沒有找到則返回null</returns>public static object GetCache(string key){using (MemcachedClient mc = new MemcachedClient()){return mc.Get(key);}}#endregion#region 是否存在該緩存/// <summary>/// 是否存在該緩存/// </summary>/// <param name="key">鍵</param>/// <returns></returns>public static bool IsExists(string key){using (MemcachedClient mc = new MemcachedClient()){return mc.Get(key) != null;}}#endregion#region 刪除緩存(如果鍵不存在,則返回false)/// <summary>/// 刪除緩存(如果鍵不存在,則返回false)/// </summary>/// <param name="key">鍵</param>/// <returns>成功:true失敗:false</returns>public static bool DelCache(string key){using (MemcachedClient mc = new MemcachedClient()){return mc.Remove(key);}}#endregion#region 清空緩存/// <summary>/// 清空緩存/// </summary>public static void FlushCache(){using (MemcachedClient mc = new MemcachedClient()){mc.FlushAll();}}#endregion}二.如果不想在web.config配置,那就使用下面的通用類。
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Net; using System.Web; using Enyim.Caching; using Enyim.Caching.Configuration; using Enyim.Caching.Memcached;/// <summary> /// MemberHelper 的摘要說明 /// </summary> public abstract class MemberHelper {public MemberHelper(){//// TODO: 在此處添加構(gòu)造函數(shù)邏輯//}#region 創(chuàng)建Memcache客戶端/// <summary>/// 創(chuàng)建Memcache客戶端/// </summary>/// <param name="serverList">服務(wù)列表</param>/// <returns></returns>private static MemcachedClient CreateServer(List<IPEndPoint> serverList){MemcachedClientConfiguration config = new MemcachedClientConfiguration();//創(chuàng)建配置參數(shù)for (int i = 0; i < serverList.Count; i++){config.Servers.Add(new System.Net.IPEndPoint(IPAddress.Parse(serverList[i].Address.ToString()), serverList[i].Port));//增加服務(wù)節(jié)點}config.Protocol = MemcachedProtocol.Text;config.Authentication.Type = typeof(PlainTextAuthenticator);//設(shè)置驗證模式config.Authentication.Parameters["userName"] = "uid";//用戶名參數(shù)config.Authentication.Parameters["password"] = "pwd";//密碼參數(shù)MemcachedClient mac = new MemcachedClient(config);//創(chuàng)建客戶端return mac;}#endregion#region 添加緩存/// <summary>/// 添加緩存(鍵不存在則添加,存在則替換)/// </summary>/// <param name="serverList">服務(wù)器列表</param>/// <param name="key">鍵</param>/// <param name="value">值</param>/// <returns></returns>public static bool AddCache(List<IPEndPoint> serverList, string key, object value){using (MemcachedClient mc = CreateServer(serverList)){return mc.Store(StoreMode.Set, key, value);}}#endregion#region 添加緩存/// <summary>/// 添加緩存(鍵不存在則添加,存在則替換)/// </summary>/// <param name="serverList">服務(wù)器列表</param>/// <param name="key">鍵</param>/// <param name="value">值</param>/// <param name="minutes">緩存時間(分鐘)</param>/// <returns></returns>public static bool AddCache(List<IPEndPoint> serverList,string key, object value, int minutes){using (MemcachedClient mc = CreateServer(serverList)){return mc.Store(StoreMode.Set, key, value, DateTime.Now.AddMinutes(minutes));}}#endregion#region 獲取緩存/// <summary>/// 獲取緩存/// </summary>/// <param name="serverList">服務(wù)器列表</param>/// <param name="key">鍵</param>/// <returns>返回緩存,沒有找到則返回null</returns>public static object GetCache(List<IPEndPoint> serverList,string key){using (MemcachedClient mc = CreateServer(serverList)){return mc.Get(key);}}#endregion#region 是否存在該緩存/// <summary>/// 是否存在該緩存/// </summary>/// <param name="serverList">服務(wù)器列表</param>/// <param name="key">鍵</param>/// <returns></returns>public static bool IsExists(List<IPEndPoint> serverList,string key){using (MemcachedClient mc = CreateServer(serverList)){return mc.Get(key) != null;}}#endregion#region 刪除緩存(如果鍵不存在,則返回false)/// <summary>/// 刪除緩存(如果鍵不存在,則返回false)/// </summary>/// <param name="serverList">服務(wù)器列表</param>/// <param name="key">鍵</param>/// <returns>成功:true失敗:false</returns>public static bool DelCache(List<IPEndPoint> serverList, string key){using (MemcachedClient mc = CreateServer(serverList)){return mc.Remove(key);}}#endregion#region 清空緩存/// <summary>/// 清空緩存/// </summary>/// <param name="serverList">服務(wù)器列表</param>public static void FlushCache(List<IPEndPoint> serverList){using (MemcachedClient mc = CreateServer(serverList)){mc.FlushAll();}}#endregion}總結(jié)
以上是生活随笔為你收集整理的Memcached通用类(基于enyim.com Memcached Client)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MemCached配置与缓存知识概述
- 下一篇: memcached协议