DeNA / cloud-datastore-interceptor

Interceptors for Cloud Datastore

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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...)

About

Interceptors for Cloud Datastore

License:MIT License


Languages

Language:Go 100.0%