cache

package
v0.0.27 Latest Latest
Warning

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

Go to latest
Published: May 28, 2026 License: MIT Imports: 14 Imported by: 0

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

func NewFile(dir string, ttl time.Duration) (*FileCache, error)

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

func (c *FileCache) Close() error

Close is a no-op for the file cache.

func (*FileCache) Delete

func (c *FileCache) Delete(_ context.Context, key string) error

Delete removes the cached file for key. Returns nil if key does not exist.

func (*FileCache) Get

func (c *FileCache) Get(_ context.Context, key string) ([]byte, bool)

Get retrieves the cached bytes for key. Returns (nil, false) on miss or when the file is older than the TTL. Expired files are deleted lazily.

func (*FileCache) Set

func (c *FileCache) Set(_ context.Context, key string, value []byte, _ time.Duration) error

Set writes value to disk, overwriting any existing file for key. The TTL parameter is stored implicitly via the file's mtime.

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

func (c *MemoryCache) Get(_ context.Context, key string) ([]byte, bool)

Get retrieves a cached value. A miss or an expired entry both return (nil, false). On expiry the entry is deleted.

func (*MemoryCache) Set

func (c *MemoryCache) Set(_ context.Context, key string, value []byte, ttl time.Duration) error

Set stores value under key with the given TTL. If maxSize is reached the least-recently-used entry is evicted first. Overwriting an existing key updates the value and moves it to the front of the LRU list.

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

func (c *RedisCache) Get(ctx context.Context, key string) ([]byte, bool)

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.

func (*RedisCache) Set

func (c *RedisCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

Set stores value under key with the given TTL. A TTL of zero stores the entry without an expiration time (it persists until explicitly deleted or Redis is flushed). 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

func (c *SQLiteCache) Get(ctx context.Context, key string) ([]byte, bool)

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.

func (*SQLiteCache) Set

func (c *SQLiteCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

Set stores value under key with the given TTL. A TTL of zero means the entry never expires (expires_at stored as 0). An existing entry with the same key is replaced (INSERT OR REPLACE).

Jump to

Keyboard shortcuts

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