cache

package
v0.1.3-rc.4 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package cache defines a generic read-result cache with per-table invalidation. Writes (create/update/delete) call Invalidate(table) to drop entries for that table precisely, rather than relying on blind TTL expiry (Oracle result-cache semantics). TTLCache uses sync.Map and lazy expiry; weak-pointer soft caching is a future refinement.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[T any] interface {
	Get(ctx context.Context, k Key) (T, bool)
	Set(ctx context.Context, k Key, v T) error
	Invalidate(entity string) error
}

Cache is a generic read cache with per-table invalidation.

type Key

type Key struct {
	Entity string
	SQL    string
	Args   string
	Scope  string
}

Key identifies a cached entry. Scope separates authorization/RLS identities.

type NoopCache

type NoopCache[T any] struct{}

NoopCache is a Cache that stores nothing.

func (NoopCache[T]) Get

func (NoopCache[T]) Get(_ context.Context, _ Key) (T, bool)

Get always misses.

func (NoopCache[T]) Invalidate

func (NoopCache[T]) Invalidate(_ string) error

Invalidate is a no-op.

func (NoopCache[T]) Set

func (NoopCache[T]) Set(_ context.Context, _ Key, _ T) error

Set is a no-op.

type TTLCache

type TTLCache[T any] struct {
	// contains filtered or unexported fields
}

TTLCache stores values with a TTL, evaluated lazily on Get. Set removes all expired entries before evicting an arbitrary live entry to stay under the configured cap.

func NewTTLCache

func NewTTLCache[T any](ttl time.Duration, maxSize int) *TTLCache[T]

NewTTLCache returns a TTLCache. maxSize <= 0 uses a safe default capacity.

func (*TTLCache[T]) Get

func (c *TTLCache[T]) Get(_ context.Context, k Key) (T, bool)

Get returns the value if present and unexpired.

func (*TTLCache[T]) Invalidate

func (c *TTLCache[T]) Invalidate(entity string) error

Invalidate removes all entries whose Key.Entity matches.

func (*TTLCache[T]) Set

func (c *TTLCache[T]) Set(_ context.Context, k Key, v T) error

Set stores a value with the configured TTL, evicting if over maxSize.

Jump to

Keyboard shortcuts

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