responsecache

package
v0.0.0-...-5fe9b4e Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 31 Imported by: 0

Documentation

Index

Constants

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

CacheTypeHeader values for X-Cache-Type.

Variables

This section is empty.

Functions

func ComputeGuardrailsHash

func ComputeGuardrailsHash(rules []GuardrailRuleDescriptor) string

ComputeGuardrailsHash computes the guardrails_hash for a set of rule identifiers. Each rule is represented as "name:type:order:mode:content_hash". The combined seed is sorted for stability, then passed through SHA-256. Uses xxhash64 per-component and SHA-256 for the final hash to balance speed and collision resistance.

func GuardrailsHashFromContext

func GuardrailsHashFromContext(ctx context.Context) string

GuardrailsHashFromContext retrieves the guardrails hash from the context, using the core package's storage key.

func IoReadAllBody

func IoReadAllBody(c *echo.Context) ([]byte, error)

IoReadAllBody reads and restores c.Request().Body, returning the raw bytes. Safe to call multiple times — each call resets the body.

func ShouldSkipAllCache

func ShouldSkipAllCache(req *http.Request) bool

ShouldSkipAllCache reports whether caching must be bypassed for this request, matching the exact-cache middleware semantics for no-cache and no-store.

func ShouldSkipExactCache

func ShouldSkipExactCache(req *http.Request) bool

ShouldSkipExactCache reports whether the X-Cache-Type header requests semantic-only mode.

func WithGuardrailsHash

func WithGuardrailsHash(ctx context.Context, hash string) context.Context

WithGuardrailsHash stores the guardrails hash into the context.

Types

type GuardrailRuleDescriptor

type GuardrailRuleDescriptor struct {
	Name    string
	Type    string
	Order   int
	Mode    string
	Content string
}

GuardrailRuleDescriptor describes a single active guardrail rule for hashing.

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 MapVecStore

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

MapVecStore is an in-memory VecStore for testing. It supports cosine-similarity search via dot-product on normalised vectors and TTL filtering.

func NewMapVecStore

func NewMapVecStore() *MapVecStore

NewMapVecStore creates a new in-memory VecStore.

func (*MapVecStore) Close

func (s *MapVecStore) Close() error

func (*MapVecStore) DeleteExpired

func (s *MapVecStore) DeleteExpired(_ context.Context) error

func (*MapVecStore) Insert

func (s *MapVecStore) Insert(_ context.Context, key string, vec []float32, response []byte, paramsHash string, ttl time.Duration) error

func (*MapVecStore) Len

func (s *MapVecStore) Len() int

func (*MapVecStore) Search

func (s *MapVecStore) Search(_ context.Context, vec []float32, paramsHash string, limit int) ([]VecResult, 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 (*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(*echo.Context) error,
) (*InternalHandleResult, error)

HandleInternalRequest runs the normal cache middleware for a transport-free internal request using a minimal synthetic HTTP/Echo context.

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) Middleware

Middleware returns the Echo middleware function for the exact-match (simple) cache. This is kept for backward compatibility but cache checks are now primarily done via Handle() inside the translated inference handlers, after guardrail patching.

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
⌂ 换网址