Documentation
¶
Index ¶
- type CacheEntry
- type CacheEvent
- type CacheEventType
- type CacheKey
- type IntCacheKey
- type LeaderElection
- type LeaderElectionOptions
- type LocalCache
- func (c *LocalCache[K, V]) Contains(key K) bool
- func (c *LocalCache[K, V]) Get(key K) (*V, bool)
- func (c *LocalCache[K, V]) Load() ([]CacheEntry[K, V], error)
- func (c *LocalCache[K, V]) Remove(key K) bool
- func (c *LocalCache[K, V]) RemovePrefix(prefix string)
- func (c *LocalCache[K, V]) Set(key K, value V)
- type LocalCacheOptions
- type RedisStorageBackend
- func (b *RedisStorageBackend[K, V]) AddCallback(callback func(CacheEvent[K, V]))
- func (b *RedisStorageBackend[K, V]) Close() error
- func (b *RedisStorageBackend[K, V]) Contains(ctx context.Context, key K) (bool, error)
- func (b *RedisStorageBackend[K, V]) Get(ctx context.Context, key K) (*V, error)
- func (b *RedisStorageBackend[K, V]) GetStringKey(key K) string
- func (b *RedisStorageBackend[K, V]) Load(ctx context.Context) ([]CacheEntry[K, V], error)
- func (b *RedisStorageBackend[K, V]) PublishEvent(ctx context.Context, event *CacheEvent[K, V]) error
- func (b *RedisStorageBackend[K, V]) Remove(ctx context.Context, key K) error
- func (b *RedisStorageBackend[K, V]) RemovePrefix(ctx context.Context, keyPrefix string) error
- func (b *RedisStorageBackend[K, V]) Set(ctx context.Context, key K, value V) error
- func (b *RedisStorageBackend[K, V]) Ttl(ctx context.Context, key K) (time.Duration, error)
- type RedisStorageBackendOptions
- type StorageBackend
- type StringCacheKey
- type Stringer
- type SynchronizedCache
- func (c *SynchronizedCache[K, V]) Contains(ctx context.Context, key K) (bool, error)
- func (c *SynchronizedCache[K, V]) Get(ctx context.Context, key K) (*V, bool)
- func (c *SynchronizedCache[K, V]) Remove(ctx context.Context, key K) error
- func (c *SynchronizedCache[K, V]) RemovePrefix(ctx context.Context, prefix string) error
- func (c *SynchronizedCache[K, V]) Set(ctx context.Context, key K, value V) error
- func (c *SynchronizedCache[K, V]) Ttl(ctx context.Context, key K) (time.Duration, error)
- type SynchronizedCacheOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CacheEntry ¶
type CacheEntry[K comparable, V any] struct { Key K Value *V }
type CacheEvent ¶
type CacheEvent[K comparable, V any] struct { Entry *CacheEntry[K, V] Type CacheEventType KeyPrefix string }
type CacheEventType ¶
type CacheEventType int
const ( CacheEventSet CacheEventType = iota CacheEventRemove CacheEventRemovePrefix )
type IntCacheKey ¶
type IntCacheKey struct {
}
func (*IntCacheKey) Marshal ¶
func (k *IntCacheKey) Marshal(key int) string
func (*IntCacheKey) MarshallAll ¶
func (k *IntCacheKey) MarshallAll(keys []int) []string
func (*IntCacheKey) UnmarshalAll ¶
func (k *IntCacheKey) UnmarshalAll(data []string) ([]int, error)
type LeaderElection ¶ added in v0.16.0
type LeaderElection struct {
// contains filtered or unexported fields
}
LeaderElection manages distributed leader election via Redis.
func NewLeaderElection ¶ added in v0.16.0
func NewLeaderElection(opts LeaderElectionOptions) (*LeaderElection, error)
NewLeaderElection creates a new leader election instance. Call Start to begin participating in the election.
func (*LeaderElection) FencingToken ¶ added in v0.16.0
func (le *LeaderElection) FencingToken() int64
FencingToken returns the current monotonically increasing epoch. Returns 0 if this instance is not the leader. Use this to guard operations that should only be performed by the current leader.
func (*LeaderElection) IsLeader ¶ added in v0.16.0
func (le *LeaderElection) IsLeader() bool
IsLeader returns whether this instance currently holds the leader lock.
func (*LeaderElection) Start ¶ added in v0.16.0
func (le *LeaderElection) Start(ctx context.Context) error
Start begins the election loop. It is non-blocking and returns immediately. The election loop runs until the provided context is cancelled or Stop is called.
func (*LeaderElection) Stop ¶ added in v0.16.0
func (le *LeaderElection) Stop() error
Stop gracefully resigns leadership (if held) and stops the election loop. It blocks until shutdown is complete. The Redis client is not closed — the caller owns it. Calling Stop multiple times is safe.
type LeaderElectionOptions ¶ added in v0.16.0
type LeaderElectionOptions struct {
// Client is a pre-existing go-redis client. Required.
Client *redis.Client
// LockKey is the Redis key used for the leader lock. Required.
LockKey string
// InstanceID uniquely identifies this participant.
// If empty, a random ID is generated.
InstanceID string
// LeaseDuration is how long the lock is held before expiry.
// Must be positive. Recommended: 10-15s.
LeaseDuration time.Duration
// RenewInterval is how often the leader renews its lease.
// If zero, defaults to LeaseDuration/3.
RenewInterval time.Duration
// RetryInterval is how often non-leaders attempt acquisition.
// If zero, defaults to LeaseDuration/2.
RetryInterval time.Duration
// OnElected is called when this instance becomes leader.
// The provided context is cancelled when leadership is lost.
// Called in a new goroutine — safe to block.
OnElected func(ctx context.Context)
// OnDemoted is called when this instance loses leadership.
OnDemoted func()
// PubSubChannel enables fast failover via Redis PubSub.
// If empty, PubSub is disabled and failover relies on lease expiry + polling.
PubSubChannel string
}
LeaderElectionOptions configures a leader election instance.
type LocalCache ¶
type LocalCache[K comparable, V any] struct { Options *LocalCacheOptions[K] Cache *lru.LRU[string, *CacheEntry[K, V]] CacheKey CacheKey[K] }
func NewLocalCache ¶
func NewLocalCache[K comparable, V any](options *LocalCacheOptions[K]) *LocalCache[K, V]
func (*LocalCache[K, V]) Contains ¶
func (c *LocalCache[K, V]) Contains(key K) bool
func (*LocalCache[K, V]) Get ¶
func (c *LocalCache[K, V]) Get(key K) (*V, bool)
func (*LocalCache[K, V]) Load ¶
func (c *LocalCache[K, V]) Load() ([]CacheEntry[K, V], error)
func (*LocalCache[K, V]) Remove ¶
func (c *LocalCache[K, V]) Remove(key K) bool
func (*LocalCache[K, V]) RemovePrefix ¶
func (c *LocalCache[K, V]) RemovePrefix(prefix string)
func (*LocalCache[K, V]) Set ¶
func (c *LocalCache[K, V]) Set(key K, value V)
type LocalCacheOptions ¶
type LocalCacheOptions[K comparable] struct { TTL time.Duration Size int CacheKey CacheKey[K] }
Options passed to NewLocalCache
Ttl: Time to live for each entry in the cache. Set to 0 to disable expiration Size: Maximum number of entries in the cache. Set to 0 for unlimited size
func (*LocalCacheOptions[K]) GetCacheKey ¶
func (o *LocalCacheOptions[K]) GetCacheKey() CacheKey[K]
func (*LocalCacheOptions[K]) GetSize ¶
func (o *LocalCacheOptions[K]) GetSize() int
func (*LocalCacheOptions[K]) GetTtl ¶
func (o *LocalCacheOptions[K]) GetTtl() time.Duration
type RedisStorageBackend ¶
type RedisStorageBackend[K comparable, V any] struct { Options *RedisStorageBackendOptions[K] Client *redis.Client // contains filtered or unexported fields }
func NewRedisStorageBackend ¶
func NewRedisStorageBackend[K comparable, V any](options *RedisStorageBackendOptions[K]) (*RedisStorageBackend[K, V], error)
func (*RedisStorageBackend[K, V]) AddCallback ¶
func (b *RedisStorageBackend[K, V]) AddCallback(callback func(CacheEvent[K, V]))
func (*RedisStorageBackend[K, V]) Close ¶
func (b *RedisStorageBackend[K, V]) Close() error
func (*RedisStorageBackend[K, V]) Contains ¶
func (b *RedisStorageBackend[K, V]) Contains(ctx context.Context, key K) (bool, error)
func (*RedisStorageBackend[K, V]) Get ¶
func (b *RedisStorageBackend[K, V]) Get(ctx context.Context, key K) (*V, error)
func (*RedisStorageBackend[K, V]) GetStringKey ¶
func (b *RedisStorageBackend[K, V]) GetStringKey(key K) string
func (*RedisStorageBackend[K, V]) Load ¶
func (b *RedisStorageBackend[K, V]) Load(ctx context.Context) ([]CacheEntry[K, V], error)
func (*RedisStorageBackend[K, V]) PublishEvent ¶
func (b *RedisStorageBackend[K, V]) PublishEvent(ctx context.Context, event *CacheEvent[K, V]) error
func (*RedisStorageBackend[K, V]) Remove ¶
func (b *RedisStorageBackend[K, V]) Remove(ctx context.Context, key K) error
func (*RedisStorageBackend[K, V]) RemovePrefix ¶
func (b *RedisStorageBackend[K, V]) RemovePrefix(ctx context.Context, keyPrefix string) error
type RedisStorageBackendOptions ¶
type RedisStorageBackendOptions[K comparable] struct { RedisOptions *redis.Options TTL time.Duration CacheKey CacheKey[K] KeyPrefix string PubSub bool PubSubChannelName string ScanCount int64 }
func (*RedisStorageBackendOptions[K]) GetScanCount ¶
func (o *RedisStorageBackendOptions[K]) GetScanCount() int64
type StorageBackend ¶
type StorageBackend[K comparable, V any] interface { Get(context.Context, K) (*V, error) Ttl(context.Context, K) (time.Duration, error) Set(context.Context, K, V) error Remove(context.Context, K) error RemovePrefix(context.Context, string) error Contains(context.Context, K) (bool, error) Load(context.Context) ([]CacheEntry[K, V], error) AddCallback(func(CacheEvent[K, V])) }
type StringCacheKey ¶
type StringCacheKey struct {
}
func (*StringCacheKey) Marshal ¶
func (k *StringCacheKey) Marshal(key string) string
func (*StringCacheKey) MarshallAll ¶
func (k *StringCacheKey) MarshallAll(keys []string) []string
func (*StringCacheKey) UnmarshalAll ¶
func (k *StringCacheKey) UnmarshalAll(data []string) ([]string, error)
type SynchronizedCache ¶
type SynchronizedCache[K comparable, V any] struct { // contains filtered or unexported fields }
func NewSynchronizedCache ¶
func NewSynchronizedCache[K comparable, V any](options *SynchronizedCacheOptions[K, V]) (*SynchronizedCache[K, V], error)
func (*SynchronizedCache[K, V]) Contains ¶
func (c *SynchronizedCache[K, V]) Contains(ctx context.Context, key K) (bool, error)
func (*SynchronizedCache[K, V]) Get ¶
func (c *SynchronizedCache[K, V]) Get(ctx context.Context, key K) (*V, bool)
func (*SynchronizedCache[K, V]) Remove ¶
func (c *SynchronizedCache[K, V]) Remove(ctx context.Context, key K) error
func (*SynchronizedCache[K, V]) RemovePrefix ¶
func (c *SynchronizedCache[K, V]) RemovePrefix(ctx context.Context, prefix string) error