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] Add a method for list all caches in IFusionCacheProvider (Ex: for dispose purposes) #206

Closed viniciusvarzea closed 6 months ago

viniciusvarzea commented 6 months ago

Problem

I think that a method for list all caches will be very helpfull, in many applications we need to dispose some objects before the application ends. We need to manually dispose all caches (hardcoded), if we have a method to list the registered caches, we could iterate over it and dispose all caches.

Solution

Create a method at interface 'IFusionCacheProvider', called 'GetAllCaches' or something like it.

Alternatives

Additional context

jodydonetti commented 6 months ago

Hi @viniciusvarzea and thanks for using FusionCache!

Your need, if I got it right, is already taken care of by FusionCache itself when disposing the ServiceProvider, which usually happens automatically when the host app shuts down.

Basically, since any singleton service registered via DI has been requested at least once, it's the job of the DI container to dispose it, if the class implements IDisposable or a corresponding interface.

By doing so, any instance of IFusionCache that has been requested will be disposed, too.

Nitpicking corner (in case you are interested)

The so called "default cache" (meaning the FusionCache instance registered via AddFusionCache() with the default name) is requested to the service provider via the IFusionCache interface, so if it has been required even once, it will be automatically disposed.

For the named caches instead (the ones registered via AddFusionCache("name") with a custom name), what gets registered underneath is a LazyNamedCache service per each cache, which in turn can provide the actual named cache when asked: this has been done so that I can enumerate all the registered ones internally via a ctor param of type IEnumerable<LazyNamedCache> and then only if and when asked for one in particular, that cache will be actually instantiated and so, later on, disposed. In this way if you register 20 different named caches but then only work with 1, you'll only instantiate 1 (and later on automatically dispose it).

Hope this helps.

viniciusvarzea commented 6 months ago

Hello @jodydonetti thank you for the further response. I was worried about the dispose of the cache instances.

Thank you for the excellent project, you guys really rock.

jodydonetti commented 6 months ago

Hello @jodydonetti thank you for the further response. I was worried about the dispose of the cache instances.

Good, so I'll close this!

Thank you for the excellent project, you guys really rock.

Thanks, even though "us guys" it's just me 😅

image

viniciusvarzea commented 6 months ago

Wow, @jodydonetti you did a really good job alone. Thank you bro