store

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package store defines tenant metadata storage contracts and memory, cached, and SQL implementations.

Index

Constants

View Source
const (
	// DefaultSQLTableName is the default tenant metadata table name.
	DefaultSQLTableName = "tenants"
)

Variables

View Source
var (
	// ErrTenantNotFound reports that a tenant does not exist in the store.
	ErrTenantNotFound = errors.New("gotenancy/store: tenant not found")

	// ErrTenantAlreadyExists reports that a tenant already exists in the store.
	ErrTenantAlreadyExists = errors.New("gotenancy/store: tenant already exists")

	// ErrTenantConflict reports that tenant metadata changed after it was read.
	ErrTenantConflict = errors.New("gotenancy/store: tenant update conflict")

	// ErrInvalidTenant reports that tenant metadata is not valid for persistence.
	ErrInvalidTenant = errors.New("gotenancy/store: invalid tenant")

	// ErrInvalidListFilter reports an invalid tenant list filter.
	ErrInvalidListFilter = errors.New("gotenancy/store: invalid list filter")

	// ErrNilStore reports that a store dependency is nil.
	ErrNilStore = errors.New("gotenancy/store: nil store")

	// ErrNilCache reports that a cache dependency is nil.
	ErrNilCache = errors.New("gotenancy/store: nil cache")

	// ErrNilDB reports that a SQL store was created with a nil database handle.
	ErrNilDB = errors.New("gotenancy/store: nil db")

	// ErrInvalidTableName reports an unsafe SQL table name.
	ErrInvalidTableName = errors.New("gotenancy/store: invalid table name")

	// ErrUnsupportedSQLDialect reports an unsupported SQLStore dialect.
	ErrUnsupportedSQLDialect = errors.New("gotenancy/store: unsupported sql dialect")

	// ErrInvalidCacheSize reports an invalid bounded memory cache size.
	ErrInvalidCacheSize = errors.New("gotenancy/store: invalid cache size")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Get(ctx context.Context, id types.TenantID) (types.Tenant, bool, error)
	Set(ctx context.Context, tenant types.Tenant, ttl time.Duration) error
	Delete(ctx context.Context, id types.TenantID) error
	Invalidate(ctx context.Context) error
}

Cache stores tenant metadata for faster lookups.

type CachedStore

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

CachedStore wraps a Store with cache-aside reads and write-through invalidation. Read fills and writes are ordered across in-process wrappers that share the same non-nil pointer-backed Cache instance, even when their Store wrappers differ. Non-pointer Cache values are used in full-bypass mode because they do not provide a stable identity for coordinating wrappers. Applications with wrappers in different processes should configure a finite TTL or a cache with its own versioning/invalidation protocol. A non-positive TTL is safe across processes only when the cache provides that stronger coherence mechanism.

func NewCachedStore

func NewCachedStore(next Store, cache Cache, ttl time.Duration) (*CachedStore, error)

NewCachedStore creates a cached store decorator.

func (*CachedStore) CompareAndSwap added in v0.1.8

func (store *CachedStore) CompareAndSwap(ctx context.Context, expected types.Tenant, updated types.Tenant) error

CompareAndSwap atomically replaces expected tenant metadata and refreshes the cache. When the wrapped store does not implement CompareAndSwapStore, CachedStore provides a process-local read/compare/update fallback under its write lock.

func (*CachedStore) Create

func (store *CachedStore) Create(ctx context.Context, tenant types.Tenant) error

Create inserts tenant metadata and refreshes the cache.

func (*CachedStore) Delete

func (store *CachedStore) Delete(ctx context.Context, id types.TenantID) error

Delete removes tenant metadata and invalidates its cache entry.

func (*CachedStore) Get

func (store *CachedStore) Get(ctx context.Context, id types.TenantID) (types.Tenant, error)

Get returns tenant metadata from cache when available, otherwise from the wrapped store.

func (*CachedStore) List

func (store *CachedStore) List(ctx context.Context, filter ListFilter) ([]types.Tenant, error)

List delegates to the wrapped store.

func (*CachedStore) Update

func (store *CachedStore) Update(ctx context.Context, tenant types.Tenant) error

Update replaces tenant metadata and refreshes the cache.

type CompareAndSwapStore added in v0.1.8

type CompareAndSwapStore interface {
	Store
	CompareAndSwap(ctx context.Context, expected types.Tenant, updated types.Tenant) error
}

CompareAndSwapStore extends Store with an atomic conditional replacement. CompareAndSwap replaces expected with updated only when the persisted tenant still equals expected. Implementations return ErrTenantConflict when another writer changed the tenant first.

type ListFilter

type ListFilter struct {
	Statuses []types.TenantStatus
	Limit    int
	Offset   int
}

ListFilter restricts tenant list queries.

type MemoryCache

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

MemoryCache is a thread-safe in-memory tenant cache with TTL support.

func NewBoundedMemoryCache

func NewBoundedMemoryCache(maxEntries int) (*MemoryCache, error)

NewBoundedMemoryCache creates a memory cache with a maximum number of entries.

func NewMemoryCache

func NewMemoryCache() *MemoryCache

NewMemoryCache creates an empty memory cache.

func (*MemoryCache) Delete

func (cache *MemoryCache) Delete(ctx context.Context, id types.TenantID) error

Delete removes cached tenant metadata by ID.

func (*MemoryCache) Get

