responsecache

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 34 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	CacheTypeExact      = "exact"
	CacheTypeSemantic   = "semantic"
	CacheHeaderExact    = "HIT (exact)"
	CacheHeaderSemantic = "HIT (semantic)"
)

CacheTypeHeader values for X-Cache-Type.

Variables

This section is empty.

Functions

This section is empty.

Types

type InternalHandleResult

type InternalHandleResult struct {
	StatusCode int
	Headers    http.Header
	Body       []byte
	CacheType  string
}

InternalHandleResult is the buffered result of running the cache middleware for a transport-free internal JSON request.

type InternalResponse

type InternalResponse struct {
	StatusCode   int
	ContentType  string
	Body         []byte
	FailoverUsed bool
}

InternalResponse is the buffered outcome of the transport-free LLM call a cache miss executes.

type ReplayError added in v0.1.91

type ReplayError struct {
	Err error
}

ReplayError reports that a cached stream could not be delivered to the client: the write or the final flush failed after the response was committed. The caller records the cause (a stalled or vanished client) instead of writing an error response.

func (*ReplayError) Error added in v0.1.91

func (e *ReplayError) Error() string

func (*ReplayError) Unwrap added in v0.1.91

func (e *ReplayError) Unwrap() error

type ResponseCacheMiddleware

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

ResponseCacheMiddleware wraps response cache logic. App and server only see this type.

func NewResponseCacheMiddleware

func NewResponseCacheMiddleware(
	cfg config.ResponseCacheConfig,
	resolvedProviders map[string]config.RawProviderConfig,
	usageLogger usage.LoggerInterface,
	pricingResolver usage.PricingResolver,
) (*ResponseCacheMiddleware, error)

NewResponseCacheMiddleware creates middleware from config. If neither simple nor semantic cache is configured, returns a no-op middleware. resolvedProviders must be the credential/env-resolved map from providers.InitResult (same keys as live routing). Semantic embedder.provider names a key in this map.

func NewResponseCacheMiddlewareWithStore

func NewResponseCacheMiddlewareWithStore(store cache.Store, ttl time.Duration) *ResponseCacheMiddleware

NewResponseCacheMiddlewareWithStore creates middleware with a custom store (for testing).

func NewResponseCacheMiddlewareWithStoreAndUsage added in v0.1.98

func NewResponseCacheMiddlewareWithStoreAndUsage(store cache.Store, ttl time.Duration, usageLogger usage.LoggerInterface, pricingResolver usage.PricingResolver) *ResponseCacheMiddleware

NewResponseCacheMiddlewareWithStoreAndUsage creates middleware with a custom store that records cache hits in usage (for testing).

func NewResponseCacheMiddlewareWithStoreAndUserPathPartition added in v0.2.2

func NewResponseCacheMiddlewareWithStoreAndUserPathPartition(store cache.Store, ttl time.Duration) *ResponseCacheMiddleware

NewResponseCacheMiddlewareWithStoreAndUserPathPartition creates middleware with a custom store whose entries are partitioned by user path (for testing).

func (*ResponseCacheMiddleware) Close

func (m *ResponseCacheMiddleware) Close() error

Close waits for any in-flight cache writes to complete, then releases cache resources.

func (*ResponseCacheMiddleware) HandleInternalRequest

func (m *ResponseCacheMiddleware) HandleInternalRequest(
	ctx context.Context,
	method, path string,
	body []byte,
	next func(ctx context.Context) (*InternalResponse, error),
) (*InternalHandleResult, error)

HandleInternalRequest runs the cache for a transport-free internal JSON request. Request headers are derived from the originating request snapshot (allowlisted), and next executes the LLM call, returning a buffered response instead of writing to a socket.

func (*ResponseCacheMiddleware) HandleRequest

func (m *ResponseCacheMiddleware) HandleRequest(c *echo.Context, body []byte, next func() error) error

HandleRequest runs the full dual-layer cache check (exact then semantic) for a translated inference request that has already been guardrail-patched. body is the final patched request bytes; next is the real LLM call. Streaming and non-streaming requests are cached independently. Streaming misses persist raw SSE bytes and replay them verbatim on cache hits.

func (*ResponseCacheMiddleware) Ping

Ping verifies connectivity to the Redis-backed exact cache. It returns nil when no Redis cache is configured, so callers should gate on UsesRedis first if they need to distinguish "not configured" from "reachable".

func (*ResponseCacheMiddleware) UsesRedis

func (m *ResponseCacheMiddleware) UsesRedis() bool

UsesRedis reports whether a Redis-backed exact (simple) cache is configured. Only then is the cache a meaningful readiness component worth probing.

type VecResult

type VecResult struct {
	Key      string
	Score    float32
	Response []byte
}

VecResult holds a single semantic cache lookup result.

type VecStore

type VecStore interface {
	// Search returns up to limit results whose params_hash matches and whose
	// similarity score to vec is above the caller's threshold. Expired entries
	// encountered during search are deleted before returning a miss.
	Search(ctx context.Context, vec []float32, paramsHash string, limit int) ([]VecResult, error)

	// Insert stores vec along with its response bytes and TTL metadata.
	// expires_at is recorded as unix-seconds; Search and DeleteExpired filter on it.
	Insert(ctx context.Context, key string, vec []float32, response []byte, paramsHash string, ttl time.Duration) error

	// DeleteExpired removes all entries whose expires_at is in the past.
	// Called periodically by a background goroutine started in NewVecStore.
	DeleteExpired(ctx context.Context) error

	Close() error
}

VecStore is the generic interface for all vector database backends. Each backend must implement similarity search, insertion with TTL metadata, and bulk expired-entry deletion for background cleanup.

func NewVecStore

func NewVecStore(cfg config.VectorStoreConfig) (VecStore, error)

NewVecStore creates a VecStore for the backend selected by cfg.Type. Type must be non-empty and one of: qdrant, pgvector, pinecone, weaviate.

Jump to

Keyboard shortcuts

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