caching

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2023 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// headers
	CacheTypeClient = "client"
	CacheTypeServer = "server"

	GrpcNoCache = "no-cache"
	// server can also manually set this to revalidate the cache
	GrpcMustRevalidateCache = "must-revalidate"
	GrpcCacheMiss           = "miss"
	GrpcCacheHit            = "hit"

	GrpcCacheIdentifier = "x-cache-req-id"
)

Variables

View Source
var (
	GrpcCacheControlHeader = func(cacheType string) string {
		return "cache-control-" + cacheType
	}
	GrpcCacheStatusHeader = func(fullMethod string) string {
		return "x-cache-" + fullMethod
	}

	GrpcMaxAge = func(d time.Duration) string {
		return fmt.Sprintf("max-age=%d", int(d.Seconds()))
	}
)

Functions

func ForceClientCaching added in v0.9.2

func ForceClientCaching(ctx context.Context, ttl time.Duration)

ForceClientCaching The client calling this RPC will be forced into caching the response for the given TTL, unless the client has explicitly opted-out of caching.

func RegisterProtoType added in v0.9.2

func RegisterProtoType(msg proto.Message, f func(msg protoreflect.ProtoMessage) string)

Register at init time a proto message from an external package to be used with the caching interceptor

func WithBypassCache added in v0.9.2

func WithBypassCache(ctx context.Context) context.Context

func WithCacheControlHeaders added in v0.9.2

func WithCacheControlHeaders(
	ctx context.Context,
	cacheType string,
	values ...string,
) context.Context

func WithGrpcClientCaching added in v0.9.2

func WithGrpcClientCaching(ctx context.Context, d time.Duration) context.Context

func WithHttpMaxAgeCachingHeader added in v0.9.2

func WithHttpMaxAgeCachingHeader(h http.Header, maxAge time.Duration)

WithHttpMaxAgeCachingHeader : header in the request or response that indicates the transport should set the value in the cache with ttl specified by maxAge

func WithHttpNoCachingHeader added in v0.9.2

func WithHttpNoCachingHeader(h http.Header)

WithHttpNoCachingHeader : header in the request or response that indicates the transport should skip the cache

func WithHttpNoStoreHeader added in v0.9.2

func WithHttpNoStoreHeader(h http.Header)

WithHttpNoStoreHeader : header in the request or response that indicates the response should not be stored in the cache

Types

type CacheKeyer added in v0.9.2

type CacheKeyer interface {
	CacheKey() string
}

CacheKeyer opt-in interface that proto messages implement. Used to determine if they are unique without hashing

type CachingProvider added in v0.9.2

type CachingProvider[T any] interface {
	storage.GrpcTtlCache[T]
	SetCache(storage.GrpcTtlCache[T])
	// contains filtered or unexported methods
}

func NewDefaultCachingProvider added in v0.9.2

func NewDefaultCachingProvider[T any]() CachingProvider[T]

type GrpcCachingInterceptor added in v0.9.2

type GrpcCachingInterceptor interface {
	UnaryServerInterceptor() grpc.UnaryServerInterceptor
	UnaryClientInterceptor() grpc.UnaryClientInterceptor
	CachingProvider[proto.Message]
}

type GrpcClientTtlCacher added in v0.9.2

type GrpcClientTtlCacher struct {
	CachingProvider[proto.Message]
	// contains filtered or unexported fields
}

func NewClientGrpcTtlCacher added in v0.9.2

func NewClientGrpcTtlCacher() *GrpcClientTtlCacher

func (*GrpcClientTtlCacher) UnaryClientInterceptor added in v0.9.2

func (g *GrpcClientTtlCacher) UnaryClientInterceptor() grpc.UnaryClientInterceptor

func (*GrpcClientTtlCacher) UnaryServerInterceptor added in v0.9.2

func (g *GrpcClientTtlCacher) UnaryServerInterceptor() grpc.UnaryServerInterceptor

type HttpCachingTransport added in v0.9.2

type HttpCachingTransport interface {
	http.RoundTripper
	storage.HttpTtlCache[[]byte]

	Use(*http.Client) error
}

HttpCachingTransport

!! Client libraries that use a *http.Client may overwrite the transport so you may want to consider using this interface with something like https://github.com/hashicorp/go-cleanhttp

type InMemoryGrpcTtlCache added in v0.9.2

type InMemoryGrpcTtlCache[T any] struct {
	// contains filtered or unexported fields
}

func NewInMemoryGrpcTtlCache added in v0.9.2

func NewInMemoryGrpcTtlCache(
	memoryLimitBytes int64,
	maxAge time.Duration,
) *InMemoryGrpcTtlCache[protoreflect.ProtoMessage]

func (InMemoryGrpcTtlCache[T]) Delete added in v0.9.2

func (i InMemoryGrpcTtlCache[T]) Delete(key string)

func (InMemoryGrpcTtlCache[T]) Get added in v0.9.2

func (i InMemoryGrpcTtlCache[T]) Get(key string) (resp T, ok bool)

func (InMemoryGrpcTtlCache[T]) MaxAge added in v0.9.2

func (i InMemoryGrpcTtlCache[T]) MaxAge() time.Duration

func (InMemoryGrpcTtlCache[T]) Set added in v0.9.2

func (i InMemoryGrpcTtlCache[T]) Set(key string, resp T, ttl time.Duration)

type InMemoryHttpTtlCache

type InMemoryHttpTtlCache[T any] struct {
	// contains filtered or unexported fields
}

func NewInMemoryHttpTtlCache

func NewInMemoryHttpTtlCache(
	memoryLimitBytes int64,
	maxAge time.Duration,
) *InMemoryHttpTtlCache[[]byte]

func (InMemoryHttpTtlCache[T]) Delete

func (i InMemoryHttpTtlCache[T]) Delete(key string)

func (InMemoryHttpTtlCache[T]) Get

func (i InMemoryHttpTtlCache[T]) Get(key string) (req T, ok bool)

func (InMemoryHttpTtlCache[T]) MaxAge

func (i InMemoryHttpTtlCache[T]) MaxAge() time.Duration

func (InMemoryHttpTtlCache[T]) Set

func (i InMemoryHttpTtlCache[T]) Set(key string, req T)

type InternalHttpCachingTransport added in v0.9.2

type InternalHttpCachingTransport struct {
	*httpcache.Transport
	storage.HttpTtlCache[[]byte]
}

InternalHttpCachingTransport Mostly RFC7234 compliant, see https://pkg.go.dev/github.com/gregjones/httpcache Should be used for internal caching only, not for LB, proxying, etc.

func NewInternalHttpCacheTransport added in v0.9.2

func NewInternalHttpCacheTransport(cache storage.HttpTtlCache[[]byte]) *InternalHttpCachingTransport

func (*InternalHttpCachingTransport) Use added in v0.9.2

Jump to

Keyboard shortcuts

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