Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Thursday, January 6, 2011 2:46 AM
Hi,
like Session data are stored in Web Server can you tell me where are cache data stored ?
Is that too stored in Web Server ? Where are they allocated ?
Thursday, January 6, 2011 9:39 AM ✅Answered
Means cache data will consume same memory space as Session data will consume.How can i take it granted that cached data will remain in memory as long as user's Session is active ?
Yes, cached objects and session objects take up same memory space in the process's heap. There is no guarantee that cached object will be available at any point of time. Cached objects can be dropped automatically depending on what kind of memory situation is there. Whenever you plan to use cached objects, make sure you check for nulls before using it. If its null, then recreate it, put it in cache and use it.
From MSDN:
ASP.NET can remove data from the cache for one of these reasons:
Because memory on the server is low, a process known as scavenging.
Because the item in the cache has expired.
Because the item's dependency changes.
To help you manage cached items, ASP.NET can notify your application when items are removed from the cache.
Read this: http://msdn.microsoft.com/en-us/library/ms178597.aspx
http://msdn.microsoft.com/en-us/library/aa478965.aspx
Friday, January 7, 2011 4:40 AM ✅Answered
Here is how you can implement code, this code will always get value either from cache or from database :-
public LookupEntity[] GetValue()
{
string cacheKey = "SomeKey";
//Check that record Exist In Cache or not
if (HttpRuntime.Cache[cacheKey] == null)
{
//If not then load Record from Database or any other location
var records = LookupEntity.GetRecord();
HttpRuntime.Cache.Insert(cacheKey,
records,//Records
null,//No Dependency
Cache.NoAbsoluteExpiration,//No Absolute Expiration
TimeSpan.FromMinutes(30));//Expire in 30 mins
}
return HttpRuntime.Cache[cacheKey] as LookupEntity[];
}
Thursday, January 6, 2011 3:11 AM
Hi
Yes, by default the session data and cache data is stored in the Ram of the Web Server.
Thursday, January 6, 2011 3:14 AM
Yes, it is stored on Web Server in Application Memory.
Cheers
Thursday, January 6, 2011 6:40 AM
Hi
Yes, by default the session data and cache data is stored in the Ram of the Web Server.
Means cache data will consume same memory space as Session data will consume.How can i take it granted that cached data will remain in memory as long as user's Session is active ?
Thursday, January 6, 2011 7:00 AM
cached data will remain in memory as long as user's Session is active ?
Are you looking for mechanism to map the user's session and cached data? if so, we had implemented it by keeping and checking a flag status whether to pick the data from cache or hit the databse to get it retrieved. The consistency of both the cache data with session data was the logic no built-in mechanism is available with ASP.NET. So data residing in memory can be refreshed (cache data) with some specific interval of time. The sample or any code of the implementation is not available.
Please inform in case you have any difference of opinion.
Thanks
Thursday, January 6, 2011 10:16 PM
One thing is quite surprising.I have created a cache object like this:
Cache("procID") = postbackControl.ProcID
But why the Cache object is expired very soon.I am able to retrive stored value for few seconds only after that it is expired.How to handle this situation ?
Friday, January 7, 2011 7:15 AM
Hi,
I don't want to reinvent the wheel, so look at this:
http://msdn.microsoft.com/en-us/library/aa478965.aspx
Particularly, take a look at 'Caching API, Using the Cache Object' section
Cheers