C# System.Web.Caching.Cache函数的通用包装器

C# System.Web.Caching.Cache函数的通用包装器,c#,asp.net,generics,caching,extension-methods,C#,Asp.net,Generics,Caching,Extension Methods,我已经为使用缓存对象创建了一个通用包装器: 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 voi

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

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()); }
}
当然,这里有两个错误:

  • 扩展方法必须在非泛型静态类中定义
  • 找不到类型或命名空间名称“cacheType”
这显然不是正确的方法,但我认为我应该展示我的工作能力。有人能给我指引正确的方向吗?

怎么样:

public static Cache<T> GetCache<T>(this Cache cache)
{
    return new Cache<T>(cache, typeof(T).Name);
}
公共静态缓存GetCache(此缓存)
{
返回新缓存(缓存,typeof(T).Name);
}

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

我最终使用了通用扩展方法:

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;
    }
}
公共静态类缓存扩展
{
公共静态void Remove(此缓存),其中T:class
{
cache.Remove(typeof(T).Name);
}
公共静态void AddToCache(此缓存,对象项),其中T:class
{
T outItem=null;
if(cache.TryGetItemFromCache(out-outItem))
抛出新ArgumentException(“此项已在缓存中”);
cache.Insert(typeof(T).Name,
项目,,
无效的
DateTime.Now.AddMinutes(5),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Normal,
无效);
}
公共静态bool TryGetItemFromCache(此缓存,out T项),其中T:class
{
item=cache.Get(typeof(T).Name)作为T;
退货项目!=空;
}
}
被称为:

MyObject myObject = null;
if(!Cache.TryGetItemFromCache(out myObject)){
     //get data
     Cache.AddToCache<MyObject>(data);
}

and..

Cache.Remove<MyObject>();
MyObject MyObject=null;
如果(!Cache.TryGetItemFromCache(out myObject)){
//获取数据
Cache.AddToCache(数据);
}
和。。
Cache.Remove();

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

 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;
    }
公共静态bool TryGetItemFromCache(此缓存,out T项),其中T:class
{
item=cache.Get(typeof(T).Name)作为T;
退货项目!=空;
}
如果该项不在缓存中,是否希望将该项放入缓存以供以后使用或检索


谢谢,

这基本上就是我在
缓存中已经拥有的
构造函数(除了使用
Name
属性作为键,这是一个事后思考的问题)。但是它做了你想要的,不是吗?是的,我没有想到简单地创建一个泛型方法而不是泛型类。现在,我只是简单地添加了通用扩展方法来实现功能。这将在调用代码中处理。
缓存
不知道数据来自何处。此方法是线程安全的吗?否-可以引入双重检查锁定(即使不能保证),但要惰性地填充缓存,我建议任何锁定都会引入不必要的延迟。您可以在运行计时器的后台线程上填充缓存(如果您对从线程池中取出一个线程来完成此操作感到满意的话)。不过我并不担心——我不认为懒洋洋地填充缓存需要线程安全。
MyObject myObject = null;
if(!Cache.TryGetItemFromCache(out myObject)){
     //get data
     Cache.AddToCache<MyObject>(data);
}

and..

Cache.Remove<MyObject>();
 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;
    }