GoFrame Redis Example
Code 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
Redis
connection usingYAML
configuration - How to create a
Redis
client - Basic
Redis
operations (SET
/GET
)
Requirements
Go 1.15
or higherRedis
serverGoFrame 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
Redis
server is running - Update the
config.yaml
if needed - Run the example:
go run main.go
Code Structure
main.go
: Contains the main logic andRedis
client initializationconfig.yaml
:Redis
configuration file
Features
Redis
client initialization with error handling- Configuration management using
GoFrame
's configuration system - Basic
Redis
operations 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/v9
client - All operations are performed with context for proper cancellation and timeout handling
- The code includes proper error handling and logging