Documentation
¶
Index ¶
- Constants
- Variables
- func HTTPCache(c *Cache, ttl time.Duration) func(http.Handler) http.Handler
- func HTTPRateLimit(c *Cache, limit int64, window time.Duration) func(http.Handler) http.Handler
- type Cache
- func (c *Cache) Broker() *pubsub.Broker
- func (c *Cache) ClusterState() *cluster.ClusterState
- func (c *Cache) Decr(key string, delta int64) (int64, error)
- func (c *Cache) Delete(key string)
- func (c *Cache) Exists(key string) bool
- func (c *Cache) Get(key string, dest interface{}) bool
- func (c *Cache) GetOrSet(key string, dest interface{}, ttl time.Duration, ...) error
- func (c *Cache) HandleDelete(key string) bool
- func (c *Cache) HandleExists(key string) bool
- func (c *Cache) HandleGet(key string) ([]byte, bool)
- func (c *Cache) HandleIncr(key string, delta int64) (int64, error)
- func (c *Cache) HandleLockAcquire(key string, ttl time.Duration) (string, uint64, bool)
- func (c *Cache) HandleLockExtend(key string, token string, ttl time.Duration) bool
- func (c *Cache) HandleLockRelease(key string, token string) bool
- func (c *Cache) HandleMGet(keys []string) map[string][]byte
- func (c *Cache) HandleSet(key string, value []byte, ttl time.Duration, tags []string)
- func (c *Cache) Incr(key string, delta int64) (int64, error)
- func (c *Cache) InvalidateTag(tag string)
- func (c *Cache) Len() int
- func (c *Cache) Lock(key string, opts LockOptions) (*LockHandle, error)
- func (c *Cache) LockEngine() *lock.Engine
- func (c *Cache) MGet(keys ...string) map[string][]byte
- func (c *Cache) MSet(items map[string]Item) error
- func (c *Cache) OnDelete(fn func(key string))
- func (c *Cache) OnEvict(fn func(key string, value interface{}))
- func (c *Cache) OnHit(fn func(key string))
- func (c *Cache) OnMiss(fn func(key string))
- func (c *Cache) OnNodeJoin(node *cluster.NodeInfo)
- func (c *Cache) OnNodeLeave(node *cluster.NodeInfo)
- func (c *Cache) OnNodeUpdate(node *cluster.NodeInfo)
- func (c *Cache) OnSet(fn func(key string, value interface{}))
- func (c *Cache) RateLimit(key string, opts RateLimitOptions) (bool, RateLimitState)
- func (c *Cache) RateLimitSliding(key string, opts SlidingWindowOptions) (bool, RateLimitState)
- func (c *Cache) Ring() *cluster.Ring
- func (c *Cache) Set(key string, value interface{}, ttl time.Duration) error
- func (c *Cache) SetWithTags(key string, value interface{}, ttl time.Duration, tags []string) error
- func (c *Cache) Shutdown() error
- func (c *Cache) Subscribe(pattern string) *Subscription
- func (c *Cache) TTL(key string) time.Duration
- func (c *Cache) TryLock(key string, ttl time.Duration) (*LockHandle, bool)
- func (c *Cache) WithNamespace(namespace string) *NamespacedCache
- type CacheEvent
- type Config
- type EventType
- type Item
- type LockHandle
- type LockOptions
- type Mode
- type NamespacedCache
- func (nc *NamespacedCache) Decr(key string, delta int64) (int64, error)
- func (nc *NamespacedCache) Delete(key string)
- func (nc *NamespacedCache) Exists(key string) bool
- func (nc *NamespacedCache) Get(key string, dest interface{}) bool
- func (nc *NamespacedCache) GetOrSet(key string, dest interface{}, ttl time.Duration, ...) error
- func (nc *NamespacedCache) Incr(key string, delta int64) (int64, error)
- func (nc *NamespacedCache) InvalidateTag(tag string)
- func (nc *NamespacedCache) Set(key string, value interface{}, ttl time.Duration) error
- func (nc *NamespacedCache) SetWithTags(key string, value interface{}, ttl time.Duration, tags []string) error
- func (nc *NamespacedCache) TTL(key string) time.Duration
- type RateLimitOptions
- type RateLimitState
- type SlidingWindowOptions
- type Subscription
Constants ¶
const ( EventSet = pubsub.EventSet EventDelete = pubsub.EventDelete EventExpire = pubsub.EventExpire EventEvict = pubsub.EventEvict )
Event type constants.
Variables ¶
var ( ErrNotFound = errors.New("cachegrid: key not found") ErrExpired = errors.New("cachegrid: key expired") ErrKeyEmpty = errors.New("cachegrid: key must not be empty") ErrSerializationFailed = errors.New("cachegrid: serialization failed") ErrShutdown = errors.New("cachegrid: cache is shut down") ErrNodeNotFound = errors.New("cachegrid: node not found in cluster") ErrRemoteCall = errors.New("cachegrid: remote call failed") ErrLockNotAcquired = errors.New("cachegrid: lock could not be acquired") ErrLockNotHeld = errors.New("cachegrid: lock not held or expired") ErrRateLimited = errors.New("cachegrid: rate limit exceeded") )
Functions ¶
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a sharded in-memory cache with LRU eviction and TTL support. It optionally forms a distributed cluster via gossip and RPC.
func (*Cache) ClusterState ¶
func (c *Cache) ClusterState() *cluster.ClusterState
ClusterState returns the cluster state, or nil if running in local mode.
func (*Cache) GetOrSet ¶
func (c *Cache) GetOrSet(key string, dest interface{}, ttl time.Duration, fn func() (interface{}, error)) error
GetOrSet retrieves a value or computes and stores it on miss.
func (*Cache) HandleDelete ¶
func (*Cache) HandleExists ¶
func (*Cache) HandleLockAcquire ¶
func (*Cache) HandleLockExtend ¶
func (*Cache) InvalidateTag ¶
InvalidateTag deletes all keys associated with the given tag.
func (*Cache) Lock ¶
func (c *Cache) Lock(key string, opts LockOptions) (*LockHandle, error)
Lock acquires a distributed lock with retry logic.
func (*Cache) LockEngine ¶
LockEngine returns the lock engine.
func (*Cache) OnNodeJoin ¶
func (*Cache) OnNodeLeave ¶
func (*Cache) OnNodeUpdate ¶
func (*Cache) RateLimit ¶
func (c *Cache) RateLimit(key string, opts RateLimitOptions) (bool, RateLimitState)
RateLimit checks a token bucket rate limiter for the given key.
func (*Cache) RateLimitSliding ¶
func (c *Cache) RateLimitSliding(key string, opts SlidingWindowOptions) (bool, RateLimitState)
RateLimitSliding checks a sliding window rate limiter for the given key.
func (*Cache) SetWithTags ¶
SetWithTags stores a value with associated tags.
func (*Cache) Subscribe ¶
func (c *Cache) Subscribe(pattern string) *Subscription
Subscribe returns a subscription for events matching the glob pattern.
func (*Cache) WithNamespace ¶
func (c *Cache) WithNamespace(namespace string) *NamespacedCache
WithNamespace returns a namespaced view of the cache. All keys will be prefixed with "namespace:".
type Config ¶
type Config struct {
// NumShards is the number of internal shards. Must be a power of 2.
// Default: 256.
NumShards int
// MaxMemoryMB is the maximum memory in megabytes across all shards.
// 0 means unlimited.
MaxMemoryMB int64
// DefaultTTL is the default expiration for entries when no TTL is specified.
// 0 means no expiry by default.
DefaultTTL time.Duration
// SweeperInterval controls how often the background goroutine
// checks for expired entries. Default: 1 second.
SweeperInterval time.Duration
// ListenAddr is the gossip protocol listen address (e.g., ":7946").
// If empty, the cache runs in local-only mode.
ListenAddr string
// Peers is the list of seed nodes for cluster discovery.
Peers []string
// Mode is the cache distribution strategy.
Mode Mode
// NodeName is a unique identifier for this node. If empty, hostname is used.
NodeName string
// GRPCPort is the port for inter-node gRPC/RPC communication. Default: 7947.
GRPCPort int
// HTTPPort is the HTTP server port for standalone mode. Default: 6380.
HTTPPort int
// VirtualNodes is the number of virtual nodes per physical node on the hash ring.
// Default: 150.
VirtualNodes int
}
Config holds configuration for a Cache instance.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with sensible defaults.
type LockHandle ¶
type LockHandle struct {
// contains filtered or unexported fields
}
LockHandle represents a held distributed lock.
func (*LockHandle) Extend ¶
func (h *LockHandle) Extend(ttl time.Duration) error
Extend extends the lock TTL.
func (*LockHandle) Token ¶
func (h *LockHandle) Token() uint64
Token returns the fencing token for this lock.
type LockOptions ¶
type LockOptions struct {
TTL time.Duration // lock auto-release time (required)
RetryCount int // number of retries (0 = no retry)
RetryDelay time.Duration // delay between retries
}
LockOptions configures lock acquisition behavior.
type NamespacedCache ¶
type NamespacedCache struct {
// contains filtered or unexported fields
}
NamespacedCache wraps a Cache and prefixes all keys with a namespace.
func (*NamespacedCache) Decr ¶
func (nc *NamespacedCache) Decr(key string, delta int64) (int64, error)
Decr decrements a counter in the namespaced cache.
func (*NamespacedCache) Delete ¶
func (nc *NamespacedCache) Delete(key string)
Delete removes a key from the namespaced cache.
func (*NamespacedCache) Exists ¶
func (nc *NamespacedCache) Exists(key string) bool
Exists checks if a key exists in the namespaced cache.
func (*NamespacedCache) Get ¶
func (nc *NamespacedCache) Get(key string, dest interface{}) bool
Get retrieves a value from the namespaced cache.
func (*NamespacedCache) GetOrSet ¶
func (nc *NamespacedCache) GetOrSet(key string, dest interface{}, ttl time.Duration, fn func() (interface{}, error)) error
GetOrSet retrieves or computes a value in the namespaced cache.
func (*NamespacedCache) Incr ¶
func (nc *NamespacedCache) Incr(key string, delta int64) (int64, error)
Incr increments a counter in the namespaced cache.
func (*NamespacedCache) InvalidateTag ¶
func (nc *NamespacedCache) InvalidateTag(tag string)
InvalidateTag invalidates all keys with the given tag in the namespace.
func (*NamespacedCache) Set ¶
func (nc *NamespacedCache) Set(key string, value interface{}, ttl time.Duration) error
Set stores a value in the namespaced cache.
func (*NamespacedCache) SetWithTags ¶
func (nc *NamespacedCache) SetWithTags(key string, value interface{}, ttl time.Duration, tags []string) error
SetWithTags stores a value with tags in the namespaced cache.
type RateLimitOptions ¶
RateLimitOptions configures token bucket rate limiting.
type RateLimitState ¶
RateLimitState is the public rate limit response.
type SlidingWindowOptions ¶
SlidingWindowOptions configures sliding window rate limiting.
type Subscription ¶
type Subscription struct {
// contains filtered or unexported fields
}
Subscription wraps an internal subscription.
func (*Subscription) Close ¶
func (s *Subscription) Close()
Close unsubscribes and closes the event channel.
func (*Subscription) Events ¶
func (s *Subscription) Events() <-chan CacheEvent
Events returns the channel to receive events on.