Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dena/cloud-datastore-interceptor
Interceptors for Cloud Datastore
https://github.com/dena/cloud-datastore-interceptor
cloud-datastore go google-cloud grpc interceptor
Last synced: 3 months ago
JSON representation
Interceptors for Cloud Datastore
- Host: GitHub
- URL: https://github.com/dena/cloud-datastore-interceptor
- Owner: DeNA
- License: mit
- Created: 2019-10-11T02:55:20.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-06-11T09:37:53.000Z (over 3 years ago)
- Last Synced: 2024-06-20T19:22:41.635Z (7 months ago)
- Topics: cloud-datastore, go, google-cloud, grpc, interceptor
- Language: Go
- Size: 38.1 KB
- Stars: 11
- Watchers: 7
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cloud-datastore-interceptor
[![GoDoc](https://godoc.org/github.com/DeNA/cloud-datastore-interceptor?status.svg)](https://godoc.org/github.com/DeNA/cloud-datastore-interceptor)
Package `github.com/DeNA/cloud-datastore-interceptor` provides gRPC interceptors for Cloud Datastore([cloud.google.com/go/datastore](https://godoc.org/cloud.google.com/go/datastore)).
## Examples
### In-Memory cache
This will use in-memory cache on [Client.Get](https://godoc.org/cloud.google.com/go/datastore#Client.Get) and [Client.GetMulti](https://godoc.org/cloud.google.com/go/datastore#Client.GetMulti).
```go
opts := []option.ClientOption{
option.WithGRPCDialOption(
grpc.WithUnaryInterceptor(
cache.UnaryClientInterceptor(memory.NewCache(1 * time.Minute)),
),
),
}
client, err := datastore.NewClient(ctx, projID, opts...)
```### Cache query results
[cache.UnaryClientInterceptor](https://godoc.org/github.com/DeNA/cloud-datastore-interceptor/cache#UnaryClientInterceptor) does not use the cache for [Query](https://godoc.org/cloud.google.com/go/datastore#Query)(e.g., [Client.GetAll](https://godoc.org/cloud.google.com/go/datastore#Client.GetAll), [Client.Run](https://godoc.org/cloud.google.com/go/datastore#Client.Run)), but by using [transform.QueryToLookupWithKeysOnly](https://godoc.org/github.com/DeNA/cloud-datastore-interceptor/transform#QueryToLookupWithKeysOnly), it is transformed to gRPC equivalent to [Client.GetMulti](https://godoc.org/cloud.google.com/go/datastore#Client.GetMulti) and the cache is used.
```go
opts := []option.ClientOption{
option.WithGRPCDialOption(
grpc.WithChainUnaryInterceptor(
transform.QueryToLookupWithKeysOnly(),
cache.UnaryClientInterceptor(memory.NewCache(1*time.Minute)),
),
),
}
client, err := datastore.NewClient(ctx, projID, opts...)
```### Redis cache
Same as [In-Memory cache](#in-memory-cache), but the backend is Redis using [redisClient](https://godoc.org/github.com/go-redis/redis#Client).
```go
opts := []option.ClientOption{
option.WithGRPCDialOption(
grpc.WithUnaryInterceptor(
cache.UnaryClientInterceptor(redis.NewCache(1*time.Minute, redisClient)),
),
),
}
client, err := datastore.NewClient(ctx, projID, opts...)
```