postgrescache

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package postgrescache is a pgx-backed cache that satisfies cache.Cache. Opt-in: importing this package pulls in pgx/v5; the root cache package has no driver dependency.

Stores values as BYTEA in a table (default name: fc_cache). Reads filter on expires_at > NOW() so an expired row is invisible even before it's reaped; writes upsert on the primary key so callers can refresh the TTL by writing again.

Call InitSchema once at startup (or fold the SQL into your own migrations) before the first Get / Set.

Index

Constants

View Source
const CreateTableSQL = `` /* 205-byte string literal not displayed */

CreateTableSQL is the schema for the cache table — substitute {table} for the actual name. Indexed on expires_at to make reaping cheap.

View Source
const DefaultTable = "fc_cache"

DefaultTable is the cache table name when New is used.

Variables

This section is empty.

Functions

func InitSchema

func InitSchema(ctx context.Context, pool *pgxpool.Pool) error

InitSchema creates DefaultTable. Safe to run repeatedly.

func InitSchemaWithTable

func InitSchemaWithTable(ctx context.Context, pool *pgxpool.Pool, table string) error

InitSchemaWithTable creates the cache table with a custom name.

Types

type Cache

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

Cache is a Postgres-backed implementation of cache.Cache.

func New

func New(pool *pgxpool.Pool) *Cache

New builds a cache against pool using DefaultTable.

func WithTable

func WithTable(pool *pgxpool.Pool, table string) *Cache

WithTable overrides the table name (for multi-tenant deployments that need separate cache tables per service).

func (*Cache) Delete

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

Delete — see cache.Cache.Delete.

func (*Cache) GetBytes

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

GetBytes — see cache.Cache.GetBytes.

func (*Cache) ReapExpired

func (c *Cache) ReapExpired(ctx context.Context) (int64, error)

ReapExpired removes rows whose TTL has elapsed and returns the number removed. Cheap thanks to the index on expires_at.

func (*Cache) SetBytes

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

SetBytes — see cache.Cache.SetBytes.

Jump to

Keyboard shortcuts

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