privatestaticreadonlyobjectCacheLockObject=newobje
ct();
publicIEnumerable<Faq>GetAllFaqs(intcount,inthours,
boolreload=false)
{
conststringcacheKey="CachedFaqRepository:GetAll";
}
}
}
As you can see, I'm just making a call to pull all of the FAQs since I don't have
that many records in the table. So our cacheKey is the name of the class and
the name of the method, just in case I need to view the cache at a later time.
Next, we need to perform the old"double-check locking"to make sure we don't
receive any race conditions and we can actually retrieve the value. Once we
have the value, we can cache it and return it safely.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingThinController.Models;
namespaceThinController.Repository
{
publicclassCachedFaqRepository:FaqRepository
{
privatestaticreadonlyobjectCacheLockObject=newobje
ct();
publicIEnumerable<Faq>GetAllFaqs(intcount,inthours,
boolreload=false)
{
conststringcacheKey="CachedFaqRepository:GetAll";
//Retrievethevaluefromthecache,seeifit'sthe
re.
varresult=HttpRuntime.Cache[cacheKey]asList<Faq>
;
//Ifit'snotinthecacheORweforceareload...
if(result==null||reload)
{
//...thenperformalock.
lock(CacheLockObject)
评论0
最新资源