Giter Site home page Giter Site logo

bxcodec / httpcache Goto Github PK

View Code? Open in Web Editor NEW
58.0 4.0 15.0 97 KB

Get a working HTTP Cache in Go (Golang) with only 3 lines of code!!!!

License: MIT License

Go 96.35% Makefile 3.65%
golang golang-library go cache http-cache http-client httpcache httpcachedclient rfc-7234

httpcache's Introduction

Iman Tumorang โ€” @bxcodec

Hi! I'm Iman!

Welcome to my Github Profile!

๐ŸŒฑ My Contributions So Far!

github stats

๐Ÿ“ My Latest Blog Post in Medium

๐Ÿ“ฎ Get In Touch!

httpcache's People

Contributors

bxcodec avatar dependabot[bot] avatar mhmxs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

httpcache's Issues

Is this repo still being maintained?

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!

Cache Key should contain more than just the HTTP Method

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

key = fmt.Sprintf("%s %s", req.Method, req.RequestURI)

Implement way to handle log messages

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.

Does not take care of the max-age when setting rfcCompliance to true

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.