我正在使用 asp.net Sqlmembership.GetAll()方法(分页重载)。现在我想添加一个缓存层来缓存结果,但是 GetAll 方法的out 参数存在问题,它返回记录总数。从缓存中检索数据时,如何为totalRecords参数赋值?
问问题
77 次
1 回答
0
如果我的理解是正确的,这个小流程将帮助您实现目标
这是一个基本流程:
- 当你想访问缓存的对象时,向缓存提供者询问
- 如果对象不为空,则转换为正确的类型并从缓存中返回对象(在本例中为用户列表)。结束进程
- 如果对象为空,则
- 从原始来源检索对象(使用
GetAll
方法) - 将检索到的对象保存到缓存中
- 返回检索到的对象。结束进程
- 从原始来源检索对象(使用
无论如何,我建议您使用自定义域类而不是MembershipUser
类
这是一个基本示例:
public IEnumerable<DomainUser> GetDomainUsers()
{
var context = HttpContext.Current;
var cache = context.Cache;
var domainUsers = cache["domainUsers"] as IEnumerable<DomainUser>;
if (domainUsers == null)
{
domainUsers = Membership.GetAllUsers().OfType<MembershipUser>().Select(x => new DomainUser
{
Email = x.Email,
Username = x.UserName
});
cache.Insert(
"domainUsers", // cache key
domainUsers, // object to cache
null, // dependencies
DateTime.Now.AddMinutes(30), // absoulute expiration
Cache.NoSlidingExpiration, // slading expiration
CacheItemPriority.High, // cache item priority
null // callback called when the cache item is removed
);
context.Trace.Warn("Data retrieved from its original source");
}
else
{
context.Trace.Warn("Data retrieved from cache");
}
return domainUsers;
}
于 2012-09-15T07:46:50.563 回答