Documentation
¶
Index ¶
- Constants
- type Cache
- type CacheManager
- type CacheOptions
- type Entry
- type InMemoryCache
- func (c *InMemoryCache) Checksum() string
- func (c *InMemoryCache) Class() string
- func (c *InMemoryCache) DecrementIfPositive(ctx context.Context, key string) (int64, bool, error)
- func (c *InMemoryCache) Delete(ctx context.Context, key string) error
- func (c *InMemoryCache) Get(ctx context.Context, key string) (string, bool, bool, error)
- func (c *InMemoryCache) GetAndDelete(ctx context.Context, key string) (string, bool, bool, error)
- func (c *InMemoryCache) Host() string
- func (c *InMemoryCache) Set(ctx context.Context, key string, value string, opts ...SetOption) error
- func (c *InMemoryCache) TTL() time.Duration
- func (c *InMemoryCache) Uncycled() bool
- func (c *InMemoryCache) Update(ctx context.Context, key string, fn UpdateFunc, opts ...SetOption) (bool, error)
- type InMemoryCacheManager
- type SetOption
- type SetOptions
- type UpdateFunc
- type ValkeyCache
- func (s *ValkeyCache) Checksum() string
- func (s *ValkeyCache) Class() string
- func (s *ValkeyCache) DecrementIfPositive(ctx context.Context, key string) (int64, bool, error)
- func (s *ValkeyCache) Delete(ctx context.Context, key string) error
- func (s *ValkeyCache) Get(ctx context.Context, key string) (string, bool, bool, error)
- func (s *ValkeyCache) GetAndDelete(ctx context.Context, key string) (string, bool, bool, error)
- func (s *ValkeyCache) Host() string
- func (s *ValkeyCache) Set(ctx context.Context, key string, value string, opts ...SetOption) error
- func (s *ValkeyCache) TTL() time.Duration
- func (s *ValkeyCache) Uncycled() bool
- func (s *ValkeyCache) Update(ctx context.Context, key string, fn UpdateFunc, opts ...SetOption) (bool, error)
- type ValkeyCacheManager
Constants ¶
const SurvivingGeneration = initialGeneration
SurvivingGeneration is the one generation of the surviving scope (every Uncycled cache). It is fixed, so a replica that first creates the cache after a host reconfiguration reads what its peers wrote.
It is initialGeneration because, before it was fixed, an Uncycled cache took whatever generation the manager was at when the cache was created: "0" for every cache created before the process's first Cycle, which is where the auth caches (sessions, codes, registered clients, consent, use counters) were found. A cache first created after a Cycle, such as the proxy's token cache, starts empty once; every such cache fails closed.
The key shape {host:class:SurvivingGeneration} holds security state (token revocations). Changing it strands that state, which un-revokes tokens: move the entries with ScanGenerations, never orphan them.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
Checksum() string
Class() string
Delete(ctx context.Context, key string) error
Get(ctx context.Context, key string) (value string, exists bool, isCurrent bool, err error)
// GetAndDelete atomically reads and removes the entry for key,
// returning whether it existed. The primitive is needed to close
// the concurrent-redeem race in auth-code / refresh-token rotation
// (kdex-tech/host-manager#71): two concurrent callers that race on
// the same key must observe exactly one winner (found=true) and
// N-1 losers (found=false). Implementations that don't natively
// expose an atomic GETDEL must guard with their own lock.
GetAndDelete(ctx context.Context, key string) (value string, exists bool, isCurrent bool, err error)
// DecrementIfPositive atomically decrements the integer value at key when
// it exists and is > 0, returning the remaining count and ok=true. When the
// key is missing or already <= 0 it returns (-1, false, nil) WITHOUT
// writing — the fail-closed primitive behind bounded-use capability tokens.
DecrementIfPositive(ctx context.Context, key string) (remaining int64, ok bool, err error)
Host() string
Set(ctx context.Context, key string, value string, opts ...SetOption) error
// Update atomically replaces key's entry in the current generation with
// what fn returns for the entry it holds, and reports whether it wrote.
// fn returning write=false leaves the entry alone. A concurrent write to
// key makes fn run again on the new value, so no update is lost; fn must
// therefore have no side effects.
Update(ctx context.Context, key string, fn UpdateFunc, opts ...SetOption) (bool, error)
TTL() time.Duration
Uncycled() bool
}
type CacheManager ¶
type CacheManager interface {
// Cycle moves every cycled class to the generation checksum. A class
// already at checksum is left alone, so its previous generation stays
// readable. Uncycled classes never move, force or not.
Cycle(checksum string, force bool) error
GetCache(class string, opts CacheOptions) Cache
// ScanGenerations calls fn for every entry this host's class holds under
// any generation, including generations no Get can reach any more. It is
// for recovering state a misclassified cache stranded, not for serving.
// On Valkey it assumes a standalone server: a cluster's SCAN reaches one
// node.
ScanGenerations(ctx context.Context, class string, fn func(Entry) error) error
}
func NewCacheManager ¶
func NewCacheManager(addr, host string, ttl *time.Duration) (CacheManager, error)
type CacheOptions ¶
type CacheOptions struct {
MaxItems *int
TTL *time.Duration
// Uncycled puts the cache in the surviving scope, the one that survives
// host generations. Anything a request creates and a later request must
// find again (a revocation, a session, a registered client, a use
// counter) belongs there. Leaving it false keys the cache on the host's
// generation, which suits only data that can be rebuilt from the CRs,
// such as a rendered page: every reconfiguration abandons it.
//
// Toward kdex-tech/host-manager#124: a cache will name the scope its
// generation is keyed on (the whole host, the auth config, ...), and
// Uncycled is the scope keyed on nothing.
Uncycled bool
}
type Entry ¶ added in v0.28.0
type Entry struct {
// Generation is the checksum the entry was written under.
Generation string
Key string
// TTL is the entry's remaining lifetime; zero when it has none.
TTL time.Duration
Value string
}
Entry is one cache entry as ScanGenerations reports it.
type InMemoryCache ¶
type InMemoryCache struct {
// contains filtered or unexported fields
}
func (*InMemoryCache) Checksum ¶ added in v0.2.7
func (c *InMemoryCache) Checksum() string
func (*InMemoryCache) Class ¶
func (c *InMemoryCache) Class() string
func (*InMemoryCache) DecrementIfPositive ¶ added in v0.4.0
DecrementIfPositive atomically decrements the integer value at key when it exists (in either the current or previous generation, matching the Get/GetAndDelete fallback semantics) and is > 0. It runs the same segment walk as lookupLocked under a single write lock, but — unlike lookupLocked — must know which segment the entry lives in so the decremented value can be written back in place, preserving the existing expiry and LRU element rather than resetting them (mirrors the invariant Set uses when refreshing an existing entry). Missing key or a non-positive/non-integer value fails closed: (-1, false, nil) WITHOUT writing.
func (*InMemoryCache) Delete ¶
func (c *InMemoryCache) Delete(ctx context.Context, key string) error
func (*InMemoryCache) GetAndDelete ¶ added in v0.2.92
GetAndDelete atomically reads and removes the entry. Implementation: run the same Get walk and, on a hit, evict the entry before releasing the lock. The single c.mu.Lock around both halves is what makes this the contract-required single-winner primitive. See kdex-tech/host-manager#71.
func (*InMemoryCache) Host ¶
func (c *InMemoryCache) Host() string
func (*InMemoryCache) TTL ¶
func (c *InMemoryCache) TTL() time.Duration
func (*InMemoryCache) Uncycled ¶
func (c *InMemoryCache) Uncycled() bool
func (*InMemoryCache) Update ¶ added in v0.28.0
func (c *InMemoryCache) Update(ctx context.Context, key string, fn UpdateFunc, opts ...SetOption) (bool, error)
Update runs fn and the write under one lock, so nothing can interleave.
type InMemoryCacheManager ¶
type InMemoryCacheManager struct {
// contains filtered or unexported fields
}
func (*InMemoryCacheManager) Cycle ¶
func (m *InMemoryCacheManager) Cycle(checksum string, force bool) error
func (*InMemoryCacheManager) GetCache ¶
func (m *InMemoryCacheManager) GetCache(class string, opts CacheOptions) Cache
func (*InMemoryCacheManager) ScanGenerations ¶ added in v0.28.0
func (m *InMemoryCacheManager) ScanGenerations(ctx context.Context, class string, fn func(Entry) error) error
ScanGenerations reports what this process still holds: memory keeps only the current and previous generation, so nothing older is left to recover.
type SetOption ¶ added in v0.2.30
type SetOption func(*SetOptions)
type SetOptions ¶ added in v0.2.30
type UpdateFunc ¶ added in v0.28.0
UpdateFunc computes an entry's next value from its current one (found is false when there is none). write=false leaves the entry as it is.
type ValkeyCache ¶
type ValkeyCache struct {
// contains filtered or unexported fields
}
func (*ValkeyCache) Checksum ¶ added in v0.2.7
func (s *ValkeyCache) Checksum() string
func (*ValkeyCache) Class ¶
func (s *ValkeyCache) Class() string
func (*ValkeyCache) DecrementIfPositive ¶ added in v0.4.0
DecrementIfPositive atomically decrements the integer value at key when it exists and is > 0, returning the remaining count and ok=true; otherwise it fails closed with (-1, false, nil) WITHOUT writing. This is the bounded-use capability token primitive; callers create these caches with CacheOptions{Uncycled: true}, so — unlike Get/Set — there is no prevPrefix fallback to consider.
func (*ValkeyCache) GetAndDelete ¶ added in v0.2.92
GetAndDelete atomically reads and removes the entry. Backed by Valkey's GETDEL primitive (single round-trip, server-side atomic), so two concurrent callers racing on the same key always produce exactly one winner. See kdex-tech/host-manager#71.
func (*ValkeyCache) Host ¶
func (s *ValkeyCache) Host() string
func (*ValkeyCache) TTL ¶
func (s *ValkeyCache) TTL() time.Duration
func (*ValkeyCache) Uncycled ¶
func (s *ValkeyCache) Uncycled() bool
func (*ValkeyCache) Update ¶ added in v0.28.0
func (s *ValkeyCache) Update(ctx context.Context, key string, fn UpdateFunc, opts ...SetOption) (bool, error)
Update is an optimistic transaction: WATCH the key, read it, and write the new value in a MULTI/EXEC that Valkey aborts if anyone wrote the key since the WATCH; an aborted attempt runs fn again on the newer value.
type ValkeyCacheManager ¶
type ValkeyCacheManager struct {
// contains filtered or unexported fields
}
func (*ValkeyCacheManager) Cycle ¶
func (m *ValkeyCacheManager) Cycle(checksum string, force bool) error
func (*ValkeyCacheManager) GetCache ¶
func (m *ValkeyCacheManager) GetCache(class string, opts CacheOptions) Cache
func (*ValkeyCacheManager) ScanGenerations ¶ added in v0.28.0
func (m *ValkeyCacheManager) ScanGenerations(ctx context.Context, class string, fn func(Entry) error) error
ScanGenerations walks the keyspace with SCAN, so it costs a pass over every key in the store; it is meant for a one-time recovery, not a request path.