{"id":13676678,"url":"https://github.com/chenyahui/gin-cache","last_synced_at":"2025-04-04T07:06:24.043Z","repository":{"id":40470060,"uuid":"376840137","full_name":"chenyahui/gin-cache","owner":"chenyahui","description":":rocket: A high performance gin middleware to cache http response. Compared to gin-contrib/cache, It has a huge performance improvement. 高性能gin缓存中间件，相比于官方版本，有巨大性能提升。","archived":false,"fork":false,"pushed_at":"2024-05-28T18:02:27.000Z","size":83,"stargazers_count":263,"open_issues_count":9,"forks_count":50,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-28T06:05:06.321Z","etag":null,"topics":["cache","gin","golang","middleware","redis","singleflight"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/chenyahui/gin-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/chenyahui.png","metadata":{"files":{"readme":"README_ZH.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":"2021-06-14T13:51:42.000Z","updated_at":"2025-02-26T08:39:01.000Z","dependencies_parsed_at":"2023-11-17T04:08:03.651Z","dependency_job_id":"71e50709-fc75-4159-9b61-d1b6e7b81d03","html_url":"https://github.com/chenyahui/gin-cache","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chenyahui%2Fgin-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chenyahui%2Fgin-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chenyahui%2Fgin-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chenyahui%2Fgin-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chenyahui","download_url":"https://codeload.github.com/chenyahui/gin-cache/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247135143,"owners_count":20889420,"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","gin","golang","middleware","redis","singleflight"],"created_at":"2024-08-02T13:00:31.295Z","updated_at":"2025-04-04T07:06:24.019Z","avatar_url":"https://github.com/chenyahui.png","language":"Go","funding_links":[],"categories":["Middlewares \u0026 framework add-ons"],"sub_categories":[],"readme":"# gin-cache \n[![Release](https://img.shields.io/github/release/chenyahui/gin-cache.svg?style=flat-square)](https://github.com/chenyahui/gin-cache/releases)\n[![doc](https://img.shields.io/badge/go.dev-doc-007d9c?style=flat-square\u0026logo=read-the-docs)](https://pkg.go.dev/github.com/chenyahui/gin-cache)\n[![goreportcard for gin-cache](https://goreportcard.com/badge/github.com/chenyahui/gin-cache)](https://goreportcard.com/report/github.com/chenyahui/gin-cache)\n![](https://img.shields.io/badge/license-MIT-green)\n[![codecov](https://codecov.io/gh/chenyahui/gin-cache/branch/main/graph/badge.svg?token=MX8Z4D5RZS)](https://codecov.io/gh/chenyahui/gin-cache)\n\n[English](README_ZH.md) | 🇨🇳中文\n\n一个用于缓存http接口内容的gin高性能中间件。相比于官方的gin-contrib/cache，gin-cache有巨大的性能提升。\n\n# 特性\n* 相比于gin-contrib/cache，性能提升巨大。\n* 同时支持本机内存和redis作为缓存后端。\n* 支持用户根据请求来指定cache策略。\n* 使用singleflight解决了缓存击穿问题。\n* 仅缓存http状态码为2xx的回包\n\n# 用法\n\n## 安装\n\n```\ngo get -u github.com/chenyahui/gin-cache\n```\n\n## 例子\n## 使用本地缓存\n```go\npackage main\n\nimport (\n\t\"time\"\n\n\t\"github.com/chenyahui/gin-cache\"\n\t\"github.com/chenyahui/gin-cache/persist\"\n\t\"github.com/gin-gonic/gin\"\n)\n\nfunc main() {\n\tapp := gin.New()\n\n\tmemoryStore := persist.NewMemoryStore(1 * time.Minute)\n\n\tapp.GET(\"/hello\",\n\t\tcache.CacheByRequestURI(memoryStore, 2*time.Second),\n\t\tfunc(c *gin.Context) {\n\t\t\tc.String(200, \"hello world\")\n\t\t},\n\t)\n\n\tif err := app.Run(\":8080\"); err != nil {\n\t\tpanic(err)\n\t}\n}\n```\n\n### 使用redis作为缓存\n```go\npackage main\n\nimport (\n\t\"time\"\n\n\t\"github.com/chenyahui/gin-cache\"\n\t\"github.com/chenyahui/gin-cache/persist\"\n\t\"github.com/gin-gonic/gin\"\n\t\"github.com/go-redis/redis/v8\"\n)\n\nfunc main() {\n\tapp := gin.New()\n\n\tredisStore := persist.NewRedisStore(redis.NewClient(\u0026redis.Options{\n\t\tNetwork: \"tcp\",\n\t\tAddr:    \"127.0.0.1:6379\",\n\t}))\n\n\tapp.GET(\"/hello\",\n\t\tcache.CacheByRequestURI(redisStore, 2*time.Second),\n\t\tfunc(c *gin.Context) {\n\t\t\tc.String(200, \"hello world\")\n\t\t},\n\t)\n\tif err := app.Run(\":8080\"); err != nil {\n\t\tpanic(err)\n\t}\n}\n```\n\n# 压测\n```\nwrk -c 500 -d 1m -t 5 http://127.0.0.1:8080/hello\n```\n\n## MemoryStore\n\n![MemoryStore QPS](https://www.cyhone.com/img/gin-cache/memory_cache_qps.png)\n\n## RedisStore\n\n![RedisStore QPS](https://www.cyhone.com/img/gin-cache/redis_cache_qps.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchenyahui%2Fgin-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchenyahui%2Fgin-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchenyahui%2Fgin-cache/lists"}