H.T
H.T

Reputation: 21

Memory cache c# .net

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

Answers (3)

Bigsby
Bigsby

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

Rafael Adnet Pinho
Rafael Adnet Pinho

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:

  • Disable Idle timeouts. 20 minutes of inactivity = boom! New process on the next incoming request. Set that to zero.
  • Disable Regular time interval - the "29 hour" default has been described as "insane", "annoying" and "clever" by various parties. Actually, only two of those are true.
  • Optionally Turn on DisallowRotationOnConfigChange (above, Disable Recycling for configuration changes) if you just can't stop playing with it - this allows you to change any app pool setting without it instantly signaling to the worker processes that it needs to be killed. You need to manually recycle the App Pool to get the settings to take effect, which lets you pre-set settings and then use a change window to apply them via your recycle process.

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

Guillaume B.
Guillaume B.

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

Related Questions