go-resty / resty

Simple HTTP and REST client library for Go
MIT License
9.68k stars 681 forks source link

cache middleware #776

Closed laggu closed 4 months ago

laggu commented 5 months ago

Is there anyway to make a caching middleware?

I hope to make a response middleware that cache response with some special key and request middleware that get response from cache and skip the http request

is this possible??

jeevatkm commented 4 months ago

@laggu You could build that using OnBeforeRequest and OnAfterResponse middleware.

laggu commented 4 months ago

@jeevatkm OnBeforeRequest only have request with argument. I need to access response with OnBeforeRequest and stop request without error

How to stop request on OnBeforeRequest and return response with cached data??

jeevatkm commented 4 months ago

@laggu I'm not sure this could help, maybe give it try. You could create a cache layer (pick any good cache library). Then you could access that cache instance in OnAfterResponse to add the response to the cache; then before you fire the resty request, check your cache and to use it.

laggu commented 4 months ago

@jeevatkm I think caching api result is a common requirement for software Would you have any plan for support this feature? Support by middleware will be perfect, but adding function like retry is fine

FarmerChillax commented 2 months ago

I also have similar demands