Help us shape the future of Go! Take the 2026 Go Developer Survey

postgres

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidDSN is returned when DSN is empty.
	ErrInvalidDSN = errors.New("postgres: DSN must not be empty")

	// ErrInvalidDB is returned when DB is nil.
	ErrInvalidDB = errors.New("postgres: DB must not be nil")

	// ErrInvalidMaxOpenConns is returned when MaxOpenConns is < 0.
	ErrInvalidMaxOpenConns = errors.New("postgres: MaxOpenConns must be >= 0")

	// ErrInvalidMaxIdleConns is returned when MaxIdleConns is < 0.
	ErrInvalidMaxIdleConns = errors.New("postgres: MaxIdleConns must be >= 0")

	// ErrInvalidCleanupBatchSize is returned when CleanupBatchSize is < 0.
	ErrInvalidCleanupBatchSize = errors.New("postgres: CleanupBatchSize must be >= 0")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// DB is the database connection (REQUIRED, must not be nil).
	// The caller is responsible for opening and configuring the connection.
	DB *sql.DB

	// DSN is the PostgreSQL connection string (REQUIRED if DB is nil, must not be empty).
	// Example: "postgres://user:password@localhost:5432/dbname?sslmode=disable"
	// Ignored if DB is provided.
	DSN string

	// TableName is the name of the cache table (OPTIONAL).
	// Default: "cache_entries"
	TableName string

	// MaxOpenConns is the maximum number of open connections to the database (OPTIONAL).
	// Default: 25
	// Set to 0 for unlimited connections.
	// Only used if DSN is provided (ignored if DB is provided).
	MaxOpenConns int

	// MaxIdleConns is the maximum number of idle connections in the pool (OPTIONAL).
	// Default: 5
	// Only used if DSN is provided (ignored if DB is provided).
	MaxIdleConns int

	// ConnMaxLifetime is the maximum lifetime of a connection (OPTIONAL).
	// Default: 5 minutes
	// Set to 0 for no maximum lifetime.
	// Only used if DSN is provided (ignored if DB is provided).
	ConnMaxLifetime time.Duration

	// CleanupInterval is the interval for cleaning up expired entries (OPTIONAL).
	// Default: 1 minute
	// Set to negative value to disable automatic cleanup.
	CleanupInterval time.Duration

	// CleanupBatchSize is the maximum number of entries to delete per cleanup batch (OPTIONAL).
	// Default: 1000
	// Set to 0 for unlimited batch size (delete all expired entries at once).
	CleanupBatchSize int
}

Config holds configuration for PostgresStore.

type PostgresStore

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

PostgresStore is a PostgreSQL-backed cache store. Thread-safe for concurrent use with connection pooling.

Reference: .references/gookit-cache/boltdb/boltdb.go (embedded database operations)

func New

func New(cfg Config) (*PostgresStore, error)

New creates a new PostgresStore with the given configuration.

func (*PostgresStore) Clear

func (s *PostgresStore) Clear(ctx context.Context) error

Clear removes all entries from PostgreSQL.

func (*PostgresStore) Close

func (s *PostgresStore) Close() error

Close releases resources associated with the PostgreSQL database. Idempotent - safe to call multiple times. Only closes the database connection if it was opened by New() (not provided by user).

func (*PostgresStore) Delete

func (s *PostgresStore) Delete(ctx context.Context, key string) error

Delete removes a value from PostgreSQL. Idempotent - deleting a non-existent key returns nil.

func (*PostgresStore) Get

func (s *PostgresStore) Get(ctx context.Context, key string) ([]byte, error)

Get retrieves a value from PostgreSQL by key. Returns cache.ErrNotFound if the key doesn't exist or has expired.

func (*PostgresStore) Set

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

Set stores a value in PostgreSQL with the specified TTL.

TTL semantics: - ttl == 0: no expiration - ttl < 0: no expiration - ttl > 0: expires after the specified duration

Jump to

Keyboard shortcuts

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