• asp.net下配置使用Memcached 如何使用Memcached .ne使用BeITMemcached.dllt配置Memcached的方法


    首先在项目中引用 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

    关于调用的类

     public class Article 
        {
    
            private static readonly DAL.Article dal = new DAL.Article();
            private static MemcachedHelper cache = new MemcachedHelper("mem176"); 
    
      public ArticleInfo GetArticleInfo(int articleId)
            {
                ArticleInfo result = null;
                string key = cacheKey + "_ArticleInfo_" + articleId;
                object obj = cache.Get(key);
                if (obj != null)
                    result = (ArticleInfo)obj;
                else
                {
                    result = dal.Get(articleId);
                    if (result != null)
                        cache.Set(key, result);
                }
                return result;
            }
        }
    View Code

     下载BeITMemcached.dll

  • 相关阅读:
    6、linux中同步、互斥、阻塞(原子操作、信号量、阻塞)
    lightOJ-1199 Partitioning Game (SG函数)
    HDU-1013 Digital Roots
    HDU-1004 Let the Balloon Rise (STL map)
    HDU-1020 Encoding (字符串)
    POJ-2524 Ubiquitous Religions (并查集)
    POJ-1988 Cube Stacking (带权并查集)
    POJ-2236 Wireless Network (并查集)
    HDU-1002 A + B Problem II (模拟大数相加)
    HDU-1829 A Bug's Life (种类并查集)
  • 原文地址:https://www.cnblogs.com/yonsy/p/4378521.html
Copyright © 2020-2023  润新知