{"id":19772169,"url":"https://github.com/zerodha/fastcache","last_synced_at":"2025-04-30T17:33:11.174Z","repository":{"id":42526628,"uuid":"409460042","full_name":"zerodha/fastcache","owner":"zerodha","description":"fastcache is an HTTP response caching package that plugs into fastglue that simplifies \"dumb\" caching of API endpoints.","archived":false,"fork":false,"pushed_at":"2025-04-15T06:24:43.000Z","size":137,"stargazers_count":35,"open_issues_count":1,"forks_count":7,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-15T07:30:42.918Z","etag":null,"topics":["fastglue","fasthttp"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zerodha.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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-09-23T05:29:10.000Z","updated_at":"2025-04-15T06:24:41.000Z","dependencies_parsed_at":"2024-05-06T05:27:13.733Z","dependency_job_id":"6d4732f6-a907-4bb6-9464-285641b76d09","html_url":"https://github.com/zerodha/fastcache","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zerodha%2Ffastcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zerodha%2Ffastcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zerodha%2Ffastcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zerodha%2Ffastcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zerodha","download_url":"https://codeload.github.com/zerodha/fastcache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251751363,"owners_count":21637911,"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":["fastglue","fasthttp"],"created_at":"2024-11-12T05:05:38.570Z","updated_at":"2025-04-30T17:33:10.897Z","avatar_url":"https://github.com/zerodha.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fastcache\n\nfastcache is a simple response caching package that plugs into [fastglue](https://github.com/zerodha/fastglue). The\n`Cached()` middleware can be wrapped around fastglue GET handlers that need to serve\ncached bytes for a request.\n\nThe `ClearGroup()` handler is meant for invalidating cache, for\nwrapping POST / PUT / DELETE handlers. It supports arbitrary backend storage implementations and ships with redigo/go-redis store implementations.\n\n## Concepts\n\n#### `namespace (fastcache.Options.NamespaceKey)`.\n\nAll cached items are saved under a namespace. For authenticated calls, this is most commonly the user ID. So, all GET requests for a user XX1234 like orders, marketwatch, profile etc. may be namespaced under a XX1234 key in the store.\n\n`fastcache.Options.NamespaceKey` is the name of the key that'll have the name of the namespace in a `RequestCtx.UserValue(NamespaceKey)`. This UserValue should be set by another middleware, such as the auth middleware, before the `Cached()` middleware is executed. For example, `RequestCtx.SetUerValue(\"user_id\", \"XX1234\")` where `user_id` is the NamespaceKey. For handlers with params like `/orders/:user_id`, this is taken care of by the router.\n\n### `group`\n\nCache for different URIs of the same type can be grouped under a single name so that the cache for a group can be deleted in one go when something changes. For instance, orders and tradebook handlers can be grouped \"orders\" and different marketwatch calls can be grouped under \"mw\".\n\n## Middlewares\n\n`Cached()` is the middleware for GET calls that does caching, 304 serving etc.\n\n`ClearGroup()` is middleware handlers for POST / PUT / DELETE methods that are meant to clear cache for GET calls.\n\n## Manual cache clearing\n\nThe `.Del()` and `.DelGroup()` can be used to manually clear cached items when handler based clearing isn't sufficient.\n\n## Example\n```shell\n# Install fastcache.\ngo get -u github.com/zerodha/fastcache/v4\n\n# Install a store. Stores are separate packages themselves.\ngo get -u github.com/zerodha/fastcache/stores/goredis/v9\n```\n\n\n```go\n\n    fc := fastcache.New(redis.New(pool))\n\n    // Long cache options.\n    long := \u0026fastcache.Options{\n        NamespaceKey: \"user_id\",\n        ETag: true,\n    }\n    short := \u0026fastcache.Options{\n        NamespaceKey: \"user_id\",\n        TTL: time.Second * 60,\n        ETag: true,\n    }\n\n    g.GET(\"/margins\", auth(fc.Cached(handleGetMargins, short, \"margins\")))\n    g.GET(\"/margins/:segment\", auth(fc.Cached(handleGetMargins, short, \"margins\")))\n\n    g.GET(\"/orders\", auth(fc.Cached(handleGetMargins, long, \"orders\")))\n    g.GET(\"/orders\", auth(fc.Cached(handleGetMargins, short, \"trades\")))\n\n    // Clear the orders group. Multiple groups can be specified like: orders, positions ...\n    g.DELETE(\"/orders/:order_id\",auth(app.respcache.ClearGroup(handleDeleteMarketwatchItems, short, []string{\"orders\"})))\n```\n\n## Dev Notes\n\n### Running Tests\n\n```shell\n    go test -v github.com/zerodha/fastcache...\n```\n\n### Running Cluster Tests\n\nCluster tests require testcontainers (and docker) to run redis containers.\n\nTherefore, we have kept the cluster tests in a separate file and\nthey are tagged with `clustertest`. If you want to run the cluster tests,\nyou need to have docker installed and running.\n\n\n\n\nAfter that you can simply run the tests with the following command:\n\n```shell\n    go test -tags clustertest -v github.com/zerodha/fastcache...\n```\n\nWe also provide a `redis-cluster-docker-compose.yml` file that can be \nused to start a redis cluster for local testing.\n\n```shell\n    docker-compose -f redis-cluster-docker-compose.yml up -d\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzerodha%2Ffastcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzerodha%2Ffastcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzerodha%2Ffastcache/lists"}