query

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrQueryNotFound indicates query result not found in cache.
	ErrQueryNotFound = errors.New("query result not found in cache")
)

Functions

This section is empty.

Types

type CacheStats

type CacheStats struct {
	Hits   atomic.Int64
	Misses atomic.Int64
}

CacheStats represents cache statistics.

func (*CacheStats) HitRate

func (s *CacheStats) HitRate() float64

HitRate returns the cache hit rate.

func (*CacheStats) Reset

func (s *CacheStats) Reset()

Reset resets all statistics to zero.

type MemoryQueryCache

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

MemoryQueryCache provides in-memory caching for query results.

func NewMemoryQueryCache

func NewMemoryQueryCache() *MemoryQueryCache

NewMemoryQueryCache creates a new in-memory query cache. The cleanup goroutine runs periodically to remove expired items.

func (*MemoryQueryCache) Clear

func (m *MemoryQueryCache) Clear()

Clear removes all items from cache.

func (*MemoryQueryCache) Close

func (m *MemoryQueryCache) Close()

Close stops the cleanup goroutine and cleans up resources. This should be called when the cache is no longer needed to prevent goroutine leaks.

func (*MemoryQueryCache) Delete

func (m *MemoryQueryCache) Delete(key string)

Delete removes search results from cache.

func (*MemoryQueryCache) Get

func (m *MemoryQueryCache) Get(key string) ([]*SearchResult, bool)

Get retrieves search results from cache.

func (*MemoryQueryCache) Len

func (m *MemoryQueryCache) Len() int

Len returns the number of items in cache.

func (*MemoryQueryCache) Set

func (m *MemoryQueryCache) Set(key string, results []*SearchResult, ttl time.Duration)

Set stores search results in cache.

type QueryCache

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

QueryCache provides caching for query results to bypass DB and embedding calls. It supports both Redis and in-memory cache with automatic fallback.

func NewQueryCache

func NewQueryCache(redisClient RedisClient, ttl time.Duration) *QueryCache

NewQueryCache creates a new query cache. redisClient is optional. If nil, it will use in-memory cache only.

func (*QueryCache) Clear

func (c *QueryCache) Clear(ctx context.Context) error

Clear removes all query results from cache.

func (*QueryCache) Delete

func (c *QueryCache) Delete(ctx context.Context, req *SearchRequest) error

Delete removes a query result from cache.

func (*QueryCache) Disable

func (c *QueryCache) Disable()

Disable disables the cache.

func (*QueryCache) Enable

func (c *QueryCache) Enable()

Enable enables the cache.

func (*QueryCache) Get

func (c *QueryCache) Get(ctx context.Context, req *SearchRequest) ([]*SearchResult, error)

Get retrieves cached search results.

func (*QueryCache) GetStats

func (c *QueryCache) GetStats() *CacheStats

GetStats returns cache statistics.

func (*QueryCache) IsEnabled

func (c *QueryCache) IsEnabled() bool

IsEnabled returns whether the cache is enabled.

func (*QueryCache) Set

func (c *QueryCache) Set(ctx context.Context, req *SearchRequest, results []*SearchResult) error

Set stores search results in cache.

type RedisClient

type RedisClient interface {
	Get(ctx context.Context, key string) (string, error)
	Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
	Del(ctx context.Context, keys ...string) error
	Keys(ctx context.Context, pattern string) ([]string, error)
}

RedisClient defines the interface for Redis operations.

type SearchRequest

type SearchRequest struct {
	Query    string
	TenantID string
	Filters  map[string]interface{}
	TopK     int
}

SearchRequest represents a search request.

type SearchResult

type SearchResult struct {
	ID       string
	Content  string
	Source   string
	Score    float64
	Metadata map[string]interface{}
}

SearchResult represents a search result from retrieval.

Jump to

Keyboard shortcuts

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