Hi! I'm Iman!
Welcome to my Github Profile!
(Generated by Medium's Recent Article)
- Contact me through Email, Twitter, Instagram, LinkedIn in https://imantumorang.com
Get a working HTTP Cache in Go (Golang) with only 3 lines of code!!!!
License: MIT License
Hi! I'm Iman!
Welcome to my Github Profile!
Putting this as an issue for tracking
When user gets a file from a server, we also can temporarily cache it into the current disk of the application.
There are a couple of issues in this repo opened for a year and some of the important ones are not merged in, like #29. Just wondering if this repo is still being actively maintained or not? Thanks!
The library uses a Cache Key composed out of the Request Method and http.Request.RequestURI
.
However "RequestURI" is a server only field and is empty on client side.
Thus all GET Request share the same cache key. ("GET "). As a result this library is basically is a single page cache.
See
Line 222 in e926f2b
In the current state, the client will print cached-item already expired failed to retrieve from cache, trying with a live version
whenever a cache miss happens.
And there doesn't seems to be a way to disable/handle those log messages.
Or, if there is one, it's not documented.
Whenever a cache makes sense in a go project, there tend to be quite a lot of requests as well. In order to not clog the logs, it would be a very useful feature to be able to adjust what is printed and when - or just disable the messages completely as a first step.
Hello,
I am testing httpcache with my http client and I encountered an unexpected result.
I send a request to a server that responds me the following response (see the header "Cache-Control: no-transform, max-age=90") :
Hypertext Transfer Protocol HTTP/1.1 200 OK\r\n Server: openresty/1.15.8.2\r\n Date: Thu, 17 Jun 2021 09:19:58 GMT\r\n Content-Type: application/json\r\n Transfer-Encoding: chunked\r\n Connection: keep-alive\r\n Vary: Accept-Encoding\r\n Cache-Control: no-transform, max-age=90\r\n ETag: W/"79736d2d"\r\n Content-Encoding: gzip\r\n \r\n ...
my request is the following:
Hypertext Transfer Protocol GET ... HTTP/1.1\r\n // I change the request by ... Host: myserver:27220\r\n User-Agent: OpenAPI-Generator/1.0.0/go\r\n Accept: application/json\r\n Content-Type: application/json\r\n Accept-Encoding: gzip\r\n \r\n
When rfcCompliance is set to true, max-age is not respected
_, err := httpcache.NewWithInmemoryCache(innerHTTPClient, true, time.Second*120)
see the result:
2021/06/17 11:47:45 Cache item's missing failed to retrieve from cache, trying with a live version Response time: 107426ms Response time: 87ms Response time: 67ms Response time: 49ms Response time: 63ms Response time: 50ms Response time: 54ms Response time: 55ms Response time: 52ms Response time: 55ms WARNING [discover.go:42] Sleep for 100s Response time: 507ms Response time: 58ms Response time: 46ms Response time: 33ms Response time: 40ms Response time: 29ms Response time: 33ms Response time: 68ms Response time: 295ms Response time: 150ms WARNING [discover.go:56] Sleep for 130s 2021/06/17 11:51:35 Cache item's missing failed to retrieve from cache, trying with a live version Response time: 121455ms Response time: 304ms Response time: 190ms Response time: 279ms Response time: 196ms Response time: 140ms Response time: 123ms Response time: 107ms Response time: 727ms Response time: 304ms
But when rfcCompliance is set to false, max-age is respected
_, err := httpcache.NewWithInmemoryCache(innerHTTPClient, true, time.Second*120)
see the result:
2021/06/17 12:34:06 Cache item's missing failed to retrieve from cache, trying with a live version Response time: 120114ms Response time: 150ms Response time: 171ms Response time: 112ms Response time: 71ms Response time: 56ms Response time: 55ms Response time: 47ms Response time: 42ms Response time: 55ms WARNING [discover.go:56] Sleep for 80s Response time: 148ms Response time: 202ms Response time: 59ms Response time: 45ms Response time: 35ms Response time: 49ms Response time: 33ms Response time: 36ms Response time: 46ms Response time: 32ms WARNING [discover.go:70] Sleep for 100s 2021/06/17 12:37:36 Cache item's missing failed to retrieve from cache, trying with a live version Response time: 111084ms Response time: 97ms Response time: 61ms Response time: 43ms Response time: 50ms Response time: 58ms Response time: 46ms Response time: 65ms Response time: 44ms Response time: 191ms WARNING [discover.go:84] Sleep for 130s 2021/06/17 12:39:46 Cache item's missing failed to retrieve from cache, trying with a live version Response time: 110458ms Response time: 88ms Response time: 56ms Response time: 54ms Response time: 39ms Response time: 54ms Response time: 60ms Response time: 1108ms Response time: 103ms Response time: 72ms
In RFC 7234, it is stated "A cache MUST NOT store a response to any request, unless the response contains a max-age response directive". So far, I understand, that in the response from the server, there is a directive max-age, then setting rfcCompliance to true should work.
Could you correct me if I am wrong, or missing something
Many thanks in advance
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.