Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should I use IMemoryCache when we have IDistributedCache?

Tags:

.net-core

.Net Core provides in-memory implementations for both interfaces (MemoryCache and DistributedMemoryCache) but let's assume we have a working IDistributedCache implementation for our application.

When does it make sense to still use IMemoryCache. In what scenarios is it helpful or preferred over caching data in a distributed cache?

like image 546
Sandro Avatar asked Jan 17 '20 17:01

Sandro


1 Answers

I was searching for same and found the answer in github issue:

They have fundamentally different semantics. MemoryCache can store live objects, the distributed cache can't, objects have to be serialized. The distributed cache can be off box and calls to it may fail or take a long time so getting and setting should be async, the MemoryCache is always in memory and fast. The distributed cache can be disconnected from the store so the interface should account for that.

https://github.com/aspnet/Caching/issues/220#issuecomment-241229013

like image 60
LazZiya Avatar answered Oct 06 '22 23:10

LazZiya