tenant

package
v0.29.1 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package tenant provides multi-tenant primitives: a tenant store abstraction and a TTL-cached store that fronts it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Base

type Base struct {
	ID        uuid.UUID `json:"id" gorm:"primaryKey;type:uuid"`
	Code      string    `json:"code" gorm:"type:varchar(50);uniqueIndex;not null"`
	IsActive  bool      `json:"isActive" gorm:"not null"`
	CreatedAt time.Time `json:"createdAt" gorm:"type:timestamp;not null"`
	UpdatedAt time.Time `json:"updatedAt" gorm:"type:timestamp;not null"`
}

func (*Base) AsBase

func (b *Base) AsBase() *Base

type CachedStore

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

CachedStore wraps a Store with an in-memory, time-bounded cache. It satisfies Store itself, so it can be passed wherever a Store is accepted.

store := tenant.NewCachedStore(dbStore, 5*time.Minute)

Both successful and "not found" lookups are cached for the configured TTL. Call Invalidate when a tenant changes to drop its entry before the TTL expires. It is safe for concurrent use.

func NewCachedStore

func NewCachedStore(store Store, ttl time.Duration, opts ...Option) *CachedStore

NewCachedStore returns a CachedStore that caches lookups from store for ttl.

func (*CachedStore) Clear added in v0.22.0

func (c *CachedStore) Clear()

Clear drops every cached entry, forcing subsequent FindTenant calls to consult the wrapped store. It is safe for concurrent use.

func (*CachedStore) FindTenant

func (c *CachedStore) FindTenant(ctx context.Context, id uuid.UUID) (Tenant, error)

FindTenant returns the tenant for id, serving from cache when a non-expired entry exists (including cached negative results) and otherwise delegating to the wrapped store and caching the outcome.

func (*CachedStore) Invalidate

func (c *CachedStore) Invalidate(id uuid.UUID)

Invalidate removes the cached entry for id, forcing the next FindTenant to consult the wrapped store.

type Option

type Option func(*CachedStore)

Option customizes a CachedStore.

func WithClock

func WithClock(clock core.TimeProvider) Option

WithClock injects the time source used for TTL expiry. Defaults to core.UTC. Pass a *core.FixedClock for deterministic tests.

type Store

type Store interface {
	FindTenant(ctx context.Context, id uuid.UUID) (Tenant, error)
}

type Tenant

type Tenant interface {
	AsBase() *Base
}

Jump to

Keyboard shortcuts

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