Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

MemoryDistributedCache should prefix keys to avoid conflicts with IMemoryCache #176

Closed
rmja opened this issue Apr 7, 2016 · 10 comments
Closed
Assignees
Milestone

Comments

@rmja
Copy link

rmja commented Apr 7, 2016

I am having a scenario where I first look in a local IMemoryCache for a hit, and if not found, go to a distribted IDistributedCache. If the key is found in the distributed cache, I store is locally for a short time in the IMemoryCache.
This works perfectly when IDistributedCache is e.g. a Redis cache, but when the implementation is MemoryDistributedCache, then the keyspace for the two caches is the same. This results in the first lookup in IMemoryCache to return the entry expected to be returned in IDistributedCache.

Would it be possible to e.g. prefix all keys in MemoryDistributedCache with something like "distributed" so that the two caches behaves like they are separate.

@Tratcher
Copy link
Member

Tratcher commented Apr 7, 2016

Is the data stored the same, or are they in different formats due to serialization requirements? If it's the same data this seems like a harmless side-effect for local scenarios.

You could just as easily apply the prefixes in your code.

One other mitigation would be to ensure they are using different instances of IMemoryCache. You can adjust the DI registrations for this.

@davidfowl
Copy link
Member

I think this is a reasonable request.

@Tratcher
Copy link
Member

Tratcher commented Apr 7, 2016

Note just appending "distributed." to each key only lowers the odds of a collision. You'd need to use a unique identifier per instance of MemoryDistributedCache.

@rmja
Copy link
Author

rmja commented Apr 7, 2016

@Tratcher that is the problem. The local one is having the instance to cache directly, where the distributed one has a serialized version. So when finding the cache hit I get invalid cast exception.

@rmja
Copy link
Author

rmja commented Apr 7, 2016

@Tratcher well that depends. That would only cause a collision if my key already ended with "distributed", where when there is no pre/suffix, then I have collision in 100% of the cases.

And it seems wrong from a user perspective that the local and distributed cache must not use the same key.

@Ulriksen
Copy link

Ulriksen commented Apr 8, 2016

The key must be unique, having a possiblity for collision between local and distributed keys will create hard to find bugs.

Having the same key space for both local and distributed cache seems like a bad idea.

@muratg muratg added this to the 1.0.0 milestone Apr 21, 2016
@sebastienros
Copy link
Member

Could the default implementation for IDistributedCache have it's own local cache storage instead of resolve the registered one? I assume we don't really care about have it use the same concrete registered implementation of IMemoryCache. The contract today is to have it work locally, not that it has to use the DI one.

@muratg
Copy link

muratg commented May 26, 2016

@sebastienros Do you have time to look into this one in 1.0.0?

@sebastienros
Copy link
Member

Yes, is there an agreement on my proposal? Or should I engage more devs?

@Tratcher
Copy link
Member

@sebastienros give this workaround a try:
services.AddSingleton<IDistributedCache>(new MemoryDistributedCache(new MemoryCache())

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants