zoukankan      html  css  js  c++  java
  • vs2010缓存类

    //.net 4.0  重写缓存类(拓展)
    using System.Web.Caching;
    
    public class InMemoryOutputCacheProvider : OutputCacheProvider
    {  
        #region Members   
        private Dictionary<string, InMemoryOutputCacheItem> _cache = new Dictionary<string, InMemoryOutputCacheItem>();  
        private readonly static object _syncLock = new object();  
        #endregion   
    
        #region Methods  
        public override object Add(string key, object entry, DateTime utcExpiry) 
        {   
            Set(key, entry, utcExpiry); 
            return entry;
        }  
    
        public override object Get(string key) 
        {   
            InMemoryOutputCacheItem item = null;   
            if (_cache.TryGetValue(key, out item))   
            {    
                if (item.UtcExpiry < DateTime.UtcNow)   
                {     
                    Remove(key);    
                    return null;      
                }     
                return item.Value;   
            }    
            return null; 
        } 
    
        public override void Remove(string key) 
        {   
            InMemoryOutputCacheItem item = null;  
            if (_cache.TryGetValue(key, out item))  
            {     
                _cache.Remove(key); 
            } 
        }  
    
        public override void Set(string key, object entry, DateTime utcExpiry) 
        {  
            var item = new InMemoryOutputCacheItem(entry, utcExpiry);   
            lock (_syncLock) 
            {  
                if (_cache.ContainsKey(key))  
                {      
                    _cache[key] = item;  
                }    
                else    
                {    
                    _cache.Add(key, item); 
                }   
            } 
        }  
    
        #endregion
    }
    
    
    
    public class InMemoryOutputCacheItem
    { 
        #region Members  
     
        public DateTime UtcExpiry { get; set; }  
        public object Value { get; set; }   
    
        #endregion 
      
        #region Ctor   
    
        public InMemoryOutputCacheItem(object value, DateTime utcExpiry) 
        {   
            Value = value;   
            UtcExpiry = utcExpiry; 
        }  
    
        #endregion
    }
    
    
    

    //配置

            <caching>
                <outputCache defaultProvider="InMemory">
                    <providers>
                        <add name="InMemory" type="InMemoryOutputCacheProvider"/>
                    </providers>
                </outputCache>
            </caching>
    

    //页面

    <%@ OutputCache Duration="15" VaryByParam="*" %>
    

     

  • 相关阅读:
    OPENCV图像变换-1
    OPENCV形态学操作1
    OPENCV基本滤波算法
    OSX下编译安装opencv3.1.0与opencv_contrib_master
    iOS8学习笔记-构建多视图应用程序
    iOS8学习笔记2--autolayout
    iOS学习笔记1--在xcode6以上的版本中不使用storyboard以及部分控件使用
    Objective-c学习笔记3
    objective-c学习笔记2
    objective-c学习笔记
  • 原文地址:https://www.cnblogs.com/zengxiangzhan/p/1844871.html
Copyright © 2011-2022 走看看