mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
更新redis
This commit is contained in:
11
cache/cache_builder.go
vendored
11
cache/cache_builder.go
vendored
@ -7,25 +7,20 @@ import (
|
|||||||
"github.com/charlienet/go-mixed/cache/bigcache"
|
"github.com/charlienet/go-mixed/cache/bigcache"
|
||||||
"github.com/charlienet/go-mixed/cache/freecache"
|
"github.com/charlienet/go-mixed/cache/freecache"
|
||||||
"github.com/charlienet/go-mixed/logx"
|
"github.com/charlienet/go-mixed/logx"
|
||||||
|
"github.com/charlienet/go-mixed/redis"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultPrefix = "cache"
|
const defaultPrefix = "cache"
|
||||||
|
|
||||||
|
|
||||||
type option func(*Cache) error
|
type option func(*Cache) error
|
||||||
|
|
||||||
type options struct {
|
type options struct {
|
||||||
Prefix string
|
Prefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithRedis(opts RedisConfig) option {
|
func WithRedis(rdb redis.Client) option {
|
||||||
return func(c *Cache) error {
|
return func(c *Cache) error {
|
||||||
if len(opts.Prefix) == 0 {
|
rds := NewRedis(rdb)
|
||||||
opts.Prefix = defaultPrefix
|
|
||||||
}
|
|
||||||
|
|
||||||
rds := NewRedis(opts)
|
|
||||||
|
|
||||||
c.rds = rds
|
c.rds = rds
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
|
||||||
|
12
cache/cache_test.go
vendored
12
cache/cache_test.go
vendored
@ -6,6 +6,8 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/charlienet/go-mixed/redis"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -60,7 +62,9 @@ func TestMemCache(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestDistributedCache(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()
|
ctx := context.Background()
|
||||||
if err := c.Ping(ctx); err != nil {
|
if err := c.Ping(ctx); err != nil {
|
||||||
@ -134,7 +138,11 @@ func load() (any, error) {
|
|||||||
func buildCache() *Cache {
|
func buildCache() *Cache {
|
||||||
c, err := New(
|
c, err := New(
|
||||||
WithFreeCache(10*1024*1024),
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
5
cache/readme.md
vendored
5
cache/readme.md
vendored
@ -1,4 +1,7 @@
|
|||||||
# 多级缓存模块
|
# 二级缓存模块
|
||||||
|
|
||||||
|
提供本地缓存和分布式缓存组合的缓存模块,可以只使用本地缓存或分布式缓存。并可全局禁用缓存。
|
||||||
|
|
||||||
|
|
||||||
1. 一级缓存可使用freecache或bigcache作为本地缓存,当数据在本地缓存不存在时,会向二级缓存请求数据
|
1. 一级缓存可使用freecache或bigcache作为本地缓存,当数据在本地缓存不存在时,会向二级缓存请求数据
|
||||||
2. 二级缓存使用redis作为缓存模块,当数据在二级缓存不存在时向资源请求数据。
|
2. 二级缓存使用redis作为缓存模块,当数据在二级缓存不存在时向资源请求数据。
|
||||||
|
35
cache/redis.go
vendored
35
cache/redis.go
vendored
@ -9,48 +9,21 @@ import (
|
|||||||
"github.com/charlienet/go-mixed/bytesconv"
|
"github.com/charlienet/go-mixed/bytesconv"
|
||||||
"github.com/charlienet/go-mixed/json"
|
"github.com/charlienet/go-mixed/json"
|
||||||
"github.com/charlienet/go-mixed/rand"
|
"github.com/charlienet/go-mixed/rand"
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/charlienet/go-mixed/redis"
|
||||||
)
|
)
|
||||||
|
|
||||||
const redisEmptyObject = "redis object not exist"
|
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 {
|
type redisClient struct {
|
||||||
client redis.UniversalClient
|
client redis.Client
|
||||||
emptyStamp string // 空对象标识,每个实例隔离
|
emptyStamp string // 空对象标识,每个实例隔离
|
||||||
prefix string // 缓存键前缀
|
prefix string // 缓存键前缀
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRedis(c RedisConfig) *redisClient {
|
func NewRedis(c redis.Client) *redisClient {
|
||||||
client := redis.NewUniversalClient(&redis.UniversalOptions{
|
|
||||||
Addrs: c.Addrs,
|
|
||||||
DB: c.DB,
|
|
||||||
Username: c.Username,
|
|
||||||
Password: c.Password,
|
|
||||||
})
|
|
||||||
|
|
||||||
return &redisClient{
|
return &redisClient{
|
||||||
emptyStamp: fmt.Sprintf("redis-empty-%d-%s", time.Now().Unix(), rand.Hex.Generate(6)),
|
emptyStamp: fmt.Sprintf("redis-empty-%d-%s", time.Now().Unix(), rand.Hex.Generate(6)),
|
||||||
prefix: c.Prefix,
|
client: c,
|
||||||
client: client,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,3 +132,4 @@ func TestPubSub(t *testing.T) {
|
|||||||
t.Logf("total received %d message", total)
|
t.Logf("total received %d message", total)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,6 +60,9 @@ func (r renameKey) renameKey(cmd redis.Cmder) {
|
|||||||
case "RENAME", "RENAMENX", "MGET", "BLPOP", "BRPOP", "RPOPLPUSH", "SDIFFSTORE", "SINTER", "SINTERSTORE", "SUNIONSTORE":
|
case "RENAME", "RENAMENX", "MGET", "BLPOP", "BRPOP", "RPOPLPUSH", "SDIFFSTORE", "SINTER", "SINTERSTORE", "SUNIONSTORE":
|
||||||
// 连续KEY
|
// 连续KEY
|
||||||
r.rename(args, createSepuence(1, len(args), 1)...)
|
r.rename(args, createSepuence(1, len(args), 1)...)
|
||||||
|
case "sssss":
|
||||||
|
// 除最后一个外连续键
|
||||||
|
r.rename(args, createSepuence(1, len(args)-1, 1)...)
|
||||||
case "MSET", "MSETNX":
|
case "MSET", "MSETNX":
|
||||||
// 间隔KEY,KEY位置规则1,3,5,7
|
// 间隔KEY,KEY位置规则1,3,5,7
|
||||||
r.rename(args, createSepuence(1, len(args), 2)...)
|
r.rename(args, createSepuence(1, len(args), 2)...)
|
||||||
|
9
redis/rename_hook_test.go
Normal file
9
redis/rename_hook_test.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package redis
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestRename(t *testing.T) {
|
||||||
|
New(&ReidsOption{
|
||||||
|
Addrs: []string{"192.168.123.100:6379"},
|
||||||
|
})
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package tests
|
package tests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -10,6 +11,19 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"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)) {
|
func RunOnRedis(t *testing.T, fn func(client redis.Client)) {
|
||||||
redis, clean, err := createMiniRedis()
|
redis, clean, err := createMiniRedis()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
Reference in New Issue
Block a user