Skip to content

Commit ce792d3

Browse files
Merge pull request #5 from RedisBloom/examples
[add] adds example for NewClientFromPool
2 parents 22e7dd7 + f24eea0 commit ce792d3

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

client.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package redis_bloom_go
22

33
import (
44
"errors"
5-
"strings"
6-
"strconv"
75
"github.com/gomodule/redigo/redis"
6+
"strconv"
7+
"strings"
88
)
99

1010
// TODO: refactor this hard limit and revise client locking
@@ -35,7 +35,6 @@ func NewClient(addr, name string, authPass *string) *Client {
3535
return ret
3636
}
3737

38-
3938
// NewClientFromPool creates a new Client with the given pool and client name
4039
func NewClientFromPool(pool *redis.Pool, name string) *Client {
4140
ret := &Client{
@@ -53,11 +52,10 @@ func NewClientFromPool(pool *redis.Pool, name string) *Client {
5352
func (client *Client) Reserve(key string, error_rate float64, capacity uint64) (err error) {
5453
conn := client.Pool.Get()
5554
defer conn.Close()
56-
_, err = conn.Do("BF.RESERVE", key, strconv.FormatFloat(error_rate, 'g', 16, 64), capacity)
55+
_, err = conn.Do("BF.RESERVE", key, strconv.FormatFloat(error_rate, 'g', 16, 64), capacity)
5756
return err
5857
}
5958

60-
6159
// Add - Add (or create and add) a new value to the filter
6260
// args:
6361
// key - the name of the filter
@@ -88,7 +86,7 @@ func (client *Client) Info(key string) (info map[string]int64, err error) {
8886
if err != nil {
8987
return nil, err
9088
}
91-
89+
9290
values, err := redis.Values(result, nil)
9391
if err != nil {
9492
return nil, err
@@ -98,7 +96,7 @@ func (client *Client) Info(key string) (info map[string]int64, err error) {
9896
}
9997
info = map[string]int64{}
10098
for i := 0; i < len(values); i += 2 {
101-
key, err = redis.String(values[i], nil)
99+
key, err = redis.String(values[i], nil)
102100
if err != nil {
103101
return nil, err
104102
}
@@ -107,5 +105,5 @@ func (client *Client) Info(key string) (info map[string]int64, err error) {
107105
return nil, err
108106
}
109107
}
110-
return info, nil
108+
return info, nil
111109
}

client_test.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package redis_bloom_go
22

33
import (
4+
"github.com/gomodule/redigo/redis"
5+
"github.com/stretchr/testify/assert"
46
"os"
57
"testing"
68
"time"
7-
"github.com/gomodule/redigo/redis"
8-
"github.com/stretchr/testify/assert"
99
)
1010

11-
1211
func getTestConnectionDetails() (string, string) {
1312
value, exists := os.LookupEnv("REDISBLOOM_TEST_HOST")
1413
host := "localhost:6379"
@@ -32,7 +31,6 @@ func createClient() *Client {
3231
return NewClient(host, "test_client", ptr)
3332
}
3433

35-
3634
func TestNewClientFromPool(t *testing.T) {
3735
host, password := getTestConnectionDetails()
3836
pool := &redis.Pool{Dial: func() (redis.Conn, error) {
@@ -65,33 +63,33 @@ func TestReserve(t *testing.T) {
6563
key := "test_RESERVE"
6664
err := client.Reserve(key, 0.1, 1000)
6765
assert.Nil(t, err)
68-
66+
6967
info, err := client.Info(key)
7068
assert.Nil(t, err)
7169
assert.Equal(t, info, map[string]int64{
72-
"Capacity": 1000,
73-
"Expansion rate": 2,
74-
"Number of filters": 1,
75-
"Number of items inserted": 0,
76-
"Size": 932,
70+
"Capacity": 1000,
71+
"Expansion rate": 2,
72+
"Number of filters": 1,
73+
"Number of items inserted": 0,
74+
"Size": 932,
7775
})
78-
76+
7977
err = client.Reserve(key, 0.1, 1000)
8078
assert.NotNil(t, err)
8179
}
8280

8381
func TestAdd(t *testing.T) {
8482
client.FlushAll()
8583
key := "test_ADD"
86-
value := "test_ADD_value";
84+
value := "test_ADD_value"
8785
exists, err := client.Add(key, value)
8886
assert.Nil(t, err)
8987
assert.True(t, exists)
90-
88+
9189
info, err := client.Info(key)
9290
assert.Nil(t, err)
9391
assert.NotNil(t, info)
94-
92+
9593
exists, err = client.Add(key, value)
9694
assert.Nil(t, err)
9795
assert.False(t, exists)
@@ -100,11 +98,11 @@ func TestAdd(t *testing.T) {
10098
func TestExists(t *testing.T) {
10199
client.FlushAll()
102100
client.Add("test_ADD", "test_EXISTS")
103-
101+
104102
exists, err := client.Exists("test_ADD", "test_EXISTS")
105103
assert.Nil(t, err)
106104
assert.True(t, exists)
107-
105+
108106
exists, err = client.Exists("test_ADD", "test_EXISTS1")
109107
assert.Nil(t, err)
110108
assert.False(t, exists)

example_client_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package redis_bloom_go_test
33
import (
44
"fmt"
55
redisbloom "github.com/RedisBloom/redisbloom-go"
6+
"github.com/gomodule/redigo/redis"
7+
"log"
68
)
79

810
// exemplifies the NewClient function
@@ -21,6 +23,30 @@ func ExampleNewClient() {
2123
fmt.Println("Error:", err)
2224
}
2325
fmt.Println("myItem exists in mytest: ", exists)
24-
// myItem exists in mytest: true
26+
// Output: myItem exists in mytest: true
27+
28+
}
29+
30+
// exemplifies the NewClientFromPool function
31+
func ExampleNewClientFromPool() {
32+
host := "localhost:6379"
33+
password := ""
34+
pool := &redis.Pool{Dial: func() (redis.Conn, error) {
35+
return redis.Dial("tcp", host, redis.DialPassword(password))
36+
}}
37+
client := redisbloom.NewClientFromPool(pool, "bloom-client-1")
38+
39+
// BF.ADD mytest item
40+
_, err := client.Add("mytest", "myItem")
41+
if err != nil {
42+
log.Fatalf("Error: %v", err)
43+
}
44+
45+
exists, err := client.Exists("mytest", "myItem")
46+
if err != nil {
47+
log.Fatalf("Error: %v", err)
48+
}
49+
fmt.Println("myItem exists in mytest: ", exists)
50+
// Output: myItem exists in mytest: true
2551

2652
}

pool.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type ConnPool interface {
1717
type SingleHostPool struct {
1818
*redis.Pool
1919
}
20+
2021
//
2122
//func (s SingleHostPool) Close() {
2223
// s.Pool.Close()
@@ -39,7 +40,6 @@ type MultiHostPool struct {
3940
authPass *string
4041
}
4142

42-
4343
func (p *MultiHostPool) Close() (err error) {
4444
p.Lock()
4545
defer p.Unlock()
@@ -57,7 +57,6 @@ func (p *MultiHostPool) Close() (err error) {
5757
return
5858
}
5959

60-
6160
func NewMultiHostPool(hosts []string, authPass *string) *MultiHostPool {
6261
return &MultiHostPool{
6362
pools: make(map[string]*redis.Pool, len(hosts)),

0 commit comments

Comments
 (0)