ZiggyCreatures / FusionCache

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

Improve the performance of the FusionCacheProvider #249

Closed 0xced closed 1 month ago

0xced commented 1 month ago

Storing the caches in a dictionary upfront allows to retrieve the cache in O(1) instead of O(n).

Using Lazy<IFusionCache> allows to preserve the semantic of throwing when accessing the (incorrectly configured) caches.

Also, the DependencyInjectionTests were very slightly improved:

jodydonetti commented 1 month ago

Hi @0xced I updated this branch based on the new main, and pushed some changes which build upon your work and can make it even better.

Basically instead of storing an IEnumerable to iterate on at every GetCache() call, I just stored either a single LazyNamedCache in case there was only one, or null in case they were more than 1: in this way no need to an iteration per-call, less cpu usage and less allocations.

Finally I've made the tests a little better around these scenarios.

Thoughts?

0xced commented 1 month ago

Looks good to me too!