代码之家  ›  专栏  ›  技术社区  ›  David Neale

system.web.caching.cache函数的通用包装

  •  3
  • David Neale  · 技术社区  · 14 年前

    我已经为使用缓存对象创建了一个通用包装器:

    public class Cache<T> where T : class
    {
        public Cache Cache {get;set;}
        public CachedKeys Key {get;set;}
    
        public Cache(Cache cache, CachedKeys key){
            Cache = cache;
            Key = key;
        }
    
        public void AddToCache(T obj){
            Cache.Add(Key.ToString(),
                obj,
                null,
                DateTime.Now.AddMinutes(5),
                System.Web.Caching.Cache.NoSlidingExpiration,
                System.Web.Caching.CacheItemPriority.Normal,
                null);                   
        }
    
        public bool TryGetFromCache(out T cachedData) {
            cachedData = Cache[Key.ToString()] as T;
            return cachedData != null;
        }
    
        public void RemoveFromCache() {
            Cache.Remove(Key.ToString()); }
    }
    

    cachedkeys枚举只是可用于缓存数据的键列表。

    问题是,可以说,这是相当令人信服的:

    var cache = new Cache<MyObject>(Page.Cache, CachedKeys.MyKey);
    MyObject myObject = null;
    
    if(!cache.TryGetFromCache(out myObject)){
        //get data...
        cache.AddToCache(data); //add to cache
        return data;
    }
    
    return myObject;
    

    我只在缓存中存储每个对象的一个实例。

    因此,是否有任何方法可以创建一个扩展方法,它接受要缓存的对象类型并使用(通过反射)它 Name 作为缓存键?

    public static Cache<T> GetCache(this Cache cache, Type cacheType){
            Cache<cacheType> Cache = new Cache<cacheType>(cache, cacheType.Name);
        }
    

    当然,这里有两个错误:

    • 扩展方法必须在非泛型静态类中定义
    • 找不到类型或命名空间名称“cachetype”

    这显然不是正确的方法,但我想我会展示我的工作。有人能指引我往正确的方向走吗?

    3 回复  |  直到 14 年前
        1
  •  5
  •   David Neale    14 年前

    我最终使用了一般的扩展方法:

    public static class CacheExtensions
    {
        public static void Remove<T>(this Cache cache) where T : class
        {
            cache.Remove(typeof(T).Name);
        }
    
        public static void AddToCache<T>(this Cache cache, object item) where T : class
        {
            T outItem = null;
            if (cache.TryGetItemFromCache<T>(out outItem))
                throw new ArgumentException("This item is already in the cache");
    
            cache.Insert(typeof(T).Name,
                    item,
                    null,
                    DateTime.Now.AddMinutes(5),
                    System.Web.Caching.Cache.NoSlidingExpiration,
                    System.Web.Caching.CacheItemPriority.Normal,
                    null);
        }
    
        public static bool TryGetItemFromCache<T>(this Cache cache, out T item) where T : class
        {
             item = cache.Get(typeof(T).Name) as T;
             return item != null;
        }
    }
    

    叫作:

    MyObject myObject = null;
    if(!Cache.TryGetItemFromCache(out myObject)){
         //get data
         Cache.AddToCache<MyObject>(data);
    }
    
    and..
    
    Cache.Remove<MyObject>();
    
        2
  •  1
  •   ErikHeemskerk    14 年前

    怎么样:

    public static Cache<T> GetCache<T>(this Cache cache)
    {
        return new Cache<T>(cache, typeof(T).Name);
    }
    

    当然,这必须在另一个类中定义。

        3
  •  0
  •   azamsharp    14 年前

    我认为需要改变以下方法:

     public static bool TryGetItemFromCache<T>(this Cache cache, out T item) where T : class
        {
             item = cache.Get(typeof(T).Name) as T;
             return item != null;
        }
    

    如果该项不在缓存中,则不希望将该项放在缓存中以供以后使用或以后检索。

    谢谢,