Redis Cache
5/14/25About 1 mincommoncachecode generation
Generate Cache Code
Generate Code
In the sponge UI interface:
- Click the left menu bar [Public] → [Generate Cache Code];
- Fill in the parameters according to the interface prompts. You can view the parameter descriptions by hovering over the
?
icon.
After filling in the parameters, click the Download Code
button to generate the cache code, as shown in the figure below:

Equivalent Command
# Full command
sponge web cache --module-name=user --cache-name=userToken --prefix-key=user:token: --key-name=id --key-type=uint64 --value-name=token --value-type=string
# Simplified command (use --out to specify the service code directory, the generated code will be automatically merged into the specified service directory)
sponge web cache --cache-name=userToken --prefix-key=user:token: --key-name=id --key-type=uint64 --value-name=token --value-type=string --out=user
Code Directory Structure
The generated cache code directory structure is as follows:
.
└─ internal
└─ cache # Contains the generated cache interface implementation and test files
Code Integration Instructions
Integration Steps
- Unzip the generated code package
- Move the entire
internal
directory to the root directory of the target service (web or gRPC) code
Notes
- If a conflict prompt appears, it means that the same cache configuration already exists and can be safely overwritten.
Cache Code Usage
Web Service Invocation Method
Locate File: internal/handler/
- Services created based on SQL:
table_name.go
- Services created based on Protobuf:
proto_file_name.go
Reference Example:
type userHandler struct {
userTokenCache cache.UserTokenCache // Inject cache interface
}
func NewUserHandler() UserHandler {
return &userHandler{
userTokenCache: cache.NewUserTokenCache(database.GetRedisCli()) // Initalize cache
}
}
// All cache operations (Get/Set/Delete, etc.) can be used directly.
gRPC Service Invocation Method
Locate File: internal/service/
- Services created based on SQL:
table_name.go
- Services created based on Protobuf:
proto_file_name.go
Reference Example:
type userService struct {
userV1.UnimplementedUserServer
userTokenCache cache.UserTokenCache // Inject cache interface
}
func NewUserServer() userV1.UserServer {
return &user{
userTokenCache: cache.NewUserTokenCache(database.GetRedisCli()) // Initalize cache
}
}
// All cache operations (Get/Set/Delete, etc.) can be used directly.