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>

  

posted @ 2017-11-13 15:49  大师兄丶  阅读(2278)  评论(0编辑  收藏  举报