How to use FusionCache with prefetching? #69
-
(This is my first time using GitHub, so if I'm doing something wrong, please don't hesitate to tell me) Hello, I am considering using FusionCache for a project and I am currently testing it out. The scenario is that I want to prefill the cache with data, but then let the GetOrSet factory stuff handle cache misses, timeouts, cache refreshes and all that good stuff after the cache have been filled. Example: I have 10.000.000 key value pairs in a db, and i know the ids upfront. I could prefill the cache by doing GetOrSet({id}, _ => GetValue(id)) for each of the ids. So conceptually I would like to do something like
But that just seems... a bit hacky to me as i neither want to get nor set the value, i just want to set the factory up I hope it makes sense, else I am happy to provide further information or input :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hi @hvgNET and thanks for considering FusionCache. Sorry for the delay in answering but covid finally got me. In general your idea seems the right one to me:
Then, when you need one of them during the lifetime of your application, call There's this passage that I don't understand though:
Why do you say it feels hacky? Just to be clear, you don't need to call Maybe you are thinking about calling So, to recap:
Doing it this way will allow you to have the cache already filled at start + automatically keep some of those items in the cache, based on actual usage. Let me know if I got this right and if I can help you more. Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
ps: if you plan on also using a 2nd layer (distributed cache) keep in mind that the initial prefetching may not be necessary, since the data may already be in the distributed cache because of previous runs. |
Beta Was this translation helpful? Give feedback.
Hi @hvgNET and thanks for considering FusionCache.
Sorry for the delay in answering but covid finally got me.
In general your idea seems the right one to me:
Set
call for each of them (with a reasonable cache duration)Then, when you need one of them during the lifetime of your application, call
GetOrSet({id}, _ => GetValue(id))
so that if the value is still in the cache you'll get it back immediately and, if not in the cache anymore, it will be loaded from the db and saved in the cache.There's this passage that I don't understand though: