【发布时间】:2011-11-29 10:35:09
【问题描述】:
在我的一个 web 应用程序中,我试图缓存一些引用实体以减少数据库命中。我正在使用缓存应用程序块来实现缓存。我是新手,我不确定它的实现。我已经编写了一个示例存储库来使用它。 我希望你们所有人都可以看看并评论它。
public class StatusRepository
{
ICacheManager statusCahce = CacheFactory.GetCacheManager(); //I am not sure whether I should initilise statusCache object here.
public StatusEntity Get(byte statusId)
{
StatusCollection statusCollection = (StatusCollection)statusCahce.GetData("Statuses");
if (statusCollection != null)
{
return (StatusEntity)statusCollection.StatusList.First(p=>p.StatusId==statusId);
}
else
{
// HIT Database to get the entity
}
}
public StatusCollection GetStatusList()
{
StatusCollection statusCollection = (StatusCollection)statusCahce.GetData("Statuses");
SqlHelper sql = new SqlHelper(true);
if (statusCollection != null) //We have it in cache
{
return statusCollection;
}
else //Hit Database
{
//Hit Database to get the StatusCollection and add that collection to cache
statusCahce.Add("Statuses", statusCollection);
return statusCollection;
}
}
}
}
}
请告诉我,我该如何改进它。
还请告诉我,我们可以在缓存中保存多少数据。
【问题讨论】:
-
根据 MSDN CacheFactory 不再使用。您为什么不描述您真正想要实现的目标,以便人们可以建议替代技术或方法。
标签: c# .net asp.net caching enterprise-library