Skip to content

Latest commit

 

History

History
121 lines (90 loc) · 2.68 KB

File metadata and controls

121 lines (90 loc) · 2.68 KB

Cache Operations

Redis-based caching system (internal/utils/cache/).

Related Documentation:

Basic 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")

Map Operations

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 Pattern

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
}

Distributed Locks

// Acquire lock with timeout
acquired := cache.Lock(key, duration, timeout)
if acquired {
    defer cache.Unlock(key)
    // Critical section
}

Auto-type Operations

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)

Session Management Example

// 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
}

Configuration

Initialize Redis client in main:

cache.InitRedisClient(
    addr,      // "localhost:6379"
    username,  // optional
    password,  
    useSsl,    // bool
    db,        // database number
)

Redis naming behavior:

  • REDIS_KEY_PREFIX defaults to plugin_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