gin-cache icon indicating copy to clipboard operation
gin-cache copied to clipboard

feat: add onlyUpdateCache option

Open tw7613781 opened this issue 2 years ago • 4 comments

provide an option to only update cache no matter cache be hit or missed to meet some business logic requirements.

tw7613781 avatar Dec 29 '23 01:12 tw7613781

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (83df4de) 62.67% compared to head (b6d3788) 64.70%. Report is 6 commits behind head on main.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #38      +/-   ##
==========================================
+ Coverage   62.67%   64.70%   +2.02%     
==========================================
  Files           5        5              
  Lines         284      289       +5     
==========================================
+ Hits          178      187       +9     
+ Misses         93       90       -3     
+ Partials       13       12       -1     
Flag Coverage Δ
unittests 64.70% <100.00%> (+2.02%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov-commenter avatar Dec 29 '23 03:12 codecov-commenter

In your scenario, is the response not required in any case (regardless of requesting the backend or not)? Only used to refresh cache?

chenyahui avatar Dec 29 '23 03:12 chenyahui

Thank you for prompt reply. The response is required, it should be the latest result of requesting the backend service.

The full scenario is as follow: the http service will have 2 endpoints.

  • one endpoint cache the result (for eg: /cache/dataset?uid=1)
  • one endpoint return the latest result from backend and refresh the cache of the above endpoint (for eg: /dataset?uid=1, it will refresh the cache of /cache/dataset?uid=1)

the code is as below

r.GET("/cache/dataset", cache.CacheByRequestURI(memoryStore, time.Hour*2, cache.IgnoreQueryOrder()), handlerFunc)
r.GET("/dataset", cache.CacheByRequestURI(memoryStore, time.Hour*2, cache.OnlyUpdateCache(), cache.IgnoreQueryOrder(), cache.WithPrefixKey("/cache")), handlerFunc)

tw7613781 avatar Dec 29 '23 05:12 tw7613781

I probably understand. Let me think about this scenario first and see if there is a more suitable solution.

chenyahui avatar Dec 29 '23 06:12 chenyahui