diff --git a/bloom/bloom.go b/bloom/bloom.go index a35aec1..a29fe32 100644 --- a/bloom/bloom.go +++ b/bloom/bloom.go @@ -6,7 +6,7 @@ import ( "github.com/charlienet/go-mixed/bytesconv" "github.com/charlienet/go-mixed/expr" "github.com/charlienet/go-mixed/hash" - "github.com/go-redis/redis/v8" + "github.com/charlienet/go-mixed/redis" ) const DEFAULT_SIZE = 2 << 24 @@ -26,13 +26,13 @@ type BloomFilter struct { } type bloomOptions struct { - redisClient *redis.Client + redisClient redis.Client redisKey string } type option func(*bloomOptions) -func WithRedis(redis *redis.Client, key string) option { +func WithRedis(redis redis.Client, key string) option { return func(bo *bloomOptions) { bo.redisClient = redis bo.redisKey = key diff --git a/bloom/boom_test.go b/bloom/boom_test.go index 6c7ef60..3dd47a0 100644 --- a/bloom/boom_test.go +++ b/bloom/boom_test.go @@ -9,7 +9,7 @@ import ( "github.com/charlienet/go-mixed/bloom" "github.com/charlienet/go-mixed/rand" "github.com/charlienet/go-mixed/sys" - "github.com/go-redis/redis/v8" + "github.com/redis/go-redis/v9" "github.com/stretchr/testify/assert" ) diff --git a/bloom/redis_store.go b/bloom/redis_store.go index c7019df..7feaa62 100644 --- a/bloom/redis_store.go +++ b/bloom/redis_store.go @@ -6,7 +6,7 @@ import ( "strconv" "time" - "github.com/go-redis/redis/v8" + "github.com/charlienet/go-mixed/redis" ) const ( @@ -38,12 +38,12 @@ var _ bitStore = &redisBitSet{} // 使用Redis存储位图 type redisBitSet struct { - store *redis.Client + store redis.Client key string bits uint } -func newRedisStore(store *redis.Client, key string, bits uint) *redisBitSet { +func newRedisStore(store redis.Client, key string, bits uint) *redisBitSet { return &redisBitSet{ store: store, key: key, diff --git a/bloom/redis_store_test.go b/bloom/redis_store_test.go index 834f943..09606ca 100644 --- a/bloom/redis_store_test.go +++ b/bloom/redis_store_test.go @@ -1,24 +1,60 @@ package bloom import ( + "log" "testing" + "time" - "github.com/go-redis/redis/v8" + "github.com/alicebob/miniredis/v2" + "github.com/charlienet/go-mixed/redis" + "github.com/stretchr/testify/assert" ) func TestRedisStore(t *testing.T) { - client := redis.NewClient(&redis.Options{ - Addr: "192.168.2.222:6379", - Password: "123456", - }) + runOnRedis(t, func(client redis.Client) { + store := newRedisStore(client, "abcdef", 10000) + err := store.Set(1, 2, 3, 9, 1223) + if err != nil { + t.Fatal(err) + } - store := newRedisStore(client, "abcdef", 10000) - err := store.Set(1, 2, 3, 9, 1223) + t.Log(store.Test(1)) + t.Log(store.Test(1, 2, 3)) + t.Log(store.Test(4, 5, 8)) + }) +} + +func runOnRedis(t *testing.T, fn func(client redis.Client)) { + redis, clean, err := CreateMiniRedis() + assert.Nil(t, err) + + defer clean() + + fn(redis) +} + +func CreateMiniRedis() (r redis.Client, clean func(), err error) { + mr, err := miniredis.Run() if err != nil { - t.Fatal(err) + return nil, nil, err } - t.Log(store.Test(1)) - t.Log(store.Test(1, 2, 3)) - t.Log(store.Test(4, 5, 8)) + addr := mr.Addr() + log.Println("mini redis run at:", addr) + + return redis.New(&redis.ReidsOption{ + Addrs: []string{addr}, + }), func() { + ch := make(chan struct{}) + + go func() { + mr.Close() + close(ch) + }() + + select { + case <-ch: + case <-time.After(time.Second): + } + }, nil }