cache

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: MIT Imports: 9 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetJSON

func GetJSON[T any](c Cache, ctx context.Context, key string) (T, bool, error)

GetJSON retrieves a value from the cache and unmarshals it into the target type.

func HTTPCache

func HTTPCache(maxAge int) func(http.Handler) http.Handler

HTTPCache returns middleware that sets Cache-Control headers. maxAge is the duration in seconds that the response may be cached.

func NoCache

func NoCache() func(http.Handler) http.Handler

NoCache returns middleware that sets headers to prevent caching.

func SetJSON

func SetJSON(c Cache, ctx context.Context, key string, value any, ttl time.Duration) error

SetJSON marshals the value to JSON and stores it in the cache.

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.

func (*MemoryCache) Delete

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

func (*MemoryCache) Flush

func (c *MemoryCache) Flush(_ context.Context) error

func (*MemoryCache) Get

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

func (*MemoryCache) Set

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

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

func NewSQLiteCache(db *sql.DB, cleanupInterval time.Duration) (*SQLiteCache, error)

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.

func (*SQLiteCache) Delete

func (c *SQLiteCache) Delete(ctx context.Context, key string) error

func (*SQLiteCache) Flush

func (c *SQLiteCache) Flush(ctx context.Context) error

func (*SQLiteCache) Get

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

func (*SQLiteCache) Set

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

Jump to

Keyboard shortcuts

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