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
- func InitSchema(ctx context.Context, pool *pgxpool.Pool) error
- func InitSchemaWithTable(ctx context.Context, pool *pgxpool.Pool, table string) error
- type Cache
- func (c *Cache) Delete(ctx context.Context, key string) error
- func (c *Cache) GetBytes(ctx context.Context, key string) ([]byte, bool, error)
- func (c *Cache) ReapExpired(ctx context.Context) (int64, error)
- func (c *Cache) SetBytes(ctx context.Context, key string, value []byte, ttl time.Duration) error
Constants ¶
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.
const DefaultTable = "fc_cache"
DefaultTable is the cache table name when New is used.
Variables ¶
This section is empty.
Functions ¶
func InitSchema ¶
InitSchema creates DefaultTable. Safe to run repeatedly.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a Postgres-backed implementation of cache.Cache.
func WithTable ¶
WithTable overrides the table name (for multi-tenant deployments that need separate cache tables per service).
func (*Cache) ReapExpired ¶
ReapExpired removes rows whose TTL has elapsed and returns the number removed. Cheap thanks to the index on expires_at.