Documentation
¶
Overview ¶
Package cache provides caching backends for Foxhound responses.
Two implementations are provided:
- MemoryCache: in-process LRU cache with TTL expiry.
- FileCache: disk-based cache using SHA256-hashed filenames.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
// Get retrieves a cached value by key. Returns (value, true) on hit,
// (nil, false) on miss or TTL expiry.
Get(ctx context.Context, key string) ([]byte, bool)
// Set stores value under key with the given TTL.
// A TTL of zero means the entry never expires.
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
// Delete removes a cached entry. Returns nil if the key does not exist.
Delete(ctx context.Context, key string) error
// Close releases any resources held by the cache.
Close() error
}
Cache stores and retrieves cached responses.
type FileCache ¶
type FileCache struct {
// contains filtered or unexported fields
}
FileCache stores cached responses as raw byte files on disk.
File naming:
- Each key is hashed with SHA-256 and the hex digest is used as the filename, preventing path-traversal and collisions from arbitrary keys.
TTL strategy:
- The file modification time (mtime) is compared against ttl on each Get.
- Set always overwrites the file and resets mtime via os.WriteFile.
func NewFile ¶
NewFile creates a FileCache that stores files in dir with the given default TTL. dir is created (with all parents) if it does not already exist. A ttl of zero means entries never expire.
func (*FileCache) Delete ¶
Delete removes the cached file for key. Returns nil if key does not exist.
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache is a thread-safe, in-memory LRU cache with per-entry TTL.
Eviction policy:
- When the cache is at maxSize capacity and a new key is inserted, the least-recently-used item is removed.
- Expired entries are lazily removed on Get.
func NewMemory ¶
func NewMemory(maxSize int) *MemoryCache
NewMemory returns a MemoryCache that holds at most maxSize items. maxSize must be > 0; if <= 0 it defaults to 128.
func (*MemoryCache) Close ¶
func (c *MemoryCache) Close() error
Close is a no-op for the in-memory cache; it satisfies the Cache interface.
func (*MemoryCache) Delete ¶
func (c *MemoryCache) Delete(_ context.Context, key string) error
Delete removes the entry for key. Returns nil if key does not exist.
func (*MemoryCache) Get ¶
Get retrieves a cached value. A miss or an expired entry both return (nil, false). On expiry the entry is deleted.
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache implements Cache using Redis. It is safe for concurrent use.
func NewRedis ¶
func NewRedis(addr, password string, db int, prefix string) (*RedisCache, error)
NewRedis creates a RedisCache that connects to Redis at addr. password may be empty. db selects the Redis logical database (0–15). prefix namespaces all keys to avoid collisions with other data stored in the same Redis instance.
Returns an error if the client cannot be created (e.g., invalid addr format).
func NewRedisFromClient ¶
func NewRedisFromClient(client *redis.Client, prefix string) *RedisCache
NewRedisFromClient wraps an existing *redis.Client. Use this when you need to share a connection pool across multiple components or configure advanced options (TLS, sentinel, cluster) on the client yourself.
func (*RedisCache) Close ¶
func (c *RedisCache) Close() error
Close releases the underlying Redis connection pool.
func (*RedisCache) Delete ¶
func (c *RedisCache) Delete(ctx context.Context, key string) error
Delete removes a cached entry. Returns nil if the key does not exist. The key is automatically prefixed.
func (*RedisCache) Get ¶
Get retrieves a cached value by key. Returns (value, true) on hit and (nil, false) on miss or TTL expiry. The key is automatically prefixed.
type SQLiteCache ¶
type SQLiteCache struct {
// contains filtered or unexported fields
}
SQLiteCache implements Cache using SQLite for persistent caching. It is safe for concurrent use; SQLite serialises writes internally.
func NewSQLite ¶
func NewSQLite(dbPath string) (*SQLiteCache, error)
NewSQLite opens (or creates) a SQLite database at dbPath and ensures the cache table exists. Returns an error if the file cannot be opened or the schema cannot be applied.
func (*SQLiteCache) Close ¶
func (c *SQLiteCache) Close() error
Close releases the underlying database connection pool.
func (*SQLiteCache) Delete ¶
func (c *SQLiteCache) Delete(ctx context.Context, key string) error
Delete removes a cached entry. Returns nil if the key does not exist.
func (*SQLiteCache) Get ¶
Get retrieves a cached value by key. Returns (value, true) on hit and (nil, false) on miss, TTL expiry, or any internal error.
Expired entries are treated as misses; they are not deleted eagerly here to avoid write amplification — they are overwritten on the next Set call.