mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 08:32:40 +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/bytesconv"
|
||||||
"github.com/charlienet/go-mixed/expr"
|
"github.com/charlienet/go-mixed/expr"
|
||||||
"github.com/charlienet/go-mixed/hash"
|
"github.com/charlienet/go-mixed/hash"
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/charlienet/go-mixed/redis"
|
||||||
)
|
)
|
||||||
|
|
||||||
const DEFAULT_SIZE = 2 << 24
|
const DEFAULT_SIZE = 2 << 24
|
||||||
@ -26,13 +26,13 @@ type BloomFilter struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type bloomOptions struct {
|
type bloomOptions struct {
|
||||||
redisClient *redis.Client
|
redisClient redis.Client
|
||||||
redisKey string
|
redisKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
type option func(*bloomOptions)
|
type option func(*bloomOptions)
|
||||||
|
|
||||||
func WithRedis(redis *redis.Client, key string) option {
|
func WithRedis(redis redis.Client, key string) option {
|
||||||
return func(bo *bloomOptions) {
|
return func(bo *bloomOptions) {
|
||||||
bo.redisClient = redis
|
bo.redisClient = redis
|
||||||
bo.redisKey = key
|
bo.redisKey = key
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
"github.com/charlienet/go-mixed/bloom"
|
"github.com/charlienet/go-mixed/bloom"
|
||||||
"github.com/charlienet/go-mixed/rand"
|
"github.com/charlienet/go-mixed/rand"
|
||||||
"github.com/charlienet/go-mixed/sys"
|
"github.com/charlienet/go-mixed/sys"
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/redis/go-redis/v9"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/charlienet/go-mixed/redis"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -38,12 +38,12 @@ var _ bitStore = &redisBitSet{}
|
|||||||
|
|
||||||
// 使用Redis存储位图
|
// 使用Redis存储位图
|
||||||
type redisBitSet struct {
|
type redisBitSet struct {
|
||||||
store *redis.Client
|
store redis.Client
|
||||||
key string
|
key string
|
||||||
bits uint
|
bits uint
|
||||||
}
|
}
|
||||||
|
|
||||||
func newRedisStore(store *redis.Client, key string, bits uint) *redisBitSet {
|
func newRedisStore(store redis.Client, key string, bits uint) *redisBitSet {
|
||||||
return &redisBitSet{
|
return &redisBitSet{
|
||||||
store: store,
|
store: store,
|
||||||
key: key,
|
key: key,
|
||||||
|
@ -1,24 +1,60 @@
|
|||||||
package bloom
|
package bloom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"testing"
|
"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) {
|
func TestRedisStore(t *testing.T) {
|
||||||
client := redis.NewClient(&redis.Options{
|
runOnRedis(t, func(client redis.Client) {
|
||||||
Addr: "192.168.2.222:6379",
|
store := newRedisStore(client, "abcdef", 10000)
|
||||||
Password: "123456",
|
err := store.Set(1, 2, 3, 9, 1223)
|
||||||
})
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
store := newRedisStore(client, "abcdef", 10000)
|
t.Log(store.Test(1))
|
||||||
err := store.Set(1, 2, 3, 9, 1223)
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Log(store.Test(1))
|
addr := mr.Addr()
|
||||||
t.Log(store.Test(1, 2, 3))
|
log.Println("mini redis run at:", addr)
|
||||||
t.Log(store.Test(4, 5, 8))
|
|
||||||
|
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