Skip to content

Latest commit

 

History

History
53 lines (41 loc) · 2.16 KB

README.md

File metadata and controls

53 lines (41 loc) · 2.16 KB

cloud-datastore-interceptor

GoDoc

Package github.com/DeNA/cloud-datastore-interceptor provides gRPC interceptors for Cloud Datastore(cloud.google.com/go/datastore).

Examples

In-Memory cache

This will use in-memory cache on Client.Get and Client.GetMulti.

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 does not use the cache for Query(e.g., Client.GetAll, Client.Run), but by using transform.QueryToLookupWithKeysOnly, it is transformed to gRPC equivalent to Client.GetMulti and the cache is used.

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, but the backend is Redis using redisClient.

opts := []option.ClientOption{
	option.WithGRPCDialOption(
		grpc.WithUnaryInterceptor(
			cache.UnaryClientInterceptor(redis.NewCache(1*time.Minute, redisClient)),
		),
	),
}
client, err := datastore.NewClient(ctx, projID, opts...)