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"`
}
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)
Successful lookups are cached for the configured TTL. "Not found" is cached only when WithNegativeTTL asks for it, and never for its own TTL by default. 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 ¶
FindTenant returns the tenant for id, serving from cache when a non-expired entry exists (including a negative result, when WithNegativeTTL enabled them) 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.
func WithNegativeTTL ¶ added in v0.38.0
WithNegativeTTL caches "no such tenant" for ttl, so a stream of requests carrying an unknown tenant ID costs one lookup instead of one per request — the difference between a misconfigured client being harmless and it being a load generator aimed at the tenant store.
It is off by default because it trades freshness for that protection: a tenant created after a failed lookup stays invisible to this replica until the entry expires. Keep ttl well under the positive TTL, and call Invalidate on the create path when the creating replica also serves requests.
Only a not-found error is cached. A store that fails for any other reason — a timeout, a dropped connection — is never remembered as an absent tenant.