zoukankan      html  css  js  c++  java
  • 通用缓存类

    1.接口

    public interface ICacheProvider
        {
            /// <summary>
            /// 获取缓存.
            /// </summary>
            /// <param name="key">The key.</param>
            /// <returns></returns>
            T Get<T>(string key);
    
            /// <summary>
            /// 设置缓存.
            /// </summary>
            /// <param name="key">The key.</param>
            /// <param name="data">The data.</param>
            /// <param name="seconds">The seconds.</param>
            void Set<T>(string key, T data, int seconds);
    
            /// <summary>
            /// 是否已经设置缓存
            /// </summary>
            /// <param name="key">key</param>
            /// <returns>Result</returns>
            bool IsSet(string key);
    
            /// <summary>
            /// 删除缓存.
            /// </summary>
            /// <param name="key">The key.</param>
            void Remove(string key);
    
            /// <summary>
            /// 根据键值对的模式删除缓存.
            /// </summary>
            /// <param name="pattern">The pattern.</param>
            void RemoveByPattern(string pattern);
    
            /// <summary>
            /// 清空缓存.
            /// </summary>
            void Clear();
        }

    2.MemeroyCache

    public class MemoryCacheProvider : ICacheProvider
        {
            /// <summary>
            /// 获取缓存.
            /// </summary>
            /// <param name="key">缓存key.</param>
            /// <returns></returns>
            public virtual T Get<T>(string key)
            {
                return (T)MemoryCache.Default.Get(key);
            }
    
            /// <summary>
            /// 获取缓存.
            /// </summary>
            /// <typeparam name="T">类型</typeparam>
            /// <param name="key">缓存Key</param>
            /// <param name="second">缓存时间</param>
            /// <param name="func">当取不到缓存时,设置缓存的方法</param>
            /// <returns></returns>
            public virtual T Get<T>(string key, int second, Func<T> func)
            {
                if (key == null)
                {
                    throw new ArgumentNullException("参数key不能为空");
                }
    
                var setted = IsSet(key);
                if (setted)
                {
                    return Get<T>(key);
                }
    
                var result = func();
                if (result != null)
                {
                    Set(key, result, second);
                }
    
                return (T)result;
            }
    
            /// <summary>
            /// 
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="key"></param>
            /// <param name="func"></param>
            /// <returns></returns>
            public virtual T Get<T>(string key, Func<T> func)
            {
                return Get<T>(key, 60, func);
            }
    
            /// <summary>
            /// 设置缓存.
            /// </summary>
            /// <param name="key">The key.</param>
            /// <param name="data">The data.</param>
            /// <param name="seconds">The seconds.</param>
            public virtual void Set<T>(string key, T data, int seconds)
            {
                if (key == null)
                {
                    throw new ArgumentNullException("参数key不能为空");
                }
    
                if (data != null)
                {
                    throw new ArgumentNullException("参数data不能为空");
                }
    
                var policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromSeconds(seconds);
                MemoryCache.Default.Add(new CacheItem(key, data), policy);
            }
    
            /// <summary>
            /// 是否已经设置缓存
            /// </summary>
            /// <param name="key">key</param>
            /// <returns>
            /// Result
            /// </returns>
            public bool IsSet(string key)
            {
                return MemoryCache.Default.Contains(key);
            }
    
            /// <summary>
            /// 删除缓存.
            /// </summary>
            /// <param name="key">The key.</param>
            public virtual void Remove(string key)
            {
                MemoryCache.Default.Remove(key);
            }
    
            /// <summary>
            /// 根据键值对的模式删除缓存.
            /// </summary>
            /// <param name="pattern">The pattern.</param>
            public virtual void RemoveByPattern(string pattern)
            {
                foreach (string key in GetCacheKeysByPattern(pattern))
                {
                    this.Remove(key);
                }
            }
    
            /// <summary>
            /// 清空缓存.
            /// </summary>
            public virtual void Clear()
            {
                foreach (var item in MemoryCache.Default)
                {
                    this.Remove(item.Key);
                }
            }
    
            /// <summary>
            /// 根据键值对的模式获取缓存.
            /// </summary>
            /// <param name="pattern"></param>
            /// <returns></returns>
            IEnumerable<string> GetCacheKeysByPattern(string pattern)
            {
                var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
                foreach (var item in MemoryCache.Default)
                {
                    if (regex.IsMatch(item.Key))
                    {
                        yield return item.Key;
                    }
                }
            }
        }
  • 相关阅读:
    建持续集成
    Apache通用日志工具commonslogging和Log4j使用总结
    subversion(SVN)常见问题及其解决方法
    UML类图的相关名词解释
    SVN项目的目录布局
    敏捷开发流程总结
    敏捷开发实践
    卓有成效的敏捷开发流程
    字符串反转操作
    转:图片水印 http://www.cnblogs.com/xiaoge_com/archive/2009/09/10/1564216.html
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/6073828.html
Copyright © 2011-2022 走看看