Closed giulianob closed 9 months ago
Hi @giulianob and thanks for using FusionCache!
A couple of points.
I may add a FusionCacheEntryOption
to auto-dispose a value when evicted, so that you may have granular control per each cache entry. Of course as always you may then set this in the DefaultEntryOptions
once and it will be used as a default for all entries.
Sounds good?
What do you think should happen when a value is being set (either by calling a Set()
, GetOrSet()
or because of a remote notification from the backplane, etc) and there's an override? I'm not 100% sure IMemoryCache
always considers this an eviction even though here there's a Replaced
value (anyway I'll check and do some tests), so I would like to know if in this case you would also like to dispose, since the old value is being overwritten.
Ofcourse in that case I should also probably check for the fact that the new value is different from the old one, otherwise a piece of code like this:
var myDisposable = new MyDisposable();
cache.Set("foo", myDisposable);
cache.Set("foo", myDisposable);
would end up disposing the very object we are about to set.
Let me know what you think, thanks.
In the meantime I was also thinking about this: as we know a memory cache stores live object references, therefore when you get something from the cache you are using exactly the same instance.
This means that if you get something from the cache and then it gets disposed, you'll end up working on a disposed object, with all the problems this will create.
A practical example:
var myDisposable = new MyDisposable();
cache.Set("foo", myDisposable, TimeSpan.FromSeconds(2));
var foo = cache.GetOrDefault<MyDisposable>("foo");
// 2 SEC LATER...
if (foo is not null) {
// THIS WILL PROBABLY THROW AN ObjectDisposedException
foo.Whatever();
}
What do you think?
In my scenario, I am making sure to set the duration longer than the amount of time the object can be referenced (the request timeout in my case). You do need to be careful. I am storing objects that must be disposed to free up resources rather than waiting for GC to occur.
@jodydonetti Is there any workaround available to get the item being evicted in the callback?
Hi @giulianob , I'm looking into this right now. I'd like to avoid releasing a new version in a rush, so if I can find a quick temporary workaround I'll tell you right away. Will update you asap.
I think I need to release a new version, there's no other way around it.
I'll try to release it tomorrow night.
Just to be sure: a pre-release version would be ok right?
Yeah pre-release would be fine. Thanks!
Hi all, the v0.24.0-preview1 pre-release is out now on Nuget 🎉
NOTE: remember to check the "include prerelease" checkbox in your IDE to see it!
Ho @giulianob , did you have time to check this?
Thanks!
Yes, exactly what I needed. Thanks!
Awesome, thanks 🥳
Will close this when the non-preview next version will be release.
Hi all, the v0.24.0 release is out now on Nuget 🎉
It is a massive release with a lot of new features, optimizations and more, I suggest to check out the release notes.
Problem
I'm migrating from
IMemoryCache
and I was able to callDispose
on the evicted items by passing the item to thePostEvictionCallback
, for example:Solution
I'd like the ability to either having FusionCache auto call
Dispose
on evicted items or a way to pass the evicted item to theEviction
handler.