alastairtree / LazyCache

An easy to use thread safe in-memory caching service with a simple developer friendly API for c#
https://nuget.org/packages/LazyCache
MIT License
1.72k stars 159 forks source link

Upon expiration, pull data on next Get #15

Closed jjwilliams42 closed 7 years ago

jjwilliams42 commented 7 years ago

We've started using this in a project, and I am wondering after an item expires, if it's corresponding Func not called again to repopulate? It would be nice if it was kept around to repopulate after expiration.

We are creating a generic cache, where I can key by typeof(T).FullName, with a func to call out to a web api and get the data. It works, until it expires.

Once it expires the web api is never hit again and it's gone forever. I'm guessing I probably just need to keep the func around and use GetOrAddAsync instead of GetAsync.

alastairtree commented 7 years ago

Yes you should use GetOrAddAsync in this scenario. The Func factory you pass is only executed on a cache miss, so the first time or the request after expiration. All other times it is not run. See https://github.com/alastairtree/LazyCache/blob/master/Samples/ApiAsyncCachingSample/Controllers/DbTimeController.cs sample for a demonstration of this. Does that answer your question?

alastairtree commented 7 years ago

Closing this now, but let me know if you think I should reopen.