GoFrame Redis Example
Github Source: https://github.com/gogf/examples/tree/main/nosql/redis
This example demonstrates how to use Redis with GoFrame framework.
Overview
This example shows:
- How to configure
Redisconnection usingYAMLconfiguration - How to create a
Redisclient - Basic
Redisoperations (SET/GET)
Requirements
Go 1.15or higherRedisserverGoFrame v2
Configuration
The Redis configuration is stored in config.yaml:
redis:
address: "127.0.0.1:6379"
password:
You can modify these settings according to your Redis server configuration.
Running Redis with Docker
If you don't have Redis installed locally, you can quickly start a Redis instance using Docker:
# Run Redis container
docker run --name redis-test -p 6379:6379 -d redis:latest
# Verify the container is running
docker ps
# If you need to stop the container later
docker stop redis-test
# If you need to remove the container
docker rm redis-test
For Redis with password authentication:
# Run Redis with password
docker run --name redis-test -p 6379:6379 -d redis:latest redis-server --requirepass your_password
# Remember to update config.yaml accordingly:
# redis:
# address: "127.0.0.1:6379"
# password: "your_password"
Running the Example
- Make sure your
Redisserver is running - Update the
config.yamlif needed - Run the example:
go run main.go
Code Structure
main.go: Contains the main logic andRedisclient initializationconfig.yaml:Redisconfiguration file
Features
Redisclient initialization with error handling- Configuration management using
GoFrame's configuration system - Basic
Redisoperations demonstration - Proper error handling and logging
Further Reading
For more advanced Redis usage, please refer to the third-party package github.com/redis/go-redis.
Notes
- The example uses
go-redis/v9client - All operations are performed with context for proper cancellation and timeout handling
- The code includes proper error handling and logging