开发者

Caching at repository layer in Asp.net application using Caching Application Block

开发者 https://www.devze.com 2023-04-10 06:29 出处:网络
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 sur

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号