moozzyk / EFCache

Second Level Cache for Entity Framework 6.1

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory usage?

petermorlion opened this issue · comments

I'm looking at CachingPolicy and wonder if it could lead to excessive memory usage. You can specify how long something may be cached, and for a given command definition how many rows can be cached. But you can't specify how large the cache may become. So given enough different commands in a short enough timeframe, this could lead to an out of memory exception? Or am I misunderstanding how EFCache works?

If you are using the built-in memory cache then you are right - it can lead to excessive memory usage. You don't even have to send commands in a short time frame - stale entries are only detected when cached data for a key is requested (you can Purge the cache manually but it's specific to MemoryCache - typically you have a separate thread that is responsible for removing stale entries) so memory usage will grow.
In my opinion CachingPolicy is not part of the cache hence it does not know specifics of cache (including the size of the cache) - e.g. if you cached data in SqlServer how would/should cache policy know the size of the "cache"?
The built-in memory cache is very simple. You can replace it with your own implementation that has a more sophisticated eviction algorithm, contains a thread that periodically checks and removes stale entries, do not cache results if cache gets to big (or remove older entries to make room) etc.
I also don't recommend caching results for all queries/tables - you should pick tables whose results you want to cache rather than all tables.

Thanks for you answer. I've now made my own implementation of ICache, based on InMemoryCache, but using .NET's MemoryCache internally, because it allows me to set a maximum size.