-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] MemoryCache with cloned objects on Get, atomic update of cache #262
Comments
I'm interested in that first item as well (having the MemoryCache items be cloned, but not the DistributedCache ones) I see the same request here: #194 That used the same idea you suggested (custom IMemoryCache with cloning) but I actually don't think that's a good idea with the implementation shown, because what FusionCache stores in the IMemoryCache is not just the object you return but a wrapping FusionCacheMemoryEntry, and the Metadata field (and possibly others) on that wrapper are modified by FusionCache without re-setting them in the MemoryCache, e.g. here: If our custom MemoryCache clones all returned values, then that I think we could get around this by having a custom handling for It seems like ideally this would be something built into FusionCache. I think it would make sense to re-use the existing E.g. a new method on
And if that serializer is set, whenever a value is going to be returned from the MemoryCache by FusionCache (calls to
|
Hi @kzkzg and thanks for using FusionCache.
Something important to keep in mind is that when you get something from a cache you cannot modify it:
Something I always suggest and that served me well for decades is to be explicit when caching is involved.
This is exactly the solution another FusionCache user used, see here.
Somebody else asked me for the auto-clone feature, but I never actually planne for it, for a couple of reasons. First, knowing it will create a clone for every single get operation gives me shivers 😅. Jokes aside, since it would be an opt-in, I may accept that. Second, how would the clone be done? Any opinions? I'm open to ideas.
No, not currently, but I'm open to ideas. One thing to notice is that I'm already playing with a native redlock-like feature, to allow for cache stampede protection in a multi-node environment. The thing gets pretty complicated pretty fast when you throw in expected features (at least for FusionCache users) like Soft/Hard Timeouts, Auto-Recovery and so on because I need to be able to clearly differentiate between waiting for a distributed lock because another node is executing something, and waiting for a distributed lock because Redis is down or super slow right now... I have an experimental branch with support for an Hope this helps, let me know! |
Hi @DanielStout5 read my answer to @kzkzg and let me know what you think, I'm interested!
Ah, dang it, you may be right 🤔
Let me think more about it, maybe it really is time I add the auto-cloning feature after all...
Yes, this should in fact work. As I said to @kzkzg the constant serialize+deserialize really gives me shivers 😅 but it would be a feature disabled by default, and if you opt-in, you have taken a careful decision so... yeah, it can make sense afterall. Also it can be controllable via an entry option, so you can have a very granular control for every call, but also set it once in the Let me know what you think while I marinate this idea a little bit... Meanwhile, thanks for chipping in! |
Sounds reasonable to me! Making it configurable per call could be useful in cases where some entities are immutable and others are mutable and so must be cloned. |
Hi @kzkzg , what do you think? |
Hi @jodydonetti , sorry for late answer. For now I'm simply serialize and derialize anyway using json, but in high traffic sometimes serialization doesn't finish in time, and the value of some collection in the cache changes, and I get an exception. Sometimes retrying after a short delay helps, but sometimes it doesn't. As Daniel mentioned, mayby use existing serializers, and allow passing in a custom implementation if needed. |
Hi all, Auto-Cloning is coming in v1.3.0 which will be out... I think later today 😬 Will update later. |
Hi all, v1.3.0 is out 🥳 |
Hi,
I'm using default IMemoryCache, which keeps reference to object, so when i get value from cache and change something with it, this change apply to whole cache. Is it possible that memorycache return deepcloned objects on Get? I guess solution could be to pass custom implementation of IMemoryCache which deepclone objects on read. Do you know some other implementations of IMemoryCachen which can be used to achive that? Or mayby there is other way to do that?
I have simple code, get from cache, update value, and Set to cache again, everything is in Redis Redlock block
is it better way to do that? Mayby some specialized method which could be used like this:
The text was updated successfully, but these errors were encountered: