pgxcache

package module
v0.0.0-...-6c03349 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2025 License: MIT Imports: 14 Imported by: 3

README

pgxcache

A caching layer for pgx. It's heavy inspired by sqlcache.

Go Reference

Getting Started

You can use these examples to get started.

Disclaimer

It's highly experimental and not used in production yet. It's a work in progress and the API can change at any time.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BatchQuerier

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

BatchQuerier is a wrapper around pgx.BatchResults that records the batch results.

func (*BatchQuerier) Close

func (b *BatchQuerier) Close() error

Close implements pgx.BatchResults.

func (*BatchQuerier) Exec

func (b *BatchQuerier) Exec() (pgconn.CommandTag, error)

Exec implements pgx.BatchResults.

func (*BatchQuerier) Query

func (b *BatchQuerier) Query() (pgx.Rows, error)

Query implements pgx.BatchResults.

func (*BatchQuerier) QueryRow

func (b *BatchQuerier) QueryRow() pgx.Row

QueryRow implements pgx.BatchResults.

type BatchRecorder

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

BatchRecorder is a wrapper around pgx.BatchResults that records the batch results.

func (*BatchRecorder) Close

func (b *BatchRecorder) Close() error

Close implements pgx.BatchResults.

func (*BatchRecorder) Exec

func (b *BatchRecorder) Exec() (pgconn.CommandTag, error)

Exec implements pgx.BatchResults.

func (*BatchRecorder) Query

func (b *BatchRecorder) Query() (pgx.Rows, error)

Query implements pgx.BatchResults.

func (*BatchRecorder) QueryRow

func (b *BatchRecorder) QueryRow() pgx.Row

QueryRow implements pgx.BatchResults.

type MemoryQueryCacher

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

MemoryQueryCacher is a simple in-memory cache implementation.

func NewMemoryQueryCacher

func NewMemoryQueryCacher() *MemoryQueryCacher

NewMemoryQueryCacher creates a new MemoryCacher.

func (*MemoryQueryCacher) Get

Get implements Cacher.

func (*MemoryQueryCacher) Reset

func (x *MemoryQueryCacher) Reset(_ context.Context) error

Reset resets the cache.

func (*MemoryQueryCacher) Set

func (x *MemoryQueryCacher) Set(_ context.Context, key *QueryKey, item *QueryItem, lifetime time.Duration) error

Set implements Cacher.

type Querier

type Querier struct {
	// Options is the cache options.
	Options *QueryOptions
	// Querier is the query executor.
	Querier Queryable
	// Cacher is the cache store.
	Cacher QueryCacher
}

Querier is a wrapper around pgx.Conn that caches prepared statements.

Example
config, err := pgxpool.ParseConfig(os.Getenv("PGX_DATABASE_URL"))
if err != nil {
	panic(err)
}

// create a new connection pool
conn, err := pgxpool.NewWithConfig(context.TODO(), config)
if err != nil {
	panic(err)
}
// close the connection when the function returns
defer conn.Close()

// create a new querier
querier := &pgxcache.Querier{
	// set the default query options, which can be overridden by the query
	// -- @cache-min-rows 1
	// -- @cache-max-rows 100
	// -- @cache-max-lifetime 30s
	Options: &pgxcache.QueryOptions{
		MinRows:     1,
		MaxRows:     100,
		MaxLifetime: 30 * time.Second,
	},
	Cacher:  pgxcache.NewMemoryQueryCacher(),
	Querier: conn,
}

// create a new organization struct
type Organization struct {
	Name string `db:"name"`
}

// fetch all the organizations
rows, err := querier.Query(context.TODO(), "SELECT * FROM organization")
if err != nil {
	panic(err)
}
// close the rows when the function returns
defer rows.Close()

for rows.Next() {
	organization, err := pgx.RowToStructByName[Organization](rows)
	if err != nil {
		panic(err)
	}

	fmt.Println(organization.Name)
}
Output:

func (*Querier) Begin

func (x *Querier) Begin(ctx context.Context) (pgx.Tx, error)

Begin implements Queryable.

func (*Querier) Close

func (x *Querier) Close()

Close implements Queryable.

func (*Querier) CopyFrom

func (x *Querier) CopyFrom(ctx context.Context, table pgx.Identifier, columns []string, source pgx.CopyFromSource) (int64, error)

CopyFrom implements Queryable.

func (*Querier) Exec

func (x *Querier) Exec(ctx context.Context, query string, args ...any) (pgconn.CommandTag, error)

Exec is a helper that makes it easy to execute a query that doesn't return rows.

func (*Querier) Query

func (x *Querier) Query(ctx context.Context, query string, args ...any) (pgx.Rows, error)

Query is a helper that makes it easy to query for multiple rows.

func (*Querier) QueryRow

func (x *Querier) QueryRow(ctx context.Context, query string, args ...any) pgx.Row

QueryRow is a helper that makes it easy to query for a single row.

func (*Querier) SendBatch

func (x *Querier) SendBatch(ctx context.Context, batch *pgx.Batch) pgx.BatchResults

SendBatch is a helper that makes it easy to send a batch of queries.

type QuerierTx

type QuerierTx struct {
	// Querier is the query executor.
	Tx pgx.Tx
	// Options is the cache options.
	Options *QueryOptions
	// Cacher is the cache store.
	Cacher QueryCacher
}

Querier is a wrapper around pgx.Conn that caches prepared statements.

func (*QuerierTx) Begin

func (q *QuerierTx) Begin(ctx context.Context) (pgx.Tx, error)

Begin implements pgx.Tx.

func (*QuerierTx) Commit

func (q *QuerierTx) Commit(ctx context.Context) error

Commit implements pgx.Tx.

func (*QuerierTx) Conn

func (q *QuerierTx) Conn() *pgx.Conn

Conn implements pgx.Tx.

func (*QuerierTx) CopyFrom

func (q *QuerierTx) CopyFrom(ctx context.Context, table pgx.Identifier, columns []string, source pgx.CopyFromSource) (int64, error)

CopyFrom implements pgx.Tx.

func (*QuerierTx) Exec

func (q *QuerierTx) Exec(ctx context.Context, query string, args ...any) (commandTag pgconn.CommandTag, err error)

Exec implements pgx.Tx.

func (*QuerierTx) LargeObjects

func (q *QuerierTx) LargeObjects() pgx.LargeObjects

LargeObjects implements pgx.Tx.

func (*QuerierTx) Prepare

func (q *QuerierTx) Prepare(ctx context.Context, name string, query string) (*pgconn.StatementDescription, error)

Prepare implements pgx.Tx.

func (*QuerierTx) Query

func (q *QuerierTx) Query(ctx context.Context, query string, args ...any) (pgx.Rows, error)

Query implements pgx.Tx.

func (*QuerierTx) QueryRow

func (q *QuerierTx) QueryRow(ctx context.Context, query string, args ...any) pgx.Row

QueryRow implements pgx.Tx.

func (*QuerierTx) Queryable

func (q *QuerierTx) Queryable() Queryable

Queryable returns the query executor.

func (*QuerierTx) Rollback

func (q *QuerierTx) Rollback(ctx context.Context) error

Rollback implements pgx.Tx.

func (*QuerierTx) SendBatch

func (q *QuerierTx) SendBatch(ctx context.Context, batch *pgx.Batch) pgx.BatchResults

SendBatch implements pgx.Tx.

type QueryCacher

type QueryCacher interface {
	// Get must return a pointer to the item, a boolean representing whether
	// item is present or not, and an error (must be nil when key is not
	// present).
	Get(context.Context, *QueryKey) (*QueryItem, error)
	// Set sets the item into cache with the given TTL.
	Set(context.Context, *QueryKey, *QueryItem, time.Duration) error
	// Reset resets the cache
	Reset(context.Context) error
}

QueryCacher represents a backend cache that can be used by sqlcache package.

type QueryItem

type QueryItem struct {
	// CommandTag is the command tag returned by the query.
	CommandTag string
	// Fields is the field descriptions of the query result.
	Fields []pgconn.FieldDescription
	// Rows is the query result.
	Rows [][][]byte
}

QueryItem represents a query result.

func (*QueryItem) MarshalText

func (q *QueryItem) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*QueryItem) UnmarshalText

