Documentation
¶
Overview ¶
Package cache provides a pluggable caching layer for HotPlex. It supports multiple cache backends (memory, redis, semantic) with a unified interface. Default implementation is noop (no-op) for backward compatibility.
Index ¶
- Constants
- Variables
- func ComputeKey(parts ...string) string
- func Delete(ctx context.Context, key string) error
- func GetJSON(ctx context.Context, key string, out interface{}) error
- func GetOrCompute(ctx context.Context, key string, compute func() ([]byte, error), ...) ([]byte, error)
- func PromptCacheKey(sessionID, prompt string) string
- func ResponseCacheKey(sessionID, prompt string, model string) string
- func SessionCacheKey(sessionID string) string
- func Set(ctx context.Context, key string, value []byte, opts ...CacheOption) error
- func SetGlobalCache(cache Cache)
- func SetJSON(ctx context.Context, key string, value interface{}, opts ...CacheOption) error
- func ToolCacheKey(toolName string, args map[string]interface{}) string
- type Cache
- type CacheEntry
- type CacheHelper
- func (h *CacheHelper) DeletePrefix(ctx context.Context, prefix string) error
- func (h *CacheHelper) GetJSON(ctx context.Context, key string, out interface{}) error
- func (h *CacheHelper) GetOrCompute(ctx context.Context, key string, compute func() ([]byte, error), ...) ([]byte, error)
- func (h *CacheHelper) GetOrComputeJSON(ctx context.Context, key string, compute func() (interface{}, error), ...) error
- func (h *CacheHelper) SetJSON(ctx context.Context, key string, value interface{}, opts ...CacheOption) error
- type CacheOption
- type CacheOptions
- type CacheStats
- type NoOpCache
- func (c *NoOpCache) Clear(ctx context.Context) error
- func (c *NoOpCache) Close() error
- func (c *NoOpCache) Delete(ctx context.Context, key string) error
- func (c *NoOpCache) DeleteByTag(ctx context.Context, tag string) error
- func (c *NoOpCache) Exists(ctx context.Context, key string) (bool, error)
- func (c *NoOpCache) Get(ctx context.Context, key string) (*CacheEntry, error)
- func (c *NoOpCache) GetStats(ctx context.Context) (*CacheStats, error)
- func (c *NoOpCache) ListKeysByTag(ctx context.Context, tag string) ([]string, error)
- func (c *NoOpCache) Name() string
- func (c *NoOpCache) Set(ctx context.Context, key string, value []byte, opts ...CacheOption) error
- type StatsProvider
- type TaggedCache
Constants ¶
const ( // KeyPrefixPrompt is the prefix for prompt cache keys KeyPrefixPrompt = "prompt:" // KeyPrefixResponse is the prefix for response cache keys KeyPrefixResponse = "response:" // KeyPrefixSession is the prefix for session context cache keys KeyPrefixSession = "session:" // KeyPrefixTool is the prefix for tool result cache keys KeyPrefixTool = "tool:" )
Common cache key prefixes for HotPlex
Variables ¶
var ( // TTLShort is for short-lived cache entries (5 minutes) TTLShort = 5 * time.Minute // TTLMedium is for medium-lived cache entries (1 hour) TTLMedium = 1 * time.Hour // TTLLong is for long-lived cache entries (24 hours) TTLLong = 24 * time.Hour // TTLPermanent is for permanent cache entries (no expiration) TTLPermanent = time.Duration(0) )
Common TTL presets
var ErrCacheMiss = fmt.Errorf("cache miss")
ErrCacheMiss indicates that the requested key was not found in the cache.
Functions ¶
func ComputeKey ¶
ComputeKey generates a cache key from input data using SHA256. This is useful for caching API responses, prompts, etc.
func GetOrCompute ¶
func GetOrCompute(ctx context.Context, key string, compute func() ([]byte, error), opts ...CacheOption) ([]byte, error)
GetOrCompute is a convenience function to get or compute a value from the default cache.
func PromptCacheKey ¶
PromptCacheKey generates a cache key for a prompt.
func ResponseCacheKey ¶
ResponseCacheKey generates a cache key for a response.
func SessionCacheKey ¶
SessionCacheKey generates a cache key for session context.
func SetGlobalCache ¶
func SetGlobalCache(cache Cache)
SetGlobalCache sets the global cache instance. This should be called during application initialization.
func SetJSON ¶
func SetJSON(ctx context.Context, key string, value interface{}, opts ...CacheOption) error
SetJSON is a convenience function to set a JSON value in the default cache.
func ToolCacheKey ¶
ToolCacheKey generates a cache key for tool results.
Types ¶
type Cache ¶
type Cache interface {
// Get retrieves a value from the cache by key.
// Returns nil if the key doesn't exist or has expired.
Get(ctx context.Context, key string) (*CacheEntry, error)
// Set stores a value in the cache with the given key.
// Options can specify TTL, tags, and metadata.
Set(ctx context.Context, key string, value []byte, opts ...CacheOption) error
// Delete removes a value from the cache by key.
// Returns nil if the key doesn't exist (idempotent).
Delete(ctx context.Context, key string) error
// Exists checks if a key exists in the cache (without checking expiration).
Exists(ctx context.Context, key string) (bool, error)
// Clear removes all cache entries.
// Use with caution in production.
Clear(ctx context.Context) error
// Close gracefully shuts down the cache backend.
Close() error
// Name returns the cache backend name for logging.
Name() string
}
Cache is the main cache interface that all backends must implement.
func DefaultCache ¶
func DefaultCache() Cache
DefaultCache is an alias for GetGlobalCache for convenience.
type CacheEntry ¶
type CacheEntry struct {
// Key is the unique identifier for this entry
Key string
// Value is the cached data
Value []byte
// CreatedAt is the timestamp when this entry was created
CreatedAt time.Time
// ExpiresAt is the timestamp when this entry expires (zero means no expiration)
ExpiresAt time.Time
// Metadata contains optional metadata about the cached entry
Metadata map[string]string
}
CacheEntry represents a cached item with metadata.
func Get ¶
func Get(ctx context.Context, key string) (*CacheEntry, error)
Get is a convenience function to get a value from the default cache.
func (*CacheEntry) IsExpired ¶
func (e *CacheEntry) IsExpired() bool
IsExpired returns true if the cache entry has expired.
type CacheHelper ¶
type CacheHelper struct {
// contains filtered or unexported fields
}
CacheHelper provides convenient methods for common caching operations.
func NewCacheHelper ¶
func NewCacheHelper(cache Cache) *CacheHelper
NewCacheHelper creates a new cache helper.
func (*CacheHelper) DeletePrefix ¶
func (h *CacheHelper) DeletePrefix(ctx context.Context, prefix string) error
DeletePrefix deletes all keys with the given prefix.
func (*CacheHelper) GetJSON ¶
func (h *CacheHelper) GetJSON(ctx context.Context, key string, out interface{}) error
GetJSON retrieves a JSON value from the cache and unmarshals it.
func (*CacheHelper) GetOrCompute ¶
func (h *CacheHelper) GetOrCompute(ctx context.Context, key string, compute func() ([]byte, error), opts ...CacheOption) ([]byte, error)
GetOrCompute gets a value from cache, or computes and caches it if missing.
func (*CacheHelper) GetOrComputeJSON ¶
func (h *CacheHelper) GetOrComputeJSON(ctx context.Context, key string, compute func() (interface{}, error), out interface{}, opts ...CacheOption) error
GetOrComputeJSON gets a JSON value from cache, or computes and caches it if missing.
func (*CacheHelper) SetJSON ¶
func (h *CacheHelper) SetJSON(ctx context.Context, key string, value interface{}, opts ...CacheOption) error
SetJSON marshals a value to JSON and stores it in the cache.
type CacheOption ¶
type CacheOption func(*CacheOptions)
CacheOption is a functional option for CacheOptions.
func WithMetadata ¶
func WithMetadata(metadata map[string]string) CacheOption
WithMetadata sets metadata for cache entries.
func WithSkipCache ¶
func WithSkipCache(skip bool) CacheOption
WithSkipCache skips caching for this operation.
type CacheOptions ¶
type CacheOptions struct {
// TTL is the time-to-live for the cache entry
TTL time.Duration
// Tags are optional tags for grouping cache entries
Tags []string
// Metadata is optional metadata to store with the entry
Metadata map[string]string
// SkipCache indicates whether to skip caching for this operation
SkipCache bool
}
CacheOptions configures cache operations.
type CacheStats ¶
type CacheStats struct {
// Hits is the number of cache hits
Hits int64
// Misses is the number of cache misses
Misses int64
// Size is the current number of entries in the cache
Size int64
// Evictions is the number of evicted entries
Evictions int64
// Backend is the name of the cache backend
Backend string
}
CacheStats contains cache statistics.
func (*CacheStats) HitRatio ¶
func (s *CacheStats) HitRatio() float64
HitRatio returns the cache hit ratio (0.0 to 1.0).
type NoOpCache ¶
type NoOpCache struct{}
NoOpCache is a no-operation cache implementation. It satisfies the Cache interface but doesn't actually cache anything. This is the default implementation for backward compatibility.
func (*NoOpCache) DeleteByTag ¶
DeleteByTag implements TaggedCache.DeleteByTag (no-op).
func (*NoOpCache) GetStats ¶
func (c *NoOpCache) GetStats(ctx context.Context) (*CacheStats, error)
GetStats implements StatsProvider.GetStats (no-op).
func (*NoOpCache) ListKeysByTag ¶
ListKeysByTag implements TaggedCache.ListKeysByTag (no-op).
type StatsProvider ¶
type StatsProvider interface {
// GetStats returns cache statistics.
GetStats(ctx context.Context) (*CacheStats, error)
}
StatsProvider provides cache statistics.
type TaggedCache ¶
type TaggedCache interface {
Cache
// DeleteByTag removes all cache entries with the given tag.
DeleteByTag(ctx context.Context, tag string) error
// ListKeysByTag returns all keys with the given tag.
ListKeysByTag(ctx context.Context, tag string) ([]string, error)
}
TaggedCache extends Cache with tag-based operations.