-
Notifications
You must be signed in to change notification settings - Fork 216
MemoryDistributedCache should prefix keys to avoid conflicts with IMemoryCache #176
Comments
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. |
I think this is a reasonable request. |
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. |
@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. |
@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. |
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. |
Could the default implementation for |
@sebastienros Do you have time to look into this one in 1.0.0? |
Yes, is there an agreement on my proposal? Or should I engage more devs? |
@sebastienros give this workaround a try: |
I am having a scenario where I first look in a local
IMemoryCache
for a hit, and if not found, go to a distribtedIDistributedCache
. If the key is found in the distributed cache, I store is locally for a short time in theIMemoryCache
.This works perfectly when
IDistributedCache
is e.g. a Redis cache, but when the implementation isMemoryDistributedCache
, then the keyspace for the two caches is the same. This results in the first lookup inIMemoryCache
to return the entry expected to be returned inIDistributedCache
.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.The text was updated successfully, but these errors were encountered: