GithubHelp home page GithubHelp logo

victorspringer / http-cache Goto Github PK

View Code? Open in Web Editor NEW
330.0 330.0 60.0 310 KB

High performance Golang HTTP middleware for server-side application layer caching, ideal for REST APIs

Home Page: https://godoc.org/github.com/victorspringer/http-cache

License: MIT License

Go 100.00%
cache fastest http middleware

http-cache's People

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  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  avatar  avatar

http-cache's Issues

Work together

Hello, I'm working on a maintained cache system which one can be run out of the box and compatible with Caddy as caddy-module. It supports the RFC-7234 and the new one concern the Cache-Status. I would be proud to work with you on this because you did a great work on this project.
Then it supports Olric for distributed cache, Ristretto for high-performance and redis too. Then it's easy to implement a new provider by implementing a provider interface.
Check out the repo : https://github.com/darkweak/souin

Hooks to add observability?

Hi @victorspringer, first of all - thanks for a great project. http-cache is simple to use and has met all of my needs so far!

Recently I've been trying to gain more insight into the performance of one of my applications, and especially to figure out just how much impact the cache is having. I've been able to gather some simple metrics by shimming in an adapter, but there's significant context lost.

I'm wondering if you'd be open to expanding the API a bit to run some optional "observer" functions at certain times? I'm thinking specifically it would be useful to get a handle on the request or at least the request's context even in the case of a cache hit.

I'm willing to submit a PR for this work, but wanted to get your feedback first to see if this is something you're even interested in!

PURGE

Hi,

Have you thought about using the HTTP PURGE method for handling cache purges? It'd be more standards complaint and allow this to be used behind/in-front of other products. Optionally some authentication for requests if someone is exposing their app straight to the network.

Thanks,

Karl

Pute golang storage

Badger would be an excellent storage adapter.
Pure golang.
Fast and designed for SSD drives.


I would also like to make this work as a grpc middleware. That would be really useful because it would work for both grpc and http thanks to the grocery gateway and other similar libraries.

The other thing is the caching logic in relation to the storage. You normally want intimate control of this and I noticed you had some really good generic algo . But often you want to be able to pin certain namespaces.
For example anything that was related to authourisation data you would want pinned into the cache because you always want it.
This together with warmup of an empty cache would mean your most important data is always there.

Just my 2 cents...

As far as coding styles the other issue before me commented a fair bit

Review

Hi @victorspringer, this is my code review about this project.

Make sure it is compliant with RFC7234. Another good resource about http caching is the Mozilla docs.

You shouldn't commit the vendor folder as you acting as a library and not a application. Keep Gopkg.toml for ci and tests, so everytime you run a dep ensure the tests gonna be deterministic as they run against the same expected dependencies.

Don't really like the Config structure, initialize the values at the Client and then check if it has everything you need. But this is personal taste.

