再努力一点点

没有烟抽的日子
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

缓存到上下文中

Posted on 2010-06-19 11:29  ZhangPeng.Chen  阅读(399)  评论(0)    收藏  举报
很多时候我们需要将一些东西缓存到上下文中,比如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>();
            }
        }
    }

这种方式扩展性很好。