Categorygithub.com/cappuccinotm/gcache
repositorypackage
0.1.0
Repository: https://github.com/cappuccinotm/gcache.git
Documentation: pkg.go.dev

# README

gcache Go Reference Go codecov

gcache is a gRPC caching library that provides a simple way to cache gRPC requests and responses. It is designed to be used with gRPC services that have a high request rate and where caching can improve the performance of the service.

It uses a plain old ETag and If-None-Match headers to cache the responses. The cache is stored in memory and is not persistent (unless you use a persistent cache store).

It also provides a server-side caching implementation.

Installation

go get -u github.com/cappuccinotm/gcache

Usage

Client-side caching

icptr := gcache.NewInterceptor(gcache.WithLogger(slog.Default()))
conn, err := grpc.NewClient("localhost:8080",
    grpc.WithTransportCredentials(insecure.NewCredentials()),
    grpc.WithUnaryInterceptor(icptr.UnaryClientInterceptor()),
)
if err != nil {
    return fmt.Errorf("dial localhost:8080: %w", err)
}

client := order.NewOrderServiceClient(conn)

Client-side interceptor seeks for server's ETag header in the response, if the server has provided one, it stores the response in the cache. When the client sends a request, the interceptor adds the If-None-Match header to the request. If the server responds with code Aborted and the ETag header, equal to the one that has been sent by client, the interceptor returns the cached response.

Server-side caching

icptr := gcache.NewInterceptor(gcache.WithLogger(slog.Default()))
server := grpc.NewServer(
    grpc.WithTransportCredentials(insecure.NewCredentials()),
    grpc.WithUnaryInterceptor(icptr.UnaryServerInterceptor()),
)

Server-side interceptor always sends the cached response unless client has specifically set the Cache-Control: no-cache header.