{"id":13827156,"url":"https://github.com/victorspringer/http-cache","last_synced_at":"2025-07-09T03:31:00.214Z","repository":{"id":39987605,"uuid":"129509562","full_name":"victorspringer/http-cache","owner":"victorspringer","description":"High performance Golang HTTP middleware for server-side application layer caching, ideal for REST APIs","archived":false,"fork":false,"pushed_at":"2024-07-03T18:41:12.000Z","size":311,"stargazers_count":355,"open_issues_count":10,"forks_count":64,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-11-20T06:34:14.492Z","etag":null,"topics":["cache","fastest","http","middleware"],"latest_commit_sha":null,"homepage":"https://godoc.org/github.com/victorspringer/http-cache","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/victorspringer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-14T11:10:03.000Z","updated_at":"2024-11-19T01:51:01.000Z","dependencies_parsed_at":"2024-01-05T21:31:55.735Z","dependency_job_id":"691b6404-4137-487e-b2c6-9333939274e1","html_url":"https://github.com/victorspringer/http-cache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/victorspringer/http-cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/victorspringer%2Fhttp-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/victorspringer%2Fhttp-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/victorspringer%2Fhttp-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/victorspringer%2Fhttp-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/victorspringer","download_url":"https://codeload.github.com/victorspringer/http-cache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/victorspringer%2Fhttp-cache/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264386253,"owners_count":23599963,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","fastest","http","middleware"],"created_at":"2024-08-04T09:01:51.265Z","updated_at":"2025-07-09T03:30:59.950Z","avatar_url":"https://github.com/victorspringer.png","language":"Go","readme":"# http-cache\n[![Build Status](https://travis-ci.org/victorspringer/http-cache.svg?branch=master)](https://travis-ci.org/victorspringer/http-cache) [![Coverage Status](https://coveralls.io/repos/github/victorspringer/http-cache/badge.svg?branch=master)](https://coveralls.io/github/victorspringer/http-cache?branch=master) [![](https://img.shields.io/badge/godoc-reference-5272B4.svg?style=flat)](https://godoc.org/github.com/victorspringer/http-cache)\n\nThis is a high performance Golang HTTP middleware for server-side application layer caching, ideal for REST APIs.\n\nIt is simple, super fast, thread safe and gives the possibility to choose the adapter (memory, Redis, DynamoDB etc).\n\nThe memory adapter minimizes GC overhead to near zero and supports some options of caching algorithms (LRU, MRU, LFU, MFU). This way, it is able to store plenty of gigabytes of responses, keeping great performance and being free of leaks.\n\n## Getting Started\n\n### Installation\n`go get github.com/victorspringer/http-cache`\n\n### Usage\nThis is an example of use with the memory adapter:\n\n```go\npackage main\n\nimport (\n    \"log\"\n    \"net/http\"\n    \"os\"\n    \"time\"\n    \n    \"github.com/victorspringer/http-cache\"\n    \"github.com/victorspringer/http-cache/adapter/memory\"\n)\n\nfunc example(w http.ResponseWriter, r *http.Request) {\n    w.Write([]byte(\"Ok\"))\n}\n\nfunc main() {\n    memcached, err := memory.NewAdapter(\n        memory.AdapterWithAlgorithm(memory.LRU),\n        memory.AdapterWithCapacity(10000000),\n    )\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    cacheClient, err := cache.NewClient(\n        cache.ClientWithAdapter(memcached),\n        cache.ClientWithTTL(10 * time.Minute),\n        cache.ClientWithRefreshKey(\"opn\"),\n    )\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    handler := http.HandlerFunc(example)\n\n    http.Handle(\"/\", cacheClient.Middleware(handler))\n    http.ListenAndServe(\":8080\", nil)\n}\n```\n\nExample of Client initialization with Redis adapter:\n```go\nimport (\n    \"github.com/victorspringer/http-cache\"\n    \"github.com/victorspringer/http-cache/adapter/redis\"\n)\n\n...\n\n    ringOpt := \u0026redis.RingOptions{\n        Addrs: map[string]string{\n            \"server\": \":6379\",\n        },\n    }\n    cacheClient, err := cache.NewClient(\n        cache.ClientWithAdapter(redis.NewAdapter(ringOpt)),\n        cache.ClientWithTTL(10 * time.Minute),\n        cache.ClientWithRefreshKey(\"opn\"),\n    )\n\n...\n```\n\n## Benchmarks\nThe benchmarks were based on [allegro/bigache](https://github.com/allegro/bigcache) tests and used to compare it with the http-cache memory adapter.\u003cbr\u003e\nThe tests were run using an Intel i5-2410M with 8GB RAM on Arch Linux 64bits.\u003cbr\u003e\nThe results are shown below:\n\n### Writes and Reads\n```bash\ncd adapter/memory/benchmark\ngo test -bench=. -benchtime=10s ./... -timeout 30m\n\nBenchmarkHTTPCacheMamoryAdapterSet-4             5000000     343 ns/op    172 B/op    1 allocs/op\nBenchmarkBigCacheSet-4                           3000000     507 ns/op    535 B/op    1 allocs/op\nBenchmarkHTTPCacheMamoryAdapterGet-4            20000000     146 ns/op      0 B/op    0 allocs/op\nBenchmarkBigCacheGet-4                           3000000     343 ns/op    120 B/op    3 allocs/op\nBenchmarkHTTPCacheMamoryAdapterSetParallel-4    10000000     223 ns/op    172 B/op    1 allocs/op\nBenchmarkBigCacheSetParallel-4                  10000000     291 ns/op    661 B/op    1 allocs/op\nBenchmarkHTTPCacheMemoryAdapterGetParallel-4    50000000    56.1 ns/op      0 B/op    0 allocs/op\nBenchmarkBigCacheGetParallel-4                  10000000     163 ns/op    120 B/op    3 allocs/op\n```\nhttp-cache writes are slightly faster and reads are much more faster.\n\n### Garbage Collection Pause Time\n```bash\ncache=http-cache go run benchmark_gc_overhead.go\n\nNumber of entries:  20000000\nGC pause for http-cache memory adapter:  2.445617ms\n\ncache=bigcache go run benchmark_gc_overhead.go\n\nNumber of entries:  20000000\nGC pause for bigcache:  7.43339ms\n```\nhttp-cache memory adapter takes way less GC pause time, that means smaller GC overhead.\n\n## Roadmap\n- Make it compliant with RFC7234\n- Add more middleware configuration (cacheable status codes, paths etc)\n- Develop gRPC middleware\n- Develop Badger adapter\n- Develop DynamoDB adapter\n- Develop MongoDB adapter\n\n## Godoc Reference\n- [http-cache](https://godoc.org/github.com/victorspringer/http-cache)\n- [Memory adapter](https://godoc.org/github.com/victorspringer/http-cache/adapter/memory)\n- [Redis adapter](https://godoc.org/github.com/victorspringer/http-cache/adapter/redis)\n\n## License\nhttp-cache is released under the [MIT License](https://github.com/victorspringer/http-cache/blob/master/LICENSE).\n","funding_links":[],"categories":["Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvictorspringer%2Fhttp-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvictorspringer%2Fhttp-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvictorspringer%2Fhttp-cache/lists"}