feat(cache-valkey): initial implementation — Provider, adapters (v1.0.0)
Introduces code.nochebuena.dev/einherjar/cache-valkey — the Valkey cache starter for the Einherjar framework. Absorbs the valkey package from micro-lib and adds three duck-typed adapters that wire directly into auth, web, and auth-jwt. Core: - Provider interface — Get, Set, Del, Exists, Expire, IncrWithTTL, Native() - Component interface — lifecycle.Component + observability.Checkable + Provider - Config struct (EINHERJAR_VALKEY_* env vars) - New(logger, cfg) Component — creates valkey-go client in OnInit; PING in OnStart; logs "valkey: connected" - IncrWithTTL implemented with Lua script (atomic INCR + conditional EXPIRE); race-free fixed-window semantics with no MULTI/EXEC overhead - Priority: LevelDegraded — Valkey outage degrades, does not halt the service Adapters (duck-typed — no import of auth, web, or auth-jwt): - PermissionCache — Get/Set int64 bitmasks as string; satisfies auth/rbac.Cache - RateLimiterStore — fixed-window via IncrWithTTL; satisfies web/mw.RateLimiterStore - Blacklist — Exists/Set "1" with TTL; satisfies auth-jwt.Blacklist Compliance test verifies CT-6, duck-type shape assignments, and full adapter behavioural coverage with a mockProvider (no live server required). - Component interface embeds observability.Identifiable; identifiable.go implements ModulePath and ModuleVersion via runtime/debug.ReadBuildInfo() — prints in launcher banner
This commit is contained in:
30
provider.go
Normal file
30
provider.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package cachevalkey
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
vk "github.com/valkey-io/valkey-go"
|
||||
)
|
||||
|
||||
// Provider wraps the most common Valkey operations using only standard Go types.
|
||||
// Callers that need operations beyond this interface call Native().
|
||||
// Component satisfies Provider — pass the result of New() wherever a Provider is expected.
|
||||
type Provider interface {
|
||||
// Get retrieves the string value of key. Returns ("", false, nil) on cache miss.
|
||||
Get(ctx context.Context, key string) (string, bool, error)
|
||||
// Set stores value under key. Pass 0 ttl for no expiry.
|
||||
Set(ctx context.Context, key string, value string, ttl time.Duration) error
|
||||
// Del removes one or more keys. Silently skips non-existent keys.
|
||||
Del(ctx context.Context, keys ...string) error
|
||||
// Exists reports whether key is present.
|
||||
Exists(ctx context.Context, key string) (bool, error)
|
||||
// Expire sets a TTL on an existing key.
|
||||
Expire(ctx context.Context, key string, ttl time.Duration) error
|
||||
// IncrWithTTL atomically increments the integer counter at key and sets
|
||||
// its expiry on first increment. Returns the new counter value.
|
||||
// If key does not exist it is created with value 1.
|
||||
IncrWithTTL(ctx context.Context, key string, ttl time.Duration) (int64, error)
|
||||
// Native returns the underlying valkey-go client for operations not in Provider.
|
||||
Native() vk.Client
|
||||
}
|
||||
Reference in New Issue
Block a user