cache

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UnaryClientInterceptor

func UnaryClientInterceptor(store Store, opts ...Option) grpc.UnaryClientInterceptor

UnaryClientInterceptor is a convenience that creates a CacheInterceptor internally and returns the interceptor function directly.

Types

type CacheInterceptor

type CacheInterceptor struct {
	// contains filtered or unexported fields
}

CacheInterceptor caches gRPC unary responses. Create with NewCacheInterceptor, then pass UnaryClientInterceptor() to grpc.WithChainUnaryInterceptor.

func NewCacheInterceptor

func NewCacheInterceptor(store Store, opts ...Option) *CacheInterceptor

NewCacheInterceptor creates a cache interceptor with the given store and options.

func (*CacheInterceptor) Stats

func (c *CacheInterceptor) Stats() Stats

Stats returns cache hit/miss counters (atomic reads).

func (*CacheInterceptor) UnaryClientInterceptor

func (c *CacheInterceptor) UnaryClientInterceptor() grpc.UnaryClientInterceptor

UnaryClientInterceptor returns the interceptor for use with grpc.WithChainUnaryInterceptor.

type MemoryStore

type MemoryStore struct {
	// contains filtered or unexported fields
}

MemoryStore is a bounded in-memory Store with TTL.

func NewMemoryStore

func NewMemoryStore(maxSize int) *MemoryStore

NewMemoryStore creates a memory store. maxSize <= 0 defaults to 256.

func (*MemoryStore) Delete

func (m *MemoryStore) Delete(key string)

func (*MemoryStore) Get

func (m *MemoryStore) Get(key string) ([]byte, bool)

func (*MemoryStore) Len

func (m *MemoryStore) Len() int

Len returns the current number of entries.

func (*MemoryStore) Set

func (m *MemoryStore) Set(key string, data []byte, ttl time.Duration)

type Option

type Option func(*config)

Option configures the cache interceptor.

func WithDefaultTTL

func WithDefaultTTL(d time.Duration) Option

WithDefaultTTL sets the cache TTL (default 1 min).

func WithFilter

func WithFilter(fn func(ctx context.Context, method string, req any) bool) Option

WithFilter adds a custom predicate checked before cache lookup. Return true = eligible; false = bypass cache.

func WithKeyFunc

func WithKeyFunc(fn func(method string, req any) string) Option

WithKeyFunc overrides the default cache key generation (method + SHA-256 of proto-marshalled request).

func WithMethods

func WithMethods(methods ...string) Option

WithMethods restricts caching to the listed full method names (e.g. "/pkg.Service/Method"). Nil or empty = cache all unary methods.

func WithSingleFlight

func WithSingleFlight() Option

WithSingleFlight deduplicates concurrent cache-miss invocations for the same key so all concurrent callers share the result of a single RPC.

type Stats

type Stats struct {
	Hits   int64
	Misses int64
}

Stats holds hit/miss counters.

type Store

type Store interface {
	Get(key string) ([]byte, bool)
	Set(key string, data []byte, ttl time.Duration)
	Delete(key string)
}

Store persists cached response bytes. Implementations must be concurrent-safe.

Jump to

Keyboard shortcuts

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