zoukankan      html  css  js  c++  java
  • nopCommerce_3.00-Nop.Core.Caching

    namespace Nop.Core.Caching
    {
        /// <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();
        }
    }
    View Code
    namespace Nop.Core.Caching
    {
        /// <summary>
        /// Represents a NopNullCache
        /// </summary>
        public partial class NopNullCache : 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>
            public T Get<T>(string key)
            {
                return default(T);
            }
    
            /// <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)
            {
            }
    
            /// <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 false;
            }
    
            /// <summary>
            /// Removes the value with the specified key from the cache
            /// </summary>
            /// <param name="key">/key</param>
            public void Remove(string key)
            {
            }
    
            /// <summary>
            /// Removes items by pattern
            /// </summary>
            /// <param name="pattern">pattern</param>
            public void RemoveByPattern(string pattern)
            {
            }
    
            /// <summary>
            /// Clear all cache data
            /// </summary>
            public void Clear()
            {
            }
        }
    }
    View Code
    using System;
    using System.Collections.Generic;
    using System.Runtime.Caching;
    using System.Text.RegularExpressions;
    
    namespace Nop.Core.Caching
    {
        /// <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);
            }
        }
    }
    View Code
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    using System.Web;
    
    namespace Nop.Core.Caching
    {
        /// <summary>
        /// Represents a NopStaticCache
        /// </summary>
        public partial class PerRequestCacheManager : ICacheManager
        {
            private readonly HttpContextBase _context;
    
            /// <summary>
            /// Ctor
            /// </summary>
            /// <param name="context">Context</param>
            public PerRequestCacheManager(HttpContextBase context)
            {
                this._context = context;
            }
            
            /// <summary>
            /// Creates a new instance of the NopRequestCache class
            /// </summary>
            protected IDictionary GetItems()
            {
                if (_context != null)
                    return _context.Items;
    
                return null;
            }
    
            /// <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)
            {
                var items = GetItems();
                if (items == null)
                    return default(T);
    
                return (T)items[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)
            {
                var items = GetItems();
                if (items == null)
                    return;
    
                if (data != null)
                {
                    if (items.Contains(key))
                        items[key] = data;
                    else
                        items.Add(key, data);
                }
            }
    
            /// <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)
            {
                var items = GetItems();
                if (items == null)
                    return false;
                
                return (items[key] != null);
            }
    
            /// <summary>
            /// Removes the value with the specified key from the cache
            /// </summary>
            /// <param name="key">/key</param>
            public void Remove(string key)
            {
                var items = GetItems();
                if (items == null)
                    return;
    
                items.Remove(key);
            }
    
            /// <summary>
            /// Removes items by pattern
            /// </summary>
            /// <param name="pattern">pattern</param>
            public void RemoveByPattern(string pattern)
            {
                var items = GetItems();
                if (items == null)
                    return;
    
                var enumerator = items.GetEnumerator();
                var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
                var keysToRemove = new List<String>();
                while (enumerator.MoveNext())
                {
                    if (regex.IsMatch(enumerator.Key.ToString()))
                    {
                        keysToRemove.Add(enumerator.Key.ToString());
                    }
                }
    
                foreach (string key in keysToRemove)
                {
                    items.Remove(key);
                }
            }
    
            /// <summary>
            /// Clear all cache data
            /// </summary>
            public void Clear()
            {
                var items = GetItems();
                if (items == null)
                    return;
    
                var enumerator = items.GetEnumerator();
                var keysToRemove = new List<String>();
                while (enumerator.MoveNext())
                {
                    keysToRemove.Add(enumerator.Key.ToString());
                }
    
                foreach (string key in keysToRemove)
                {
                    items.Remove(key);
                }
            }
        }
    }
    View Code
    using System;
    
    namespace Nop.Core.Caching
    {
        /// <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;
                }
            }
        }
    }
    View Code

    1.C# Default使用

    二、插件

    http://www.cnblogs.com/haoxinyue/archive/2013/06/06/3105541.html

    1.职业成功的85%取决于职业心态,15%取决于职业技能 2.不只是上级要你去做或等待别人要你去做 3.营造积极友善的工作氛围,哪怕吹点善意的牛
  • 相关阅读:
    自定义类型中结构体、枚举等,结构体内存对齐相关要点总结
    可变参数模拟printf()函数实现一个my_print()函数以及调用可变参数需注意的陷阱
    一些关于memcpy memmove函数的区别,和模拟实现
    浅谈 数组指针 指针数组 函数指针 函数指针数组... 以及它们之间区别。
    C语 三子棋小游戏
    5位运动员参加了10米台跳水比赛,有人让他们预测比赛结果
    stm32移植ds18b20时出现的问题
    自己实现strcmp
    C语言新手写扫雷攻略3
    C语言新手写扫雷攻略2
  • 原文地址:https://www.cnblogs.com/i-blog/p/3546428.html
Copyright © 2011-2022 走看看