Reputation: 21
I am using the memory cache reserve data in memory about 120 GB I know its too large but its the best practice for my project the issue is I am using the memory cache object as the following
I created static object for the cache memory
var collection = new System.Collections.Specialized.NameValueCollection();
collection.Add("CacheMemoryLimitMegaBytes", "155000");
//var memoryCashe = new MemoryCache("Images", collection);
MemoryCachingProvider.cache = new MemoryCache("Images", collection);
//Here how I add new item
CacheItemPolicy policy = new CacheItemPolicy();
policy.Priority = CacheItemPriority.NotRemovable;
cache.Add(key, value, policy);
but the memory became empty after period of time if I didn't use it.
I am building web API application
and I have enough space in the RAM about 170 GB
any suggestions
Upvotes: 0
Views: 2219
Reputation: 952
Caches are temporary by nature. If IIS spins down the app pool, then all such resources will get recycled. You will need to ensure the app pool never recycles (almost impossible) or need to move this cache to an external, durable resource such as redis.
Upvotes: 1
Reputation: 503
As mentioned by others, this is probably happening because of the IIS server Recycling. "Turning off" the recycle can be a turnaround solution to your problem and will work as an emergency solution. But if I were in your pants, after applying this turnaround, I would try different ways to use the memory cache without disabling the recycling process.
Recycling
Recycling is usually* where IIS starts up a new process as a container for your application, and then gives the old one up to ShutdownTimeLimit to go away of its own volition before it's killed.
*- usually: see DisallowOverlappingRotation / "Disable overlapped recycle" setting
It is destructive, in that the original process and all its state information are discarded (including your precious memory cache).
What To Do:
Generally:
That's enough to cause a well-behaved process to live forever. If it dies, sure, it'll be replaced. If it hangs, pinging should pick that up and a new one should start within 2 minutes (by default; worst-case calc should be: up to ping frequency + ping timeout + startup time limit before requests start working again).
Sugestion:
Using out-of-process session state (eg, State Server or a database, or even a cookie if your state is tiny) can allow you to work around this without disabling the recycle.
Hope it helps!
Upvotes: 2
Reputation: 64
Usually if it's "after period of time", it means you probably have a SlidingExpiration value other than "NoSlidingExpiration".
Other than that, it could be that your application pool is recycling at specific interval, and that gives you the impression that the cache is "emptied".
Upvotes: 0