我正在尝试创建一个缓存类来缓存页面中的一些对象.目的是使用ASP.NET框架的缓存系统,但将其抽象为单独的类.似乎缓存不会持续存在.
我在这里做错了什么想法?是否有可能将对象缓存到页面本身?
编辑:添加代码:
插入缓存
Cache c = new Cache();
c.Insert(userid.ToString(), DateTime.Now.AddSeconds(length), null, DateTime.Now.AddSeconds(length), Cache.NoSlidingExpiration,CacheItemPriority.High,null);
Run Code Online (Sandbox Code Playgroud)
从缓存中获取
DateTime expDeath = (DateTime)c.Get(userid.ToString())
Run Code Online (Sandbox Code Playgroud)
即使我确实拥有密钥,我也会在c.Get上获得null.
代码与页面本身不同(页面使用它)
谢谢.
有许多方法可以在ASP.NET中存储对象
您试图存储什么类型的数据,以及您认为它必须如何保留?
就在去年年初,我写了一篇关于我一直在编写的缓存框架的博客文章,它允许我做类似的事情:
// Get the user.
public IUser GetUser(string username)
{
// Check the cache to find the appropriate user, if the user hasn't been loaded
// then call GetUserInternal to load the user and store in the cache for future requests.
return Cache<IUser>.Fetch(username, GetUserInternal);
}
// Get the actual implementation of the user.
private IUser GetUserInternal(string username)
{
return new User(username);
}
Run Code Online (Sandbox Code Playgroud)
那是近一年前的事了,从那以后它已经有所改进了,你可以阅读我的博客文章,让我知道是否有用.
| 归档时间: |
|
| 查看次数: |
3705 次 |
| 最近记录: |