{"id":13623474,"url":"https://github.com/gin-contrib/cache","last_synced_at":"2025-06-13T18:39:31.787Z","repository":{"id":43536261,"uuid":"73724389","full_name":"gin-contrib/cache","owner":"gin-contrib","description":"Gin middleware/handler to enable Cache","archived":false,"fork":false,"pushed_at":"2024-05-10T00:47:07.000Z","size":197,"stargazers_count":351,"open_issues_count":35,"forks_count":94,"subscribers_count":13,"default_branch":"master","last_synced_at":"2024-05-10T01:41:16.914Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://gin-gonic.github.io/gin","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/gin-contrib.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":"2016-11-14T16:25:50.000Z","updated_at":"2024-06-18T12:26:04.267Z","dependencies_parsed_at":"2023-01-30T23:30:18.404Z","dependency_job_id":"f0d51810-de1b-4fab-8d2c-9f54fd20b2d3","html_url":"https://github.com/gin-contrib/cache","commit_stats":null,"previous_names":["dpordomingo/go-gingonic-cache"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gin-contrib%2Fcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gin-contrib%2Fcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gin-contrib%2Fcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gin-contrib%2Fcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gin-contrib","download_url":"https://codeload.github.com/gin-contrib/cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249089093,"owners_count":21210917,"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":[],"created_at":"2024-08-01T21:01:32.238Z","updated_at":"2025-06-13T18:39:31.773Z","avatar_url":"https://github.com/gin-contrib.png","language":"Go","readme":"# Cache middleware\n\n[![Build Status](https://github.com/gin-contrib/cache/actions/workflows/testing.yml/badge.svg)](https://github.com/gin-contrib/cache/actions/workflows/testing.yml)\n[![codecov](https://codecov.io/gh/gin-contrib/cache/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-contrib/cache)\n[![Go Report Card](https://goreportcard.com/badge/github.com/gin-contrib/cache)](https://goreportcard.com/report/github.com/gin-contrib/cache)\n[![GoDoc](https://godoc.org/github.com/gin-contrib/cache?status.svg)](https://godoc.org/github.com/gin-contrib/cache)\n\nGin middleware/handler to enable Cache.\n\n- [Cache middleware](#cache-middleware)\n  - [Usage](#usage)\n    - [Start using it](#start-using-it)\n    - [InMemory Example](#inmemory-example)\n    - [Redis Example](#redis-example)\n\n## Usage\n\n### Start using it\n\nDownload and install it:\n\n```sh\ngo get github.com/gin-contrib/cache\n```\n\nImport it in your code:\n\n```go\nimport \"github.com/gin-contrib/cache\"\n```\n\n### InMemory Example\n\nSee the [example](example/example.go)\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"time\"\n\n  \"github.com/gin-contrib/cache\"\n  \"github.com/gin-contrib/cache/persistence\"\n  \"github.com/gin-gonic/gin\"\n)\n\nfunc main() {\n  r := gin.Default()\n\n  store := persistence.NewInMemoryStore(time.Second)\n\n  r.GET(\"/ping\", func(c *gin.Context) {\n    c.String(200, \"pong \"+fmt.Sprint(time.Now().Unix()))\n  })\n  // Cached Page\n  r.GET(\"/cache_ping\", cache.CachePage(store, time.Minute, func(c *gin.Context) {\n    c.String(200, \"pong \"+fmt.Sprint(time.Now().Unix()))\n  }))\n\n  // Listen and Server in 0.0.0.0:8080\n  r.Run(\":8080\")\n}\n```\n\nYou can also use the `Delete` and `Flush` methods with the InMemory store:\n\n```go\n// Delete a specific cache entry by key\nerr := store.Delete(\"your-cache-key\")\nif err != nil {\n  // handle error\n}\n\n// Flush all cache entries\nerr = store.Flush()\nif err != nil {\n  // handle error\n}\n```\n\n### Redis Example\n\nHere is a complete example using Redis as the cache backend with `NewRedisCacheWithURL`:\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"time\"\n\n  \"github.com/gin-contrib/cache\"\n  \"github.com/gin-contrib/cache/persistence\"\n  \"github.com/gin-gonic/gin\"\n)\n\nfunc main() {\n  r := gin.Default()\n\n  // Basic usage:\n  store := persistence.NewRedisCacheWithURL(\"redis://localhost:6379\", time.Minute)\n\n  // Advanced configuration with password and DB number:\n  // store := persistence.NewRedisCacheWithURL(\"redis://:password@localhost:6379/0\", time.Minute)\n\n  r.GET(\"/ping\", func(c *gin.Context) {\n    c.String(200, \"pong \"+fmt.Sprint(time.Now().Unix()))\n  })\n  // Cached Page\n  r.GET(\"/cache_ping\", cache.CachePage(store, time.Minute, func(c *gin.Context) {\n    c.String(200, \"pong \"+fmt.Sprint(time.Now().Unix()))\n  }))\n\n  // Listen and serve on 0.0.0.0:8080\n  r.Run(\":8080\")\n}\n```\n","funding_links":[],"categories":["Middlewares","Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgin-contrib%2Fcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgin-contrib%2Fcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgin-contrib%2Fcache/lists"}