mirror of
https://github.com/click33/sa-token-go.git
synced 2025-12-24 13:48:04 +08:00
2.0 KiB
2.0 KiB
Redis Storage Example
中文说明 | English
This example demonstrates how to use Redis as the storage backend for Sa-Token-Go.
Prerequisites
- Redis server running on
localhost:6379(or setREDIS_ADDRenvironment variable) - Go 1.21 or higher
Install Redis
macOS
brew install redis
brew services start redis
Linux (Ubuntu/Debian)
sudo apt-get install redis-server
sudo systemctl start redis
Docker
docker run -d -p 6379:6379 redis:7-alpine
Run Example
# Without password
go run main.go
# With password
REDIS_PASSWORD=your-password go run main.go
# Custom Redis address
REDIS_ADDR=redis.example.com:6379 go run main.go
Key Features Demonstrated
- ✅ Redis Connection - Connect to Redis with go-redis
- ✅ Authentication - Login/Logout with Redis storage
- ✅ Permission Management - Store permissions in Redis
- ✅ Role Management - Store roles in Redis
- ✅ Session Management - Persistent session data
- ✅ Data Persistence - Data survives application restarts
Environment Variables
| Variable | Description | Default |
|---|---|---|
REDIS_ADDR |
Redis server address | localhost:6379 |
REDIS_PASSWORD |
Redis password | (empty) |
REDIS_DB |
Redis database number | 0 |
View Data in Redis
# Connect to Redis CLI
redis-cli
# List all Sa-Token keys
KEYS satoken:*
# View token info
GET satoken:login:token:{your-token}
# View session data
GET satoken:session:1000
# View permissions
SMEMBERS satoken:permission:1000
# View roles
SMEMBERS satoken:role:1000
Production Deployment
See Redis Storage Guide for:
- Connection pool configuration
- High availability (Sentinel)
- Cluster mode
- TLS/SSL support
- Docker/Kubernetes deployment