缓存到上下文中

很多时候我们需要将一些东西缓存到上下文中,比如Linq to sql的DataContext, EntityFramework的ObjectContext等等。
当然我们还应该适应非Http的请求。

1. CommonContext
    CommonContext主要用来当成缓存上下文的基类。

     public   class  CommonContext
    { }

2. ContextCache
  主要维护一个字典 IDictionary < Type, IDictionary < string , CommonContext >>  _cache,这个字典的Key是缓存上下文的类型,Value是一个 IDictionary < string , CommonContext >,也就是一个缓存上下文可以拥有多个实例,当然你没有指定的话,默认是一个。
代码
     public   class  ContextCache
    {
        
#region  Initialize

        
private  ContextCache()
        { }

        
private   static  ContextCache _current;
        
public   static  ContextCache Current
        {
            
get
            {
                
if  (HttpContext.Current  !=   null )
                {
                    ContextCache context 
=  (ContextCache)HttpContext.Current.Items[ " __ContextCache__ " ];
                    
if  (context  ==   null )
                    {
                        context 
=   new  ContextCache();
                        HttpContext.Current.Items[
" __ContextCache__ " =  context;
                    }

                    
return  context;
                }
                
else   if  (_current  ==   null )
                {
                    _current 
=   new  ContextCache();
                }

                
return  _current;
            }
        }

        
#endregion

        
private  IDictionary < Type, IDictionary < string , CommonContext >>  _cache  =
                       new  Dictionary < Type, IDictionary < string , CommonContext >> ();

        
public  CommonContext  this [Type contextType,  string  contextName]
        {
            
get
            {
                
if  (_cache.ContainsKey(contextType))
                {
                    IDictionary
< string , CommonContext >  cacheItem  =  _cache[contextType];
                    
if  (cacheItem.ContainsKey(contextName))
                    {
                        
return  cacheItem[contextName];
                    }
                }

                
return   null ;
            }
        }

        
public   void  Add(Type contextType,  string  contextName, CommonContext context)
        {
            
if  ( ! _cache.ContainsKey(contextType))
            {
                _cache.Add(contextType, 
new  Dictionary < string , CommonContext > ());
            }

            IDictionary
< string , CommonContext >  cacheItem  =  _cache[contextType];
            
if  ( ! cacheItem.ContainsKey(contextName))
            {
                cacheItem.Add(contextName, context);
            }
        }

        
public   void  Remove(Type contextType,  string  contextName, CommonContext context)
        {
            
if  (_cache.ContainsKey(contextType))
            {
                IDictionary
< string , CommonContext >  cacheItem  =  _cache[contextType];
                
if  (cacheItem.ContainsKey(contextName))
                {
                    cacheItem.Remove(contextName);
                }
            }
        }

        
public  T GetContext < T > ()  where  T : CommonContext
        {
            
return  GetContext < T > ( null );
        }

        
public  T GetContext < T > (Func < T >  create)  where  T : CommonContext
        {
            
return  GetContext < T > ( " DefaultContextName " , create);
        }

        
public  T GetContext < T > ( string  contextName, Func < T >  create)  where  T : CommonContext
        {
            Type contextType 
=   typeof (T);
            T context 
=   this [contextType, contextName]  as  T;
            
if  (context  ==   null )
            {
                
if  (create  !=   null )
                    context 
=  create();
                
else
                    context 
=  Activator.CreateInstance < T > ();

                Add(contextType, contextName, context);
            }

            
return  context;
        }
    }


3. MyContext

     public   class MyContext : CommonContext
    {
        
public   static MyContext Current
        {
            
get
            {
                
return  ContextCache.Current.GetContext < MyContext > ();
            }
        }
    }

这种方式扩展性很好。

你可能感兴趣的:(缓存)