wip: oci
This commit is contained in:
74
example/redis-cache/simple-test.go
Normal file
74
example/redis-cache/simple-test.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"gitea.loveuer.com/loveuer/upkg/database/cache"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("=== Redis Cache Package Test ===")
|
||||
|
||||
// 创建配置
|
||||
config := cache.NewConfig("redis", "redis-headless.redis-demo.svc.cluster.local:6379")
|
||||
config.MasterAddr = "redis-0.redis-headless.redis-demo.svc.cluster.local:6379"
|
||||
config.ReplicaAddrs = []string{
|
||||
"redis-1.redis-headless.redis-demo.svc.cluster.local:6379",
|
||||
"redis-2.redis-headless.redis-demo.svc.cluster.local:6379",
|
||||
}
|
||||
config.Reconnect = true
|
||||
config.ReconnectInterval = 10 * time.Second
|
||||
|
||||
// 连接 Redis
|
||||
redisCache, err := cache.NewRedis(config)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to Redis: %v", err)
|
||||
}
|
||||
defer redisCache.Close()
|
||||
|
||||
fmt.Println("✅ Connected to Redis cluster")
|
||||
|
||||
// 测试基本操作
|
||||
ctx := context.Background()
|
||||
|
||||
// SET/GET
|
||||
err = redisCache.Set(ctx, "test-key", "hello-world", 0)
|
||||
if err != nil {
|
||||
log.Printf("SET error: %v", err)
|
||||
} else {
|
||||
val, _ := redisCache.Get(ctx, "test-key")
|
||||
fmt.Printf("✅ SET/GET: %s\n", val)
|
||||
}
|
||||
|
||||
// Hash 操作
|
||||
err = redisCache.HSet(ctx, "user:1", "name", "张三")
|
||||
if err == nil {
|
||||
redisCache.HSet(ctx, "user:1", "age", "25")
|
||||
name, _ := redisCache.HGet(ctx, "user:1", "name")
|
||||
age, _ := redisCache.HGet(ctx, "user:1", "age")
|
||||
fmt.Printf("✅ HSET/HGET: name=%s, age=%s\n", name, age)
|
||||
}
|
||||
|
||||
// 计数器
|
||||
redisCache.Set(ctx, "counter", "0")
|
||||
count1, _ := redisCache.Inc(ctx, "counter")
|
||||
count2, _ := redisCache.IncBy(ctx, "counter", 5)
|
||||
fmt.Printf("✅ INCR/INCRBY: %d, %d\n", count1, count2)
|
||||
|
||||
// 测试重连
|
||||
fmt.Println("🔄 Testing reconnection (will check every 10 seconds)...")
|
||||
time.Sleep(35 * time.Second)
|
||||
|
||||
// 再次测试操作,验证重连功能
|
||||
testVal, err := redisCache.Get(ctx, "test-key")
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Reconnection failed: %v\n", err)
|
||||
} else {
|
||||
fmt.Printf("✅ Reconnection successful: %s\n", testVal)
|
||||
}
|
||||
|
||||
fmt.Println("🎉 All tests completed successfully!")
|
||||
}
|
||||
Reference in New Issue
Block a user