In one of my webapplication, I am trying to cache some reference entities so as to reduce database hits. I am using Caching application block to implement the caching. I am new to it, and I am not sure about its implementation. I have written a sample repository to utilize it. I want all of you to please have a look and comment on it.
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;
}
}
}
}
}
Please let me know, how can I improve it.
Please also let me know, How much data can we have in cache.
Something like this might be of use?
精彩评论