首先在项目中引用 BeITMemcached.dll

在Web.config中配置节点

    <configSections> 
        <section name="beitmemcached" type="System.Configuration.NameValueSectionHandler" />
    </configSections>
<!--必须紧接着configSections节点添加beitmemcached节点-->
    <beitmemcached>
        <add key="mem176" value="192.168.1.108:11211" />
    </beitmemcached>

操作缓存的类

    public class MemcachedHelper
    {
        BeIT.MemCached.MemcachedClient cache;
        public MemcachedHelper(string cacheServer)
        {
            string server = "mem176";
            if (!string.IsNullOrEmpty(cacheServer))
                server = cacheServer;
            cache = BeIT.MemCached.MemcachedClient.GetInstance(server);
        }

        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public bool Set(string key, object val)
        {
            key = key.Replace(" ", "");
            if (cache != null)
                cache.Set(key, val, DateTime.Now.AddHours(2));
            return false;
        }

        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <param name="expiry"></param>
        /// <returns></returns>
        public bool Set(string key, object val, DateTime expiry)
        {
            key = key.Replace(" ", "");
            if (cache != null)
                cache.Set(key, val, expiry);
            return false;
        }

        /// <summary>
        /// 读取缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public object Get(string key)
        {
            object obj = null;
            if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Request["delcache"] == "true")
                return null;
            key = key.Replace(" ", "");
            if (cache != null)
                obj = cache.Get(key);
            return obj;
        }
    }
View Code

相关文章:

  • 2022-12-23
  • 2022-01-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-11
  • 2018-06-05
猜你喜欢
  • 2021-12-10
  • 2021-07-16
  • 2021-06-27
  • 2022-12-23
  • 2022-01-24
  • 2022-03-06
相关资源
相似解决方案