ZiggyCreatures / FusionCache

FusionCache is an easy to use, fast and robust hybrid cache with advanced resiliency features.
MIT License
1.65k stars 87 forks source link

[FEATURE] Disposing evicted items #169

Closed giulianob closed 9 months ago

giulianob commented 1 year ago

Problem

I'm migrating from IMemoryCache and I was able to call Dispose on the evicted items by passing the item to the PostEvictionCallback, for example:

  new MemoryCacheEntryOptions()
      .RegisterPostEvictionCallback(
          callback: (key, value, reason, state) => { ((IDisposable)state).Dispose(); },
          state: item);

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 the Eviction handler.

jodydonetti commented 1 year 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.

jodydonetti commented 1 year ago

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?

giulianob commented 1 year ago

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.

giulianob commented 1 year ago

@jodydonetti Is there any workaround available to get the item being evicted in the callback?

jodydonetti commented 1 year ago

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.

jodydonetti commented 1 year ago

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?

giulianob commented 1 year ago

Yeah pre-release would be fine. Thanks!

jodydonetti commented 1 year ago

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!

jodydonetti commented 12 months ago

Ho @giulianob , did you have time to check this?

Thanks!

giulianob commented 12 months ago

Yes, exactly what I needed. Thanks!

jodydonetti commented 12 months ago

Awesome, thanks 🥳

Will close this when the non-preview next version will be release.

jodydonetti commented 9 months ago

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.