cache

package module
v0.0.0-...-3a21ebc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2024 License: MIT Imports: 11 Imported by: 0

README

Go Reference

Connect Go Caching

Easy caching for Connect RPC. Currently only supports serial read-through caching.

Usage

  • Add option idempotency_level = NO_SIDE_EFFECTS; to the RPC method you want to cache.
  • Add the connect-go-cache interceptors to your server handler.
...

service GreetService {
  rpc Greet(GreetRequest) returns (GreetResponse) {
+    // This method can be cached.
+    option idempotency_level = NO_SIDE_EFFECTS;
  }
}
...

func main() {
    mux := http.NewServeMux()

    // Configure your caching backend.
    client := redis.NewClient(&redis.Options{Addr: "localhost:6379"})
    cacheMiddleware := connectGoCache.NewCacheMiddleware(client)

+    // Configure Connect RPC Interceptors.
+    interceptors := connect.WithInterceptors(
+        cacheMiddleware.NewReadThroughInboundInterceptor(), // Manages cache interception.
+        cacheMiddleware.NewReadThroughOutboundInterceptor(), // Manages lazily updating the cache on read.
+   )

    // Register the service.
    path, handler := greetv1connect.NewGreetServiceHandler(
        &handlers.GreetServer{},
+       interceptors
    )
    mux.Handle(path, handler)

    // Start the server.
    fmt.Println("Starting server on :8080")
    http.ListenAndServe(
        "localhost:8080",
        h2c.NewHandler(mux, &http2.Server{}),
    )
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRedisClient

func NewRedisClient(addr, password string) *redis.Client

NewRedisClient creates a new Redis client

Types

type CacheMiddleware

type CacheMiddleware struct {
	Client   *redis.Client
	CacheTTL time.Duration
}

CacheMiddleware struct holds the Redis client

func NewCacheMiddleware

func NewCacheMiddleware(redisClient *redis.Client) *CacheMiddleware

NewCacheMiddleware initializes the caching middleware with a Redis client

func (*CacheMiddleware) NewReadThroughInboundInterceptor

func (cm *CacheMiddleware) NewReadThroughInboundInterceptor() connect.UnaryInterceptorFunc

func (*CacheMiddleware) NewReadThroughOutboundInterceptor

func (cm *CacheMiddleware) NewReadThroughOutboundInterceptor() connect.UnaryInterceptorFunc

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL