欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C# Memcached缓存用法实例详解

时间:2020-10-10 22:23:05|栏目:.NET代码|点击:

本文实例讲述了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#程序设计有所帮助。

上一篇:asp.net下将页面内容导入到word模板中的方法

栏    目:.NET代码

下一篇:C#中接口(interface)的理解

本文标题:C# Memcached缓存用法实例详解

本文地址:http://www.codeinn.net/misctech/9527.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有