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

发布于 2024-09-05 21:30:44 字数 1568 浏览 9 评论 0原文

我创建了一个使用 Cache 对象的通用包装器:

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”

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

I've created a generic wrapper for using the Cache object:

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()); }
}

The CachedKeys enumeration is just a list of keys that can be used to cache data.

The trouble is, to call it is quite convuluted:

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;

I only store one instance of each of my objects in the cache.

Therefore, is there any way that I can create an extension method that accepts the type of object to Cache and uses (via Reflection) its Name as the cache key?

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

Of course, there's two errors here:

  • Extension methods must be defined in a non-generic static class
  • The type or namespace name 'cacheType' could not be found

This is clearly not the right approach but I thought I'd show my working. Could somebody guide me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最美的太阳 2024-09-12 21:30:44

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

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>();

I ended up using generic extension methods:

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;
    }
}

Called like:

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

and..

Cache.Remove<MyObject>();
暮年慕年 2024-09-12 21:30:44

怎么样:

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

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

How about:

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

This will of course have to be defined in another class.

初见终念 2024-09-12 21:30:44

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

 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;
    }

如果该项目不在缓存中,您是否不想将该项目放入缓存中以供以后使用或稍后检索。

谢谢,

I think the following method needs to be changed:

 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;
    }

If the item is not in the cache don't you want to put the item in the cache for later use or later retrieval.

Thanks,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文