Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zerodha/fastcache
fastcache is an HTTP response caching package that plugs into fastglue that simplifies "dumb" caching of API endpoints.
https://github.com/zerodha/fastcache
fastglue fasthttp
Last synced: about 2 months ago
JSON representation
fastcache is an HTTP response caching package that plugs into fastglue that simplifies "dumb" caching of API endpoints.
- Host: GitHub
- URL: https://github.com/zerodha/fastcache
- Owner: zerodha
- Created: 2021-09-23T05:29:10.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-05-06T09:27:11.000Z (8 months ago)
- Last Synced: 2024-05-07T05:27:06.451Z (8 months ago)
- Topics: fastglue, fasthttp
- Language: Go
- Homepage:
- Size: 77.1 KB
- Stars: 33
- Watchers: 6
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# fastcache
fastcache is a simple response caching package that plugs into [fastglue](https://github.com/zerodha/fastglue). The
`Cached()` middleware can be wrapped around fastglue GET handlers that need to serve
cached bytes for a request.The `ClearGroup()` handler is meant for invalidating cache, for
wrapping POST / PUT / DELETE handlers. It supports arbitrary backend storage implementations and ships with redigo/go-redis store implementations.## Concepts
#### `namespace (fastcache.Options.NamespaceKey)`.
All 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.
`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.
### `group`
Cache 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".
## Middlewares
`Cached()` is the middleware for GET calls that does caching, 304 serving etc.
`ClearGroup()` is middleware handlers for POST / PUT / DELETE methods that are meant to clear cache for GET calls.
## Manual cache clearing
The `.Del()` and `.DelGroup()` can be used to manually clear cached items when handler based clearing isn't sufficient.
## Example
```shell
# Install fastcache.
go get -u github.com/zerodha/fastcache/v4# Install a store. Stores are separate packages themselves.
go get -u github.com/zerodha/fastcache/stores/goredis/v9
``````go
fc := fastcache.New(redis.New(pool))
// Long cache options.
long := &fastcache.Options{
NamespaceKey: "user_id",
ETag: true,
}
short := &fastcache.Options{
NamespaceKey: "user_id",
TTL: time.Second * 60,
ETag: true,
}g.GET("/margins", auth(fc.Cached(handleGetMargins, short, "margins")))
g.GET("/margins/:segment", auth(fc.Cached(handleGetMargins, short, "margins")))g.GET("/orders", auth(fc.Cached(handleGetMargins, long, "orders")))
g.GET("/orders", auth(fc.Cached(handleGetMargins, short, "trades")))// Clear the orders group. Multiple groups can be specified like: orders, positions ...
g.DELETE("/orders/:order_id",auth(app.respcache.ClearGroup(handleDeleteMarketwatchItems, short, []string{"orders"})))
```## Dev Notes
### Running Tests
```shell
go test -v github.com/zerodha/fastcache...
```### Running Cluster Tests
Cluster tests require testcontainers (and docker) to run redis containers.
Therefore, we have kept the cluster tests in a separate file and
they are tagged with `clustertest`. If you want to run the cluster tests,
you need to have docker installed and running.After that you can simply run the tests with the following command:
```shell
go test -tags clustertest -v github.com/zerodha/fastcache...
```We also provide a `redis-cluster-docker-compose.yml` file that can be
used to start a redis cluster for local testing.```shell
docker-compose -f redis-cluster-docker-compose.yml up -d
```