System.Web.Caching.Cache 函数的通用包装器
我创建了一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最终使用通用扩展方法:
调用方式如下:
I ended up using generic extension methods:
Called like:
怎么样:
这当然必须在另一个类中定义。
How about:
This will of course have to be defined in another class.
我认为需要更改以下方法:
如果该项目不在缓存中,您是否不想将该项目放入缓存中以供以后使用或稍后检索。
谢谢,
I think the following method needs to be changed:
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,