1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-17 16:12:42 +08:00

更新redis

This commit is contained in:
2023-10-13 10:52:36 +08:00
parent e83db7daee
commit d1c269ed90
8 changed files with 48 additions and 42 deletions

View File

@ -7,25 +7,20 @@ import (
"github.com/charlienet/go-mixed/cache/bigcache"
"github.com/charlienet/go-mixed/cache/freecache"
"github.com/charlienet/go-mixed/logx"
"github.com/charlienet/go-mixed/redis"
)
const defaultPrefix = "cache"
type option func(*Cache) error
type options struct {
Prefix string
}
func WithRedis(opts RedisConfig) option {
func WithRedis(rdb redis.Client) option {
return func(c *Cache) error {
if len(opts.Prefix) == 0 {
opts.Prefix = defaultPrefix
}
rds := NewRedis(opts)
rds := NewRedis(rdb)
c.rds = rds
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)

12
cache/cache_test.go vendored
View File

@ -6,6 +6,8 @@ import (
"sync/atomic"
"testing"
"time"
"github.com/charlienet/go-mixed/redis"
)
var (
@ -60,7 +62,9 @@ func TestMemCache(t *testing.T) {
}
func TestDistributedCache(t *testing.T) {
c := NewRedis(RedisConfig{Addrs: []string{"192.168.2.222:6379"}, DB: 6, Password: "123456", Prefix: "abcdef"})
c := NewRedis(redis.New(&redis.ReidsOption{
Addrs: []string{"192.168.2.222:6379"}, DB: 6, Password: "123456", Prefix: "abcdef",
}))
ctx := context.Background()
if err := c.Ping(ctx); err != nil {
@ -134,7 +138,11 @@ func load() (any, error) {
func buildCache() *Cache {
c, err := New(
WithFreeCache(10*1024*1024),
WithRedis(RedisConfig{Addrs: []string{"192.168.2.222:6379"}, DB: 6, Password: "123456"}))
WithRedis(redis.New(&redis.ReidsOption{
Addrs: []string{"192.168.2.222:6379"},
DB: 6,
Password: "123456",
})))
if err != nil {
panic(err)

5
cache/readme.md vendored
View File

@ -1,4 +1,7 @@
# 级缓存模块
# 级缓存模块
提供本地缓存和分布式缓存组合的缓存模块,可以只使用本地缓存或分布式缓存。并可全局禁用缓存。
1. 一级缓存可使用freecache或bigcache作为本地缓存当数据在本地缓存不存在时会向二级缓存请求数据
2. 二级缓存使用redis作为缓存模块当数据在二级缓存不存在时向资源请求数据。

35
cache/redis.go vendored
View File

@ -9,48 +9,21 @@ import (
"github.com/charlienet/go-mixed/bytesconv"
"github.com/charlienet/go-mixed/json"
"github.com/charlienet/go-mixed/rand"
"github.com/go-redis/redis/v8"
"github.com/charlienet/go-mixed/redis"
)
const redisEmptyObject = "redis object not exist"
type RedisConfig struct {
Prefix string // key perfix
Addrs []string
// Database to be selected after connecting to the server.
// Only single-node and failover clients.
DB int
Username string
Password string
MaxRetries int
MinRetryBackoff time.Duration
MaxRetryBackoff time.Duration
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
}
type redisClient struct {
client redis.UniversalClient
client redis.Client
emptyStamp string // 空对象标识,每个实例隔离
prefix string // 缓存键前缀
}
func NewRedis(c RedisConfig) *redisClient {
client := redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: c.Addrs,
DB: c.DB,
Username: c.Username,
Password: c.Password,
})
func NewRedis(c redis.Client) *redisClient {
return &redisClient{
emptyStamp: fmt.Sprintf("redis-empty-%d-%s", time.Now().Unix(), rand.Hex.Generate(6)),
prefix: c.Prefix,
client: client,
client: c,
}
}

View File

@ -132,3 +132,4 @@ func TestPubSub(t *testing.T) {
t.Logf("total received %d message", total)
})
}

View File

@ -60,6 +60,9 @@ func (r renameKey) renameKey(cmd redis.Cmder) {
case "RENAME", "RENAMENX", "MGET", "BLPOP", "BRPOP", "RPOPLPUSH", "SDIFFSTORE", "SINTER", "SINTERSTORE", "SUNIONSTORE":
// 连续KEY
r.rename(args, createSepuence(1, len(args), 1)...)
case "sssss":
// 除最后一个外连续键
r.rename(args, createSepuence(1, len(args)-1, 1)...)
case "MSET", "MSETNX":
// 间隔KEYKEY位置规则1,3,5,7
r.rename(args, createSepuence(1, len(args), 2)...)

View File

@ -0,0 +1,9 @@
package redis
import "testing"
func TestRename(t *testing.T) {
New(&ReidsOption{
Addrs: []string{"192.168.123.100:6379"},
})
}

View File

@ -1,6 +1,7 @@
package tests
import (
"context"
"log"
"testing"
"time"
@ -10,6 +11,19 @@ import (
"github.com/stretchr/testify/assert"
)
func RunOnSpecifiedRedis(t *testing.T, fn func(client redis.Client), addr ...string) {
rdb := redis.New(&redis.ReidsOption{
Addrs: addr,
})
defer rdb.Close()
if err := rdb.Ping(context.Background()).Err(); err != nil {
t.Fatal(err)
}
fn(rdb)
}
func RunOnRedis(t *testing.T, fn func(client redis.Client)) {
redis, clean, err := createMiniRedis()
assert.Nil(t, err)