C#操作Memcached帮助类
在VS中安装Memcached,直接在NuGet下搜索Memcached,选择第一个进行安装:
服务端资源下载地址:https://pan.baidu.com/s/1gf3tupl
接下来开始写程序,老规矩,直接上代码:
public class Memcached : ICache { private static readonly MemcachedClient CacheClient = new MemcachedClient(); /// <summary> /// 获取缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="cacheKey"></param> /// <returns></returns> public T GetCache<T>(string cacheKey) where T : class { try { return (T)CacheClient.Get(cacheKey); } catch { return default(T); } } /// <summary> /// 写入缓存,过期时间默认10分钟 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> /// <param name="cacheKey"></param> public void WriteCache<T>(T value, string cacheKey) where T : class { //CacheClient.Store(StoreMode.Set, cacheKey, value); CacheClient.Store(Exists(cacheKey) ? StoreMode.Set : StoreMode.Replace, cacheKey, value, DateTimeHelper.Now.AddMinutes(10)); } /// <summary> /// 写入缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> /// <param name="cacheKey"></param> /// <param name="expireTime"></param> public void WriteCache<T>(T value, string cacheKey, DateTime expireTime) where T : class { //CacheClient.Store(StoreMode.Set, cacheKey, value, expireTime); CacheClient.Store(Exists(cacheKey) ? StoreMode.Set : StoreMode.Replace, cacheKey, value, expireTime); } /// <summary> /// 移除缓存 /// </summary> /// <param name="cacheKey"></param> public void RemoveCache(string cacheKey) { CacheClient.Remove(cacheKey); } /// <summary> /// 移除所有缓存 /// </summary> public void RemoveCache() { CacheClient.FlushAll(); } /// <summary> /// 是否存在 /// </summary> /// <param name="key"></param> /// <returns></returns> private static bool Exists(string key) { return CacheClient.Get(key) != null; } }
接口类:
public interface ICache { /// <summary> /// 读取缓存 /// </summary> /// <param name="cacheKey">键</param> /// <returns></returns> T GetCache<T>(string cacheKey) where T : class; /// <summary> /// 写入缓存 /// </summary> /// <param name="value">对象数据</param> /// <param name="cacheKey">键</param> void WriteCache<T>(T value, string cacheKey) where T : class; /// <summary> /// 写入缓存 /// </summary> /// <param name="value">对象数据</param> /// <param name="cacheKey">键</param> /// <param name="expireTime">到期时间</param> void WriteCache<T>(T value, string cacheKey, DateTime expireTime) where T : class; /// <summary> /// 移除指定数据缓存 /// </summary> /// <param name="cacheKey">键</param> void RemoveCache(string cacheKey); /// <summary> /// 移除全部缓存 /// </summary> void RemoveCache(); }
配置文件:
<configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration" /> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> <sectionGroup name="enyim.com"> <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" /> </sectionGroup> </configSections> <enyim.com> <memcached> <servers> <add address="192.168.1.12" port="11211" /> </servers> <socketPool minPoolSize="10" maxPoolSize="1000" connectionTimeout="00:00:10" deadTimeout="00:02:00" /> </memcached> </enyim.com>
作 者:大師兄丶
出 处:http://www.cnblogs.com/zhao-yi
Git 地 址:https://github.com/ZhaoYis
个人博客:http://www.zhaoyis.com.cn
关于作者:主要从事基于.Net Framework平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!
出 处:http://www.cnblogs.com/zhao-yi
Git 地 址:https://github.com/ZhaoYis
个人博客:http://www.zhaoyis.com.cn
关于作者:主要从事基于.Net Framework平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!