当前位置:  开发笔记 > 编程语言 > 正文

C# Memcached缓存用法实例详解

这篇文章主要介绍了C#中Memcached缓存用法,以实例形式详细讲述了在C#中针对Memcached缓存的各种操作,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了C#中Memcached缓存的用法,分享给大家供大家参考。具体方法如下:

ICacheStrategy.cs文件如下:

复制代码 代码如下:
public interface ICacheStrategy
{
        ///
        /// 添加数据到缓存
        ///

        /// 缓存名称
        /// 缓存内容
        /// 缓存时间(秒)
        void Add(string objId, object o, int second);

        ///


        /// 添加数据到缓存 (依赖其它缓存)
        ///

        /// 缓存名称
        /// 缓存内容
        /// 缓存时间(秒)
        /// 依赖缓存名称数组
        void AddCacheDepend(string objId, object o, int second, string[] dependKey);

        ///


        /// 添加数据到缓存 (依赖文件)
        ///

        /// 缓存名称
        /// 缓存内容
        /// 缓存时间(秒)
        /// 依赖缓存名称文件名数组
        void AddFileDepend(string objId, object o, int second, string[] files);

        ///


        /// 删除指定缓存
        ///

        ///
        void RemoveCache(string objId);

        ///


        /// 删除所有缓存
        ///

        void RemoveCacheAll();

        ///


        /// 删除匹配到的缓存
        ///

        ///
        ///
        void RemoveCacheRegex(string pattern);

        ///


        /// 获取所有缓存键
        ///

        ///
        IList GetCacheKeys();

        ///


        /// 搜索 匹配到的缓存
        ///

        ///
        ///
        IList SearchCacheRegex(string pattern);

        ///


        /// 获得缓存数据
        ///

        ///
        ///
        object GetCacheData(string objId);

        ///


        /// 判断此缓存是否有效
        ///

        ///
        ///
        bool HasCache(string objID);
}

MemcachedStrategy.cs文件如下:

复制代码 代码如下:
public class MemcachedStrategy : ICacheStrategy
{
        #region ICacheStrategy 成员

        ///


        /// 添加数据到缓存
        ///

        /// 缓存名称
        /// 缓存内容
        /// 缓存时间(秒)
        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
        ///


        /// 添加数据到缓存 (依赖其它缓存)
        ///

        /// 缓存名称
        /// 缓存内容
        /// 缓存时间(秒)
        /// 依赖缓存名称数组
        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);
        }

        ///


        /// 添加数据到缓存 (依赖文件)
        ///

        /// 缓存名称
        /// 缓存内容
        /// 缓存时间(秒)
        /// 依赖缓存名称文件名数组
        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

        ///


        /// 删除指定缓存
        ///

        ///
        public void RemoveCache(string objId)
        {
            if (MemcachedManager.CacheClient.KeyExists(objId))
                MemcachedManager.CacheClient.Delete(objId);
        }

        ///


        /// 删除所有缓存
        ///

        public void RemoveCacheAll()
        {
            MemcachedManager.CacheClient.FlushAll();
        }

        ///


        /// 删除匹配到的缓存
        ///

        ///
        public void RemoveCacheRegex(string pattern)
        {
            IList list = SearchCacheRegex(pattern);
            foreach (var item in list)
            {
                MemcachedManager.CacheClient.Delete(item);
            }
        }

        ///


        /// 获取所有缓存键
        ///

        ///
        public IList GetCacheKeys()
        {
            return MemcachedManager.GetAllKeys();
        }

        ///


        /// 搜索 匹配到的缓存
        ///

        ///
        ///
        public IList SearchCacheRegex(string pattern)
        {
            List l = new List();
            IList cacheKeys = MemcachedManager.GetAllKeys();
            foreach (var item in cacheKeys)
            {
                if (Regex.IsMatch(item, pattern))
                {
                    l.Add(item);
                }
            }
            return l.AsReadOnly();
        }

        ///


        /// 获得缓存数据
        ///

        ///
        ///
        public object GetCacheData(string objId)
        {
            return MemcachedManager.CacheClient.Get(objId);
        }

        ///


        /// 判断此缓存是否有效
        ///

        ///
        ///
        public bool HasCache(string objID)
        {
            return MemcachedManager.CacheClient.KeyExists(objID);
        }
        #endregion
}

希望本文所述对大家的C#程序设计有所帮助。

推荐阅读
ar_wen2402851455
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有