mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-17 16:12:42 +08:00
use redis
This commit is contained in:
@ -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
|
||||
|
@ -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"
|
||||
)
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user