postgres

package
v1.0.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPingFailed is returned if the initial ping to the database returns an error
	ErrPingFailed = errors.New("ping returned error")
)

Functions

This section is empty.

Types

type Cache

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

Cache implements the gocondcache.Cache interface using PostgreSQL as the storage backend. It provides thread-safe operations for storing and retrieving cached HTTP responses.

func New

func New(ctx context.Context, db *sql.DB, config *Config) (*Cache, error)

New creates a new PostgreSQL cache instance with the provided configuration. It verifies the database connection, creates the necessary table structure, and optionally starts the cleanup task for expired items.

Returns an error if: - The database connection test fails - Table creation fails - Configuration validation fails

func (*Cache) Get

func (p *Cache) Get(ctx context.Context, k string) (*gocondcache.CacheItem, error)

Get retrieves a cache item from PostgreSQL by its key. It returns the cached item if found and not expired, or an appropriate error otherwise. Returns caches.ErrNoCacheItem if the item doesn't exist.

func (*Cache) Set

func (p *Cache) Set(ctx context.Context, k string, v *gocondcache.CacheItem) error

Set stores a new cache item in PostgreSQL with the provided key and value. It handles the serialization of the cache item using gob encoding.

func (*Cache) Update

func (bc *Cache) Update(ctx context.Context, key string, expiration time.Time) error

Update modifies the expiration time of an existing cache item in PostgreSQL. This is typically used when a cached response is revalidated with the origin server

type Config

type Config struct {
	// DeleteExpiredItems enables automatic cleanup of expired cache entries
	// through a background task.
	DeleteExpiredItems bool

	// ExpiredTaskTimer defines the interval at which the cleanup task runs.
	// Shorter durations may impact database performance.
	ExpiredTaskTimer time.Duration

	// ItemExpiration defines how long items remain valid in the database.
	// This is separate from the expiration time derived from conditional response headers.
	ItemExpiration time.Duration
}

Config defines the configuration options for the PostgreSQL cache implementation.

Jump to

Keyboard shortcuts

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