func (cache *MemoryCache) Get(ctx context.Context, id types.TenantID) (types.Tenant, bool, error)

Get returns cached tenant metadata.

func (*MemoryCache) Invalidate

func (cache *MemoryCache) Invalidate(ctx context.Context) error

Invalidate removes all cached tenant metadata.

func (*MemoryCache) Set

func (cache *MemoryCache) Set(ctx context.Context, tenant types.Tenant, ttl time.Duration) error

Set stores tenant metadata with ttl. A non-positive ttl means no expiration.

type MemoryStore

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

MemoryStore is a thread-safe in-memory tenant metadata store.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates an empty memory store.

func (*MemoryStore) CompareAndSwap added in v0.1.8

func (store *MemoryStore) CompareAndSwap(ctx context.Context, expected types.Tenant, updated types.Tenant) error

CompareAndSwap atomically replaces expected tenant metadata with updated.

func (*MemoryStore) Create

func (store *MemoryStore) Create(ctx context.Context, tenant types.Tenant) error

Create inserts tenant metadata.

func (*MemoryStore) Delete

func (store *MemoryStore) Delete(ctx context.Context, id types.TenantID) error

Delete removes tenant metadata by ID.

func (*MemoryStore) Get

func (store *MemoryStore) Get(ctx context.Context, id types.TenantID) (types.Tenant, error)

Get returns tenant metadata by ID.

func (*MemoryStore) List

func (store *MemoryStore) List(ctx context.Context, filter ListFilter) ([]types.Tenant, error)

List returns tenants matching filter.

func (*MemoryStore) ListPage added in v0.1.6

func (store *MemoryStore) ListPage(ctx context.Context, filter PageFilter) ([]types.Tenant, error)

ListPage returns tenants after the cursor while preserving List filtering semantics.

func (*MemoryStore) Update

func (store *MemoryStore) Update(ctx context.Context, tenant types.Tenant) error

Update replaces existing tenant metadata.

type PageFilter added in v0.1.6

type PageFilter struct {
	Statuses []types.TenantStatus
	Limit    int
	Offset   int
	// Cursor returns rows ordered after the tenant ID cursor.
	Cursor types.TenantID
}

PageFilter restricts cursor-based tenant list queries.

type PagedStore added in v0.1.6

type PagedStore interface {
	Store
	ListPage(ctx context.Context, filter PageFilter) ([]types.Tenant, error)
}

PagedStore extends Store with cursor-based tenant listing.

type SQLDialect

type SQLDialect string

SQLDialect controls SQL placeholder rendering for SQLStore.

const (
	// SQLDialectMySQL uses question-mark placeholders and is the default.
	SQLDialectMySQL SQLDialect = "mysql"

	// SQLDialectSQLite uses question-mark placeholders.
	SQLDialectSQLite SQLDialect = "sqlite"

	// SQLDialectPostgres uses numbered placeholders.
	SQLDialectPostgres SQLDialect = "postgres"
)

type SQLStore

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

SQLStore persists tenant metadata through database/sql.

The table is expected to contain these columns: id, name, status, plan_id, config. The config column stores a JSON object with string keys and values.

func NewSQLStore

func NewSQLStore(db *sql.DB, opts ...SQLStoreOption) (*SQLStore, error)

NewSQLStore creates a SQL-backed tenant metadata store.

func (*SQLStore) CompareAndSwap added in v0.1.8

func (store *SQLStore) CompareAndSwap(ctx context.Context, expected types.Tenant, updated types.Tenant) (err error)

CompareAndSwap atomically replaces expected tenant metadata with updated.

func (*SQLStore) Create

func (store *SQLStore) Create(ctx context.Context, tenant types.Tenant) error

Create inserts tenant metadata.

func (*SQLStore) Delete

func (store *SQLStore) Delete(ctx context.Context, id types.TenantID) error

Delete removes tenant metadata by ID.

func (*SQLStore) Get

func (store *SQLStore) Get(ctx context.Context, id types.TenantID) (types.Tenant, error)

Get returns tenant metadata by ID.

func (*SQLStore) List

func (store *SQLStore) List(ctx context.Context, filter ListFilter) (tenants []types.Tenant, err error)

List returns tenants matching filter.

func (*SQLStore) ListPage added in v0.1.6

func (store *SQLStore) ListPage(ctx context.Context, filter PageFilter) (tenants []types.Tenant, err error)

ListPage returns tenants after the cursor while preserving List filtering semantics.

func (*SQLStore) Update

func (store *SQLStore) Update(ctx context.Context, tenant types.Tenant) error

Update replaces existing tenant metadata.

type SQLStoreOption

type SQLStoreOption func(*SQLStore) error

SQLStoreOption configures SQLStore.

func WithSQLDialect

func WithSQLDialect(dialect SQLDialect) SQLStoreOption

WithSQLDialect configures SQL placeholder rendering.

func WithTableName

func WithTableName(table string) SQLStoreOption

WithTableName overrides the default tenant metadata table name.

type Store

type Store interface {
	Get(ctx context.Context, id types.TenantID) (types.Tenant, error)
	List(ctx context.Context, filter ListFilter) ([]types.Tenant, error)
	Create(ctx context.Context, tenant types.Tenant) error
	Update(ctx context.Context, tenant types.Tenant) error
	Delete(ctx context.Context, id types.TenantID) error
}

Store persists tenant metadata.

Jump to

Keyboard shortcuts

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