Use this skill to implement Redis cache patterns. It provides templates for Redis initialization (standalone/cluster) and advanced caching strategies (MultiGet, Pipeline, SingleFlight).
This skill provides standard patterns for integrating Redis into go-zero services.
Create infra/cfg/redis.go (or similar utility path) using the standard template to handle connection logic.
Template Usage:
templates/redis.go.tmplinfra/cfg/redis.go{{PACKAGE_NAME}}: Package name (e.g., cfg)Update Service config struct (internal/config/config.go) and YAML file (etc/service.yaml).
Config Struct:
type Config struct {
// ...
RedisConf cfg.RedisConf
}
YAML Config:
RedisConf:
Host: 127.0.0.1:6379
Type: node # or cluster
Pass: ""
In internal/svc/service_context.go:
// Initialize Redis
rds, err := cfg.InitRedis(c.RedisConf)
if err != nil {
panic(err)
}
s.Redis = rds
Refer to RdsMultiGet in the references or previous code examples for batch fetching to reduce network RTT.
Use go-zero/core/stores/cache for anti-breakdown patterns.
templates/redis.go.tmpl: Universal Redis client initializer.