Documentation
¶
Overview ¶
Package cache provides the cache service interface for AI agents. This interface is consumed by Team B (Assistant+Schedule) and Team C (Memo Enhancement).
Package cache provides semantic caching for AI agents. Issue #91: 语义缓存层实现 - 基于 Embedding 相似度匹配
Index ¶
- type CacheService
- type EmbeddingService
- type LRUCache
- type MockCacheService
- func (m *MockCacheService) Clear()
- func (m *MockCacheService) Get(ctx context.Context, key string) ([]byte, bool)
- func (m *MockCacheService) Invalidate(ctx context.Context, pattern string) error
- func (m *MockCacheService) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- func (m *MockCacheService) Size() int
- type SemanticCache
- type SemanticCacheConfig
- type SemanticCacheStats
- type Service
- func (s *Service) Clear()
- func (s *Service) Close()
- func (s *Service) Get(_ context.Context, key string) ([]byte, bool)
- func (s *Service) Invalidate(_ context.Context, pattern string) error
- func (s *Service) Set(_ context.Context, key string, value []byte, ttl time.Duration) error
- func (s *Service) Size() int
- type ServiceConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CacheService ¶
type CacheService interface {
// Get retrieves a value from cache.
// Returns: value, whether it exists
Get(ctx context.Context, key string) ([]byte, bool)
// Set stores a value in cache.
// ttl: expiration time
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
// Invalidate invalidates cache entries.
// pattern: supports wildcards (user:123:*)
Invalidate(ctx context.Context, pattern string) error
}
Consumers: Team B (Assistant+Schedule), Team C (Memo Enhancement).
type EmbeddingService ¶ added in v0.94.0
type EmbeddingService interface {
// Embed generates a vector embedding for the given text.
Embed(ctx context.Context, text string) ([]float32, error)
// EmbedBatch generates vector embeddings for multiple texts.
EmbedBatch(ctx context.Context, texts []string) ([][]float32, error)
}
EmbeddingService defines the interface for generating vector embeddings. This is a local interface to avoid circular dependencies.
type LRUCache ¶
type LRUCache struct {
// contains filtered or unexported fields
}
LRUCache implements an LRU cache with TTL support.
func NewLRUCache ¶
NewLRUCache creates a new LRU cache.
func (*LRUCache) CleanupExpired ¶
CleanupExpired removes all expired entries. Returns the number of entries removed.
func (*LRUCache) Invalidate ¶
Invalidate removes entries matching the pattern. Supports * wildcard at the end (e.g., "user:123:*").
type MockCacheService ¶
type MockCacheService struct {
// contains filtered or unexported fields
}
MockCacheService is a mock implementation of CacheService for testing.
func NewMockCacheService ¶
func NewMockCacheService() *MockCacheService
NewMockCacheService creates a new MockCacheService.
func (*MockCacheService) Clear ¶
func (m *MockCacheService) Clear()
Clear removes all items from the cache (for testing).
func (*MockCacheService) Invalidate ¶
func (m *MockCacheService) Invalidate(ctx context.Context, pattern string) error
Invalidate invalidates cache entries.
func (*MockCacheService) Set ¶
func (m *MockCacheService) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
Set stores a value in cache.
func (*MockCacheService) Size ¶
func (m *MockCacheService) Size() int
Size returns the number of items in the cache (for testing).
type SemanticCache ¶ added in v0.94.0
type SemanticCache struct {
// contains filtered or unexported fields
}
SemanticCache provides two-layer caching: exact match (SHA256) and semantic match (cosine similarity).
func NewSemanticCache ¶ added in v0.94.0
func NewSemanticCache(cfg SemanticCacheConfig) *SemanticCache
NewSemanticCache creates a new semantic cache.
func (*SemanticCache) Clear ¶ added in v0.94.0
func (c *SemanticCache) Clear()
Clear clears all cache entries.
func (*SemanticCache) Get ¶ added in v0.94.0
Get retrieves a cached embedding. Returns (embedding, found, similarity, isExactMatch).
func (*SemanticCache) GetStats ¶ added in v0.94.0
func (c *SemanticCache) GetStats() SemanticCacheStats
GetStats returns the cache statistics.
type SemanticCacheConfig ¶ added in v0.94.0
type SemanticCacheConfig struct {
// MaxEntries is the maximum number of entries in the cache.
MaxEntries int
// SimilarityThreshold is the minimum cosine similarity for a match (0-1).
SimilarityThreshold float32
// TTL is the time-to-live for cache entries.
TTL time.Duration
// EmbeddingService is the vector embedding service.
EmbeddingService EmbeddingService
}
SemanticCacheConfig configures the semantic cache.
func DefaultSemanticCacheConfig ¶ added in v0.94.0
func DefaultSemanticCacheConfig() SemanticCacheConfig
DefaultSemanticCacheConfig returns the default configuration.
type SemanticCacheStats ¶ added in v0.94.0
type SemanticCacheStats struct {
ExactHits int64
ExactMisses int64
SemanticHits int64
SemanticMisses int64
SemanticSize int
SimilarityDistribution map[string]int64
}
SemanticCacheStats represents cache statistics.
func (SemanticCacheStats) MarshalJSON ¶ added in v0.94.0
func (s SemanticCacheStats) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler for stats.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service implements CacheService with LRU eviction.
func NewService ¶
func NewService(cfg ServiceConfig) *Service
NewService creates a new cache service.
func (*Service) Invalidate ¶
Invalidate invalidates cache entries matching the pattern.
type ServiceConfig ¶
type ServiceConfig struct {
Capacity int // Maximum number of entries (default: 1000)
DefaultTTL time.Duration // Default TTL for entries (default: 5 minutes)
CleanupInterval time.Duration // Interval for expired entry cleanup (default: 1 minute)
}
ServiceConfig configures the cache service.
func DefaultServiceConfig ¶
func DefaultServiceConfig() ServiceConfig
DefaultServiceConfig returns default cache service configuration.