ZiggyCreatures / FusionCache

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

[QUESTION] Can I use FusionCache only for getting data from a 2nd level (distributed) without InMemory part? #200

Closed suchoss closed 9 months ago

suchoss commented 9 months ago

Is it possible to skip saving data to InMemory and use only 2nd level distributed cache?

We have some really heavy distributed cache with more than 5 GB of data and also some small caches up to 10 MB.

I can imagine that those small ones would be in memory or in memory with 2nd level support. But the big heavy cache would be only in distributed cache.

Thanks

jodydonetti commented 9 months ago

Hi @suchoss , and thanks for considering FusionCache!

It's probably not the best choice, since the memory cache gives you some features that a distributed alone would not.

Having said that, while you cannot totally remove the 1st level (memory), what you can do is you can skip the memory cache, either on the read phase, on the write phase or both.

A nice thing for most of the options in FusionCache is that you can tweak them granularly, on a call-by-call basis, and the options you are looking for is called SkipMemoryCache, and you can specify it like this:

// GET OR SET
cache.GetOrSet<Product>(
    $"product:{id}",
    _ => GetProductFromDb(id),
    options => options.SetSkipMemoryCache(true)
);

// SET
cache.Set<Product>(
    $"product:{id}",
    myProduct,
    options => options.SetSkipMemoryCache(true)
);

If you want to avoid having to specify SetSkipMemoryCache(true) for every call, you can of course set that in the DefaultEntryOptions that will be used both as a default for when in each specific call you will not specify any options at all, or as a "starting point" for when you'll use the options => options.Whatever(...) version.

If I may though, I'd like to suggest a different path: you can also specify a different Duration for the distributed level, so that you can cache the values there for more time and for a very low amount of time in the memory cache. In this way you'll have the memory cache automatically populated with the most used values, and the less used will only be in the distributed cache, saving you memory on the client side.

Hope this helps!

Let me know if this worked so i can close this issue.

ps: for questions please use the Discussion tab 😉

suchoss commented 9 months ago

Hi @jodydonetti Thank you for the answer and even for the suggestion in the end. This is exactly what I needed.

jodydonetti commented 9 months ago

Happy it helped!

Out of curiosity, what you needed was the SkipMemoryCache option or the different Durations?

suchoss commented 9 months ago

I was asking out of curiosity, because we are not using FusionCache yet. We are using our own implementation (which I don't like and looking for a replacement). Yesterday I saw your presentation for dotnet community on youtubue and I immediately liked it.

I needed SkipMemoryCache, but after your answer I will probably reconsider and use Duration's when we migrate to FusionCache. Now I just need to persuade my colleague 😄.

IMO one thing I am missing now are some (3-4) examples - caching scenarios - in the repository. I guess it would help newcomers with transition.

If we decide to switch to the FusionCache (4-6 months), then I can add there some examples too 😉

jodydonetti commented 9 months ago

Yesterday I saw your presentation for dotnet community on youtubue and I immediately liked it.

That's good 😬

I needed SkipMemoryCache, but after your answer I will probably reconsider and use Duration's when we migrate to FusionCache. Now I just need to persuade my colleague 😄.

Aahah got it, let me know how it goes and, if there are reasons not to (understandable), I would like to know so I can make it better in the next versions.

IMO one thing I am missing now are some (3-4) examples - caching scenarios - in the repository. I guess it would help newcomers with transition.

These are next in line in my Trello board to-do list:

image

Right now a good place to start to see all the available options and what results they can give is the Step By Step doc imho: quite long so there's that, but also I think quite clear in understanding every main feature.

If we decide to switch to the FusionCache (4-6 months), then I can add there some examples too 😉

That would be great, thanks!