func (q *QueryItem) UnmarshalText(data []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type QueryKey

type QueryKey struct {
	// SQL is the SQL query.
	SQL string
	// Args are the arguments to the query.
	Args []any
}

QueryKey is a unique identifier for a query.

func (*QueryKey) String

func (x *QueryKey) String() string

String returns a string representation of the query key.

type QueryOptions

type QueryOptions struct {
	// MinRows is the minimum number of rows that the query should return.
	MinRows int
	// MaxRows is the maximum number of rows that the query should return.
	MaxRows int
	// MaxLifetime is the duration that the query result should be cached.
	MaxLifetime time.Duration
}

QueryOptions represents the options that can be specified in a SQL query.

func ParseQueryOptions

func ParseQueryOptions(query string) (*QueryOptions, error)

ParseQueryOptions parses query options from a SQL query.

type Queryable

type Queryable interface {
	// Begin starts a pseudo nested transaction.
	Begin(ctx context.Context) (pgx.Tx, error)
	// Exec executes a query that doesn't return rows.
	Exec(ctx context.Context, query string, args ...any) (pgconn.CommandTag, error)
	// Query executes a query that returns rows, typically a SELECT.
	Query(ctx context.Context, query string, args ...any) (pgx.Rows, error)
	// QueryRow executes a query that is expected to return at most one row. QueryRow will always return a non-nil value.
	QueryRow(ctx context.Context, query string, args ...any) pgx.Row
	// SendBatch sends a batch of queries to the server. Use Batch.Send to create a batch. The returned Batch
	SendBatch(ctx context.Context, batch *pgx.Batch) pgx.BatchResults
	// CopyFrom copies data from reader to table.
	CopyFrom(ctx context.Context, table pgx.Identifier, columns []string, source pgx.CopyFromSource) (int64, error)
}

Queryable is an interface that wraps the pgx.Executor interface.

type Row

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

Row is a wrapper around pgx.Row.

func (*Row) Scan

func (r *Row) Scan(values ...any) error

Scan implements pgx.Row.

type RowError

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

RowError is a wrapper around pgx.Row that returns an error.

func (RowError) Scan

func (e RowError) Scan(values ...any) error

Scan implements pgx.Row.

type RowRecorder

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

RowRecorder is a wrapper around pgx.Row that records the error.

func (*RowRecorder) RawValues

func (r *RowRecorder) RawValues() [][]byte

RawValues returns the raw values.

func (*RowRecorder) Scan

func (r *RowRecorder) Scan(values ...any) error

Scan implements pgx.Row.

type RowScanner

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

RowScanner is a wrapper around pgx.Row that scans the values.

func (*RowScanner) Scan

func (r *RowScanner) Scan(values ...any) error

Scan implements pgx.Row.

type Rows

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

Rows is a wrapper around pgx.Rows that caches the rows.

func (*Rows) Close

func (r *Rows) Close()

Close implements pgx.Rows.

func (*Rows) CommandTag

func (r *Rows) CommandTag() pgconn.CommandTag

CommandTag implements pgx.Rows.

func (*Rows) Conn

func (r *Rows) Conn() *pgx.Conn

Conn implements pgx.Rows.

func (*Rows) Err

func (r *Rows) Err() error

Err implements pgx.Rows.

func (*Rows) FieldDescriptions

func (r *Rows) FieldDescriptions() []pgconn.FieldDescription

FieldDescriptions implements pgx.Rows.

func (*Rows) Next

func (r *Rows) Next() bool

Next implements pgx.Rows.

func (*Rows) RawValues

func (r *Rows) RawValues() [][]byte

RawValues implements pgx.Rows.

func (*Rows) Scan

func (r *Rows) Scan(values ...any) error

Scan implements pgx.Rows.

func (*Rows) Values

func (r *Rows) Values() ([]any, error)

Values implements pgx.Rows.

type RowsError

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

RowsError is a wrapper around pgx.Rows that returns an error.

func (*RowsError) Close

func (r *RowsError) Close()

Close implements pgx.Rows.

func (*RowsError) CommandTag

func (r *RowsError) CommandTag() pgconn.CommandTag

CommandTag implements pgx.Rows.

func (*RowsError) Conn

func (r *RowsError) Conn() *pgx.Conn

Conn implements pgx.Rows.

func (*RowsError) Err

func (r *RowsError) Err() error

Err implements pgx.Rows.

func (*RowsError) FieldDescriptions

func (r *RowsError) FieldDescriptions() []pgconn.FieldDescription

FieldDescriptions implements pgx.Rows.

func (*RowsError) Next

func (r *RowsError) Next() bool

Next implements pgx.Rows.

func (*RowsError) RawValues

func (r *RowsError) RawValues() [][]byte

RawValues implements pgx.Rows.

func (*RowsError) Scan

func (r *RowsError) Scan(dest ...any) error

Scan implements pgx.Rows.

func (*RowsError) Values

func (r *RowsError) Values() ([]any, error)

Values implements pgx.Rows.

type RowsRecorder

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

RowsRecorder is a wrapper around pgx.Rows that records the rows.

func (*RowsRecorder) Close

func (r *RowsRecorder) Close()

Close implements pgx.Rows.

func (*RowsRecorder) CommandTag

func (r *RowsRecorder) CommandTag() pgconn.CommandTag

CommandTag implements pgx.Rows.

func (*RowsRecorder) Conn

func (r *RowsRecorder) Conn() *pgx.Conn

Conn implements pgx.Rows.

func (*RowsRecorder) Err

func (r *RowsRecorder) Err() error

Err implements pgx.Rows.

func (*RowsRecorder) FieldDescriptions

func (r *RowsRecorder) FieldDescriptions() []pgconn.FieldDescription

FieldDescriptions implements pgx.Rows.

func (*RowsRecorder) Next

func (r *RowsRecorder) Next() bool

Next implements pgx.Rows.

func (*RowsRecorder) RawValues

func (r *RowsRecorder) RawValues() [][]byte

RawValues implements pgx.Rows.

func (*RowsRecorder) Scan

func (r *RowsRecorder) Scan(values ...any) error

Scan implements pgx.Rows.

func (*RowsRecorder) Values

func (r *RowsRecorder) Values() ([]any, error)

Values implements pgx.Rows.

Jump to

Keyboard shortcuts

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