Redis-based caching system (internal/utils/cache/).
Related Documentation:
- Stream Operations - Pub/sub streaming patterns
- Database Operations - Cache-aside pattern for DB results
- Generic Types - Type-safe cache operations
All cache helpers accept logical names and apply the configured Redis prefix internally.
By default the daemon uses plugin_daemon, and operators can override it with
REDIS_KEY_PREFIX.
// Store and retrieve
cache.Store("key", value, time.Minute*30)
val, err := cache.Get[Type]("key")
cache.Del("key")
// Check existence
exists, _ := cache.Exist("key")For cluster state management:
// Set map field
cache.SetMapOneField(CLUSTER_STATUS_KEY, nodeId, nodeStatus)
// Get single field
node, err := cache.GetMapField[node](CLUSTER_STATUS_KEY, nodeId)
// Get entire map
nodes, err := cache.GetMap[node](CLUSTER_STATUS_KEY)
// Delete field
cache.DelMapField(CLUSTER_STATUS_KEY, nodeId)Pub/sub channels are prefixed with the same REDIS_KEY_PREFIX value as keys, so
callers should always pass logical channel names.
// Publish event
cache.Publish(CHANNEL, event)
// Subscribe to channel
eventChan, cancel := cache.Subscribe[EventType](CHANNEL)
defer cancel()
for event := range eventChan {
// Process event
}// Acquire lock with timeout
acquired := cache.Lock(key, duration, timeout)
if acquired {
defer cache.Unlock(key)
// Critical section
}Helper functions in redis_auto_type.go:
// Get with automatic getter fallback
value := cache.AutoGetWithGetter(key, func() (*Type, error) {
return fetchFromDB()
}, ttl)
// Auto delete
cache.AutoDelete[Type](key)// Store session
sessionKey := fmt.Sprintf("session_info:%s", id)
cache.Store(sessionKey, session, time.Minute*30)
// Retrieve session
session, err := cache.Get[Session](sessionKey)
if err == cache.ErrNotFound {
// Session expired or not found
}Initialize Redis client in main:
cache.InitRedisClient(
addr, // "localhost:6379"
username, // optional
password,
useSsl, // bool
db, // database number
)Redis naming behavior:
REDIS_KEY_PREFIXdefaults toplugin_daemon- applies to keys and pub/sub channels managed by this package
- changing the prefix switches the active Redis namespace and does not migrate old data
- logical keys that contain Redis Cluster hash tags such as
{remote:key:manager}keep those tags intact because the prefix is prepended to the full logical key