postgres

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: Apache-2.0, MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package postgres is a cache.Cache backed by a Postgres table, for apps that want a shared cache without running Redis. Entries expire by TTL; Sweep removes the dead rows.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SchemaSQL

func SchemaSQL() string

SchemaSQL returns the DDL for the cache table. It is safe to run again. The table is UNLOGGED: cache contents are rebuildable, and skipping the write-ahead log makes writes much cheaper. They are lost after a crash and not copied to replicas.

Types

type Cache

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

Cache is a Postgres-backed cache.Cache.

func New

func New(db DB, opts ...Option) *Cache

New returns a Cache over db, usually a *pgxpool.Pool. Create the table with SchemaSQL first. It does no I/O.

func (*Cache) Delete

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

Delete implements cache.Cache.

func (*Cache) Describe

func (c *Cache) Describe() cache.Description

Describe implements cache.Cache.

func (*Cache) Get

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

Get implements cache.Cache.

func (*Cache) Set

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

Set implements cache.Cache. A ttl of zero or less never expires.

func (*Cache) Sweep

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

Sweep deletes expired entries, for every prefix, and returns how many it removed. Run it every few minutes.

type DB

type DB interface {
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
	Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
}

DB is the subset of *pgxpool.Pool (and pgx.Conn, pgx.Tx) the cache uses.

type Option

type Option func(*Cache)

Option configures a Cache.

func Clock

func Clock(now func() time.Time) Option

Clock sets the time source for expiry. Default time.Now. Expiry is judged by the application's clock, so instances with very different clocks see entries expire at different moments.

func Prefix

func Prefix(p string) Option

Prefix is prepended to every key, so several caches can share the table.

Jump to

Keyboard shortcuts

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