type Config struct {

This function has some fixed rules, it may be ok, but you could accept as params a new function on parameters to change the behaviour. What if the user want to cache post? What if only 2XX should be cached?

func (c *Client) Middleware(next http.Handler) http.Handler {

This is my 2 cents.

Cannot build for 386 architecture

Cannot build for 386 architecture for any platform. Constant frequency := 9999999999999 is too big :)

$ GOARCH=386 go build -o lol github.com/victorspringer/http-cache/adapter/memory
# github.com/victorspringer/http-cache/adapter/memory
../../go/src/github.com/victorspringer/http-cache/adapter/memory/memory.go:108:15: constant 9999999999999 overflows int

Add go.mod files for the adapters

The adapter dependencies may be CGO or non-cgo, and may be undesirable to pull into a project if unused.

Add go.mod files into adapter packages to exclude those dependencies from the main go.mod; For tests, a separate package (and separate go.mod for the test/ package) would avoid those dependencies being pulled into the main go.mod

Data race

BenchmarkHTTPCacheMamoryAdapterSetParallel-4    ==================
WARNING: DATA RACE
Read at 0x00c4200ca030 by goroutine 27:
  github.com/victorspringer/http-cache/adapter/memory.(*Adapter).Set()
      /Users/olegkovalov/go/src/github.com/victorspringer/http-cache/adapter/memory/memory.go:81 +0x57
  github.com/victorspringer/http-cache/adapter/memory/benchmark.BenchmarkHTTPCacheMamoryAdapterSetParallel.func1()
      /Users/olegkovalov/go/src/github.com/victorspringer/http-cache/adapter/memory/benchmark/benchmark_comparison_test.go:63 +0x138
  testing.(*B).RunParallel.func1()
      /usr/local/Cellar/go/1.10.1/libexec/src/testing/benchmark.go:623 +0x156

Previous write at 0x00c4200ca030 by goroutine 26:
  runtime.mapassign_fast64()
      /usr/local/Cellar/go/1.10.1/libexec/src/runtime/hashmap_fast.go:522 +0x0
  github.com/victorspringer/http-cache/adapter/memory.(*Adapter).Set()
      /Users/olegkovalov/go/src/github.com/victorspringer/http-cache/adapter/memory/memory.go:88 +0xd4
  github.com/victorspringer/http-cache/adapter/memory/benchmark.BenchmarkHTTPCacheMamoryAdapterSetParallel.func1()
      /Users/olegkovalov/go/src/github.com/victorspringer/http-cache/adapter/memory/benchmark/benchmark_comparison_test.go:63 +0x138
  testing.(*B).RunParallel.func1()
      /usr/local/Cellar/go/1.10.1/libexec/src/testing/benchmark.go:623 +0x156

Goroutine 27 (running) created at:
  testing.(*B).RunParallel()
      /usr/local/Cellar/go/1.10.1/libexec/src/testing/benchmark.go:616 +0x24b
  github.com/victorspringer/http-cache/adapter/memory/benchmark.BenchmarkHTTPCacheMamoryAdapterSetParallel()
      /Users/olegkovalov/go/src/github.com/victorspringer/http-cache/adapter/memory/benchmark/benchmark_comparison_test.go:59 +0x1de
  testing.(*B).runN()
      /usr/local/Cellar/go/1.10.1/libexec/src/testing/benchmark.go:141 +0x127
  testing.(*B).launch()
      /usr/local/Cellar/go/1.10.1/libexec/src/testing/benchmark.go:290 +0xb2

Goroutine 26 (running) created at:
  testing.(*B).RunParallel()
      /usr/local/Cellar/go/1.10.1/libexec/src/testing/benchmark.go:616 +0x24b
  github.com/victorspringer/http-cache/adapter/memory/benchmark.BenchmarkHTTPCacheMamoryAdapterSetParallel()
      /Users/olegkovalov/go/src/github.com/victorspringer/http-cache/adapter/memory/benchmark/benchmark_comparison_test.go:59 +0x1de
  testing.(*B).runN()
      /usr/local/Cellar/go/1.10.1/libexec/src/testing/benchmark.go:141 +0x127
  testing.(*B).launch()
      /usr/local/Cellar/go/1.10.1/libexec/src/testing/benchmark.go:290 +0xb2
==================
--- FAIL: BenchmarkHTTPCacheMamoryAdapterSetParallel-4
        benchmark.go:147: race detected during execution of benchmark

Questions

Hey @victorspringer, if you can help with these two questions, I'd appreciate.

  • This uses "normal" memory, and does not expect a memcached instance installed, right?

  • Would this be a good configuration for a Gorilla Mux router?

// Initialize the router
router := mux.NewRouter().StrictSlash(true)

//Memory Cache code
memcached, err := memory.NewAdapter(
  memory.AdapterWithAlgorithm(memory.LRU),
  memory.AdapterWithCapacity(10000000),
)
if err != nil {
  fmt.Println(err)
  os.Exit(1)
}

cacheClient, err := cache.NewClient(
  cache.ClientWithAdapter(memcached),
  cache.ClientWithTTL(30*time.Second),
  cache.ClientWithRefreshKey("opn"),
)
if err != nil {
  fmt.Println(err)
  os.Exit(1)
}
router.Use(cacheClient.Middleware)

Thank you!

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.