Reputation: 5044
I have been scratching my head for this from last one week, i will really appreciate any help here, so here we go:
I have a website running on Dnn 7.4 and i want to add caching functionality on it. On first attempt it reads from database and on second attempt onwards it should read from cache, the problem is on first attempt it reads as expected, on 2nd attempt it reads from cache also, but then again on 3rd attempt it fetches from database. Here's my simple code:
if (Cache["Something"] != null)
{
Response.Write("Cache" + Cache["Something"]);
}
else
{
Cache["Something"] = "Cool";
Response.Write("Latest" + Cache["Something"]);
}
DataCache implementation:
using DotNetNuke.Common.Utilities;
string dCache = DataCache.GetCache("test") as string;
if (String.IsNullOrEmpty(dCache))
{
DataCache.SetCache("test", "cache content");
Response.Write("From Database: cache content");
}
else
{
Response.Write("From Cache: " + dCache);
}
Upvotes: 0
Views: 104
Reputation: 529
I suggest to use DataCache from DotNetNuke.Common.Utilities
//Get Cache
string str = DataCache.GetCache("something") as string;
//Remove cache
DataCache.RemoveCache("something");
// Set Cache
DataCache.SetCache("something", "value of something");
Upvotes: 0
Reputation: 27944
You do not show what kind of cache you are using and your policies of the cached items. Here a sample on how it might work with a MemoryCache:
ObjectCache cache = MemoryCache.Default;
string something = cache["Something"] as string;
if (something == null){
CacheItemPolicy policy = new CacheItemPolicy();
//Configure your expire policy here
cache.Set("Something", "Cool", policy);
something = "Cool";
}
return something;
Now you only have to configure the policy for the cache item handling.
Upvotes: 1