Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do persistent caching in golang?

Tags:

caching

go

I am creating a command line utility using go , which will request particular server for data and will get the data from server in JSON response. Utility can do multiple requests for multiple products . I am able to do multiple requests and get the response for each product properly in JSON format. Now , I want to save the result locally in caching or local files. By which on every request I will check the local cache before sending request to server . If data is available for that product then no request will be send .

I want to save the whole json response in cache or local file and keep that data every time before doing any request to server for data.

Use Case : Products {"A","B","C","D","E"} It could be any number of products Test 1 : Get data for A,B,C Check local storage whether data is available or do request. Save json request in storage for each product.

So for test 1 ,It will have entry like: [{test 1 : [{product a : json response} , {product b : json response} ,{product c : json response}]}]

And in case if test fails in between and it get results for two products it should save response for 2 products and if we reran the test it will get result for 3rd product only.

like image 352
rohan Avatar asked Nov 08 '22 13:11

rohan


1 Answers

There's a bunch of Go libraries to do HTTP caching transparently.

  • https://github.com/lox/httpcache
  • https://github.com/gregjones/httpcache

Choose the one you like most and satisfies your needs better, both have examples in their README to get you started real fast.

If for some reason you can't or don't wanna use third-party libraries check out this answer https://stackoverflow.com/a/32885209/322221 which uses httputil.DumpResponse and http.ReadResponse, both on Go's standard library and also the answerer provides an example implementation you can base your work on.

like image 155
toqueteos Avatar answered Nov 14 '22 23:11

toqueteos