zoukankan      html  css  js  c++  java
  • Cache缓存

    {
        /// <summary>
        /// Cache manager interface
        /// </summary>
        public interface ICacheManager
        {
            /// <summary>
            /// Gets or sets the value associated with the specified key.
            /// </summary>
            /// <typeparam name="T">Type</typeparam>
            /// <param name="key">The key of the value to get.</param>
            /// <returns>The value associated with the specified key.</returns>
            T Get<T>(string key);
    
            /// <summary>
            /// Adds the specified key and object to the cache.
            /// </summary>
            /// <param name="key">key</param>
            /// <param name="data">Data</param>
            /// <param name="cacheTime">Cache time</param>
            void Set(string key, object data, int cacheTime);
    
            /// <summary>
            /// Gets a value indicating whether the value associated with the specified key is cached
            /// </summary>
            /// <param name="key">key</param>
            /// <returns>Result</returns>
            bool IsSet(string key);
    
            /// <summary>
            /// Removes the value with the specified key from the cache
            /// </summary>
            /// <param name="key">/key</param>
            void Remove(string key);
    
            /// <summary>
            /// Removes items by pattern
            /// </summary>
            /// <param name="pattern">pattern</param>
            void RemoveByPattern(string pattern);
    
            /// <summary>
            /// Clear all cache data
            /// </summary>
            void Clear();
        }
    缓存接口类:
    /// <summary>
        /// Represents a MemoryCacheCache
        /// </summary>
        public partial class MemoryCacheManager : ICacheManager
        {
            protected ObjectCache Cache
            {
                get
                {
                    return MemoryCache.Default;
                }
            }
            
            /// <summary>
            /// Gets or sets the value associated with the specified key.
            /// </summary>
            /// <typeparam name="T">Type</typeparam>
            /// <param name="key">The key of the value to get.</param>
            /// <returns>The value associated with the specified key.</returns>
            public T Get<T>(string key)
            {
                return (T)Cache[key];
            }
    
            /// <summary>
            /// Adds the specified key and object to the cache.
            /// </summary>
            /// <param name="key">key</param>
            /// <param name="data">Data</param>
            /// <param name="cacheTime">Cache time</param>
            public void Set(string key, object data, int cacheTime)
            {
                if (data == null)
                    return;
    
                var policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);
                Cache.Add(new CacheItem(key, data), policy);
            }
    
            /// <summary>
            /// Gets a value indicating whether the value associated with the specified key is cached
            /// </summary>
            /// <param name="key">key</param>
            /// <returns>Result</returns>
            public bool IsSet(string key)
            {
                return (Cache.Contains(key));
            }
    
            /// <summary>
            /// Removes the value with the specified key from the cache
            /// </summary>
            /// <param name="key">/key</param>
            public void Remove(string key)
            {
                Cache.Remove(key);
            }
    
            /// <summary>
            /// Removes items by pattern
            /// </summary>
            /// <param name="pattern">pattern</param>
            public void RemoveByPattern(string pattern)
            {
                var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
                var keysToRemove = new List<String>();
    
                foreach (var item in Cache)
                    if (regex.IsMatch(item.Key))
                        keysToRemove.Add(item.Key);
    
                foreach (string key in keysToRemove)
                {
                    Remove(key);
                }
            }
    
            /// <summary>
            /// Clear all cache data
            /// </summary>
            public void Clear()
            {
                foreach (var item in Cache)
                    Remove(item.Key);
            }
        }
    实现缓存接口
    /// <summary>
        /// Extensions
        /// </summary>
        public static class CacheExtensions
        {
            public static T Get<T>(this ICacheManager cacheManager, string key, Func<T> acquire)
            {
                return Get(cacheManager, key, 60, acquire);
            }
    
            public static T Get<T>(this ICacheManager cacheManager, string key, int cacheTime, Func<T> acquire) 
            {
                if (cacheManager.IsSet(key))
                {
                    return cacheManager.Get<T>(key);
                }
                else
                {
                    var result = acquire();
                    //if (result != null)
                        cacheManager.Set(key, result, cacheTime);
                    return result;
                }
            }
        }
    扩展缓存
    /// <summary>
            /// 获取XX列表
            /// </summary>
            /// <param name="prjTypes"></param>
            /// <returns></returns>
            private List<xxxxPrjModel> GetxxxxProject()
            {
                var resultList = new List<xxxxPrjModel>();
    
                //加入5分钟缓存
                resultList = _cacheManager.Get("xxxx_PROJECTLIST", 5, () =>
                {
                    return prjList.ToList();
                });
                return resultList;
            }
    缓存调用
  • 相关阅读:
    python的argparse模块
    Robotframework之SSHLibrary库
    Python 中的 getopt 模块
    Python list 列表
    Samba windows 10 share: mount error(112): Host is down
    安装两个版本的python安装包,后安装的python程序打开时闪退
    NetScaler VPX configration
    drupal smtp could not connect to smtp
    drupal7 判断用户是否具有某个权限
    微信支付报错:time_expire时间过短,刷卡至少1分钟,其他5分钟]
  • 原文地址:https://www.cnblogs.com/ingstyle/p/6655901.html
Copyright © 2011-2022 走看看