Documentation
¶
Overview ¶
Package cache provides caching backends for generated applications. It defines the Cache interface and provides MemoryCache (LRU+TTL) and SQLiteCache (persistent) implementations.
Index ¶
- func GetJSON[T any](c Cache, ctx context.Context, key string) (T, bool, error)
- func HTTPCache(maxAge int) func(http.Handler) http.Handler
- func NoCache() func(http.Handler) http.Handler
- func SetJSON(c Cache, ctx context.Context, key string, value any, ttl time.Duration) error
- type Cache
- type Closeable
- type MemoryCache
- func (c *MemoryCache) Close() error
- func (c *MemoryCache) Delete(_ context.Context, key string) error
- func (c *MemoryCache) Flush(_ context.Context) error
- func (c *MemoryCache) Get(_ context.Context, key string) ([]byte, bool, error)
- func (c *MemoryCache) Set(_ context.Context, key string, value []byte, ttl time.Duration) error
- type SQLiteCache
- func (c *SQLiteCache) Close() error
- func (c *SQLiteCache) Delete(ctx context.Context, key string) error
- func (c *SQLiteCache) Flush(ctx context.Context) error
- func (c *SQLiteCache) Get(ctx context.Context, key string) ([]byte, bool, error)
- func (c *SQLiteCache) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HTTPCache ¶
HTTPCache returns middleware that sets Cache-Control headers. maxAge is the duration in seconds that the response may be cached.
Types ¶
type Cache ¶
type Cache interface {
Get(ctx context.Context, key string) ([]byte, bool, error)
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
Delete(ctx context.Context, key string) error
Flush(ctx context.Context) error
}
Cache is the interface for cache backends. Get returns (value, found, error). A nil error with found=false means cache miss.
type Closeable ¶
type Closeable interface {
Close() error
}
Closeable is optionally implemented by caches that need cleanup (e.g., stop background goroutines).
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache is an in-memory cache with LRU eviction and TTL expiration.
func NewMemoryCache ¶
func NewMemoryCache(maxEntries int, cleanupInterval time.Duration) *MemoryCache
NewMemoryCache creates an in-memory cache with the given capacity. A background goroutine cleans up expired entries at cleanupInterval. Call Close() to stop the cleanup goroutine.
func (*MemoryCache) Close ¶
func (c *MemoryCache) Close() error
Close stops the background cleanup goroutine.
type SQLiteCache ¶
type SQLiteCache struct {
// contains filtered or unexported fields
}
SQLiteCache is a cache backed by a SQLite database table. Data persists across application restarts.
func NewSQLiteCache ¶
NewSQLiteCache creates a persistent cache using the given SQLite database. It creates the _cache table if it doesn't exist. A background goroutine cleans up expired entries at cleanupInterval.
func (*SQLiteCache) Close ¶
func (c *SQLiteCache) Close() error
Close stops the background cleanup goroutine.