程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# Memcached緩存用法實例詳解

C# Memcached緩存用法實例詳解

編輯:C#入門知識

C# Memcached緩存用法實例詳解。本站提示廣大學習愛好者:(C# Memcached緩存用法實例詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是C# Memcached緩存用法實例詳解正文


本文實例講述了C#中Memcached緩存的用法,分享給年夜家供年夜家參考。詳細辦法以下:

ICacheStrategy.cs文件以下:

public interface ICacheStrategy
{
        /// <summary>
        /// 添加數據到緩存
        /// </summary>
        /// <param name="objId">緩存稱號</param>
        /// <param name="o">緩存內容</param>
        /// <param name="second">緩存時光(秒)</param>
        void Add(string objId, object o, int second);

        /// <summary>
        /// 添加數據到緩存 (依附其它緩存)
        /// </summary>
        /// <param name="objId">緩存稱號</param>
        /// <param name="o">緩存內容</param>
        /// <param name="second">緩存時光(秒)</param>
        /// <param name="dependKey">依附緩存稱號數組</param>
        void AddCacheDepend(string objId, object o, int second, string[] dependKey);

        /// <summary>
        /// 添加數據到緩存 (依附文件)
        /// </summary>
        /// <param name="objId">緩存稱號</param>
        /// <param name="o">緩存內容</param>
        /// <param name="second">緩存時光(秒)</param>
        /// <param name="files">依附緩存稱號文件名數組</param>
        void AddFileDepend(string objId, object o, int second, string[] files);

        /// <summary>
        /// 刪除指定緩存
        /// </summary>
        /// <param name="objId"></param>
        void RemoveCache(string objId);

        /// <summary>
        /// 刪除一切緩存
        /// </summary>
        void RemoveCacheAll();

        /// <summary>
        /// 刪除婚配到的緩存
        /// </summary>
        /// <param name="pattern"></param>
        /// <returns></returns>
        void RemoveCacheRegex(string pattern);

        /// <summary>
        /// 獲得一切緩存鍵
        /// </summary>
        /// <returns></returns>
        IList<string> GetCacheKeys();

        /// <summary>
        /// 搜刮 婚配到的緩存
        /// </summary>
        /// <param name="pattern"></param>
        /// <returns></returns>
        IList<string> SearchCacheRegex(string pattern);

        /// <summary>
        /// 取得緩存數據
        /// </summary>
        /// <param name="objId"></param>
        /// <returns></returns>
        object GetCacheData(string objId);

        /// <summary>
        /// 斷定此緩存能否有用
        /// </summary>
        /// <param name="objID"></param>
        /// <returns></returns>
        bool HasCache(string objID);
}

MemcachedStrategy.cs文件以下:

public class MemcachedStrategy : ICacheStrategy
{
        #region ICacheStrategy 成員

        /// <summary>
        /// 添加數據到緩存
        /// </summary>
        /// <param name="objId">緩存稱號</param>
        /// <param name="o">緩存內容</param>
        /// <param name="second">緩存時光(秒)</param>
        public void Add(string objId, object o, int second)
        {
            if (second > 0)
            {
                MemcachedManager.CacheClient.Set(objId, o, DateTime.Now.AddSeconds(second));
            }
            else
            {
                MemcachedManager.CacheClient.Set(objId, o);
            }
        }

        #region 緩存依附沒有效到memcached
        /// <summary>
        /// 添加數據到緩存 (依附其它緩存)
        /// </summary>
        /// <param name="objId">緩存稱號</param>
        /// <param name="o">緩存內容</param>
        /// <param name="second">緩存時光(秒)</param>
        /// <param name="dependKey">依附緩存稱號數組</param>
        public void AddCacheDepend(string objId, object o, int second, string[] dependKey)
        {
            CacheDependency dependencies = new CacheDependency(null, dependKey, DateTime.Now);
            HttpRuntime.Cache.Insert(objId, o, dependencies, DateTime.Now.AddSeconds((double)second), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
        }

        /// <summary>
        /// 添加數據到緩存 (依附文件)
        /// </summary>
        /// <param name="objId">緩存稱號</param>
        /// <param name="o">緩存內容</param>
        /// <param name="second">緩存時光(秒)</param>
        /// <param name="files">依附緩存稱號文件名數組</param>
        public void AddFileDepend(string objId, object o, int second, string[] files)
        {
            CacheDependency dependencies = new CacheDependency(files, DateTime.Now);
            HttpRuntime.Cache.Insert(objId, o, dependencies, DateTime.Now.AddSeconds((double)second), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
        }
        #endregion

        /// <summary>
        /// 刪除指定緩存
        /// </summary>
        /// <param name="objId"></param>
        public void RemoveCache(string objId)
        {
            if (MemcachedManager.CacheClient.KeyExists(objId))
                MemcachedManager.CacheClient.Delete(objId);
        }

        /// <summary>
        /// 刪除一切緩存
        /// </summary>
        public void RemoveCacheAll()
        {
            MemcachedManager.CacheClient.FlushAll();
        }

        /// <summary>
        /// 刪除婚配到的緩存
        /// </summary>
        /// <param name="pattern"></param>
        public void RemoveCacheRegex(string pattern)
        {
            IList<string> list = SearchCacheRegex(pattern);
            foreach (var item in list)
            {
                MemcachedManager.CacheClient.Delete(item);
            }
        }

        /// <summary>
        /// 獲得一切緩存鍵
        /// </summary>
        /// <returns></returns>
        public IList<string> GetCacheKeys()
        {
            return MemcachedManager.GetAllKeys();
        }

        /// <summary>
        /// 搜刮 婚配到的緩存
        /// </summary>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public IList<string> SearchCacheRegex(string pattern)
        {
            List<string> l = new List<string>();
            IList<string> cacheKeys = MemcachedManager.GetAllKeys();
            foreach (var item in cacheKeys)
            {
                if (Regex.IsMatch(item, pattern))
                {
                    l.Add(item);
                }
            }
            return l.AsReadOnly();
        }

        /// <summary>
        /// 取得緩存數據
        /// </summary>
        /// <param name="objId"></param>
        /// <returns></returns>
        public object GetCacheData(string objId)
        {
            return MemcachedManager.CacheClient.Get(objId);
        }

        /// <summary>
        /// 斷定此緩存能否有用
        /// </summary>
        /// <param name="objID"></param>
        /// <returns></returns>
        public bool HasCache(string objID)
        {
            return MemcachedManager.CacheClient.KeyExists(objID);
        }
        #endregion
}

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved