Skip to content

Commit 06744d2

Browse files
committed
Update dependencies
1 parent a271fa0 commit 06744d2

File tree

13 files changed

+287
-147
lines changed

13 files changed

+287
-147
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Create a backend cache instance and install the interceptor:
3434
import (
3535
"database/sql"
3636

37-
redis "github.com/go-redis/redis/v7"
37+
"github.com/go-redis/redis/v8"
3838
"github.com/jackc/pgx/v4/stdlib"
3939
"github.com/prashanthpai/sqlcache"
4040
)

cache/cache.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cache
22

33
import (
4+
"context"
45
"database/sql/driver"
56
"time"
67
)
@@ -17,7 +18,7 @@ type Cacher interface {
1718
// Get must return a pointer to the item, a boolean representing whether
1819
// item is present or not, and an error (must be nil when key is not
1920
// present).
20-
Get(key string) (*Item, bool, error)
21+
Get(ctx context.Context, key string) (*Item, bool, error)
2122
// Set sets the item into cache with the given TTL.
22-
Set(key string, item *Item, ttl time.Duration) error
23+
Set(ctx context.Context, key string, item *Item, ttl time.Duration) error
2324
}

cache_redis.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package sqlcache
22

33
import (
4+
"context"
45
"time"
56

67
"github.com/prashanthpai/sqlcache/cache"
78

8-
redis "github.com/go-redis/redis/v7"
9-
msgpack "github.com/vmihailenco/msgpack/v4"
9+
"github.com/go-redis/redis/v8"
10+
"github.com/vmihailenco/msgpack/v4"
1011
)
1112

1213
// Redis implements cache.Cacher interface to use redis as backend with
@@ -18,8 +19,8 @@ type Redis struct {
1819

1920
// Get gets a cache item from redis. Returns pointer to the item, a boolean
2021
// which represents whether key exists or not and an error.
21-
func (r *Redis) Get(key string) (*cache.Item, bool, error) {
22-
b, err := r.c.Get(r.keyPrefix + key).Bytes()
22+
func (r *Redis) Get(ctx context.Context, key string) (*cache.Item, bool, error) {
23+
b, err := r.c.Get(ctx, r.keyPrefix+key).Bytes()
2324
switch err {
2425
case nil:
2526
var item cache.Item
@@ -35,13 +36,13 @@ func (r *Redis) Get(key string) (*cache.Item, bool, error) {
3536
}
3637

3738
// Set sets the given item into redis with provided TTL duration.
38-
func (r *Redis) Set(key string, item *cache.Item, ttl time.Duration) error {
39+
func (r *Redis) Set(ctx context.Context, key string, item *cache.Item, ttl time.Duration) error {
3940
b, err := msgpack.Marshal(item)
4041
if err != nil {
4142
return err
4243
}
4344

44-
_, err = r.c.Set(r.keyPrefix+key, b, ttl).Result()
45+
_, err = r.c.Set(ctx, r.keyPrefix+key, b, ttl).Result()
4546
return err
4647
}
4748

cache_ristretto.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sqlcache
22

33
import (
4+
"context"
45
"fmt"
56
"time"
67

@@ -17,7 +18,7 @@ type Ristretto struct {
1718

1819
// Get gets a cache item from ristretto. Returns pointer to the item, a boolean
1920
// which represents whether key exists or not and an error.
20-
func (r *Ristretto) Get(key string) (*cache.Item, bool, error) {
21+
func (r *Ristretto) Get(ctx context.Context, key string) (*cache.Item, bool, error) {
2122
i, ok := r.c.Get(key)
2223
if !ok {
2324
return nil, false, nil
@@ -32,7 +33,7 @@ func (r *Ristretto) Get(key string) (*cache.Item, bool, error) {
3233
}
3334

3435
// Set sets the given item into ristretto with provided TTL duration.
35-
func (r *Ristretto) Set(key string, item *cache.Item, ttl time.Duration) error {
36+
func (r *Ristretto) Set(ctx context.Context, key string, item *cache.Item, ttl time.Duration) error {
3637
// using # of rows as cost
3738
_ = r.c.SetWithTTL(key, item, int64(len(item.Rows)), ttl)
3839
return nil

doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Usage:
99
import (
1010
"database/sql"
1111
12-
redis "github.com/go-redis/redis/v7"
12+
"github.com/go-redis/redis/v8"
1313
"github.com/prashanthpai/sqlcache"
1414
"github.com/jackc/pgx/v4/stdlib"
1515
)

example/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# example
2+
3+
```sh
4+
ppai@mbp:~/src/sqlcache/example$ go build .
5+
ppai@mbp:~/src/sqlcache/example$ ./example
6+
i=0; t=2.796458ms
7+
i=1; t=536.167µs
8+
i=2; t=242.125µs
9+
i=3; t=434.375µs
10+
i=4; t=273.666µs
11+
i=5; t=1.674375ms
12+
i=6; t=281.083µs
13+
i=7; t=166.167µs
14+
i=8; t=240.542µs
15+
i=9; t=211.375µs
16+
i=10; t=1.464042ms
17+
i=11; t=381.25µs
18+
i=12; t=293.459µs
19+
i=13; t=284.708µs
20+
i=14; t=240.75µs
21+
22+
Interceptor metrics: &{Hits:12 Misses:3 Errors:0}
23+
```

example/main.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"github.com/prashanthpai/sqlcache/cache"
1212

1313
"github.com/dgraph-io/ristretto"
14-
redis "github.com/go-redis/redis/v7"
1514
"github.com/jackc/pgx/v4/stdlib"
15+
// "github.com/go-redis/redis/v8"
1616
)
1717

1818
const (
@@ -32,17 +32,19 @@ func newRistrettoCache(maxRowsToCache int64) (cache.Cacher, error) {
3232
return sqlcache.NewRistretto(c), nil
3333
}
3434

35+
/*
3536
func newRedisCache() (cache.Cacher, error) {
3637
r := redis.NewUniversalClient(&redis.UniversalOptions{
3738
Addrs: []string{"127.0.0.1:6379"},
3839
})
3940
40-
if _, err := r.Ping().Result(); err != nil {
41+
if _, err := r.Ping(context.Background()).Result(); err != nil {
4142
return nil, err
4243
}
4344
4445
return sqlcache.NewRedis(r, "sqc:"), nil
4546
}
47+
*/
4648

4749
func main() {
4850

@@ -52,7 +54,7 @@ func main() {
5254
}
5355

5456
/*
55-
cache, err := newRedisCache()
57+
cache, err = newRedisCache()
5658
if err != nil {
5759
log.Fatalf("newRedisCache() failed: %v", err)
5860
}
@@ -80,7 +82,7 @@ func main() {
8082
func run() error {
8183

8284
db, err := sql.Open("pgx-sqlcache",
83-
"host=127.0.0.1 port=5432 user=prashanthpai dbname=postgres sslmode=disable")
85+
"host=localhost user=postgres dbname=postgres password=postgres")
8486
if err != nil {
8587
return err
8688
}

go.mod

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
module github.com/prashanthpai/sqlcache
22

3-
go 1.14
3+
go 1.19
44

55
require (
6-
github.com/DATA-DOG/go-sqlmock v1.4.1
7-
github.com/dgraph-io/ristretto v0.0.3
8-
github.com/go-redis/redis/v7 v7.4.0
9-
github.com/jackc/pgx/v4 v4.6.0
10-
github.com/mitchellh/hashstructure v1.0.0
11-
github.com/ngrok/sqlmw v0.0.0-20200129213757-d5c93a81bec6
12-
github.com/stretchr/testify v1.6.1
6+
github.com/DATA-DOG/go-sqlmock v1.5.0
7+
github.com/dgraph-io/ristretto v0.1.1
8+
github.com/go-redis/redis/v8 v8.11.5
9+
github.com/jackc/pgx/v4 v4.18.1
10+
github.com/mitchellh/hashstructure/v2 v2.0.2
11+
github.com/ngrok/sqlmw v0.0.0-20220520173518-97c9c04efc79
12+
github.com/stretchr/testify v1.8.2
1313
github.com/vmihailenco/msgpack/v4 v4.3.12
1414
)
15+
16+
require (
17+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
18+
github.com/davecgh/go-spew v1.1.1 // indirect
19+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
20+
github.com/dustin/go-humanize v1.0.0 // indirect
21+
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
22+
github.com/golang/protobuf v1.3.4 // indirect
23+
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
24+
github.com/jackc/pgconn v1.14.0 // indirect
25+
github.com/jackc/pgio v1.0.0 // indirect
26+
github.com/jackc/pgpassfile v1.0.0 // indirect
27+
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
28+
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
29+
github.com/jackc/pgtype v1.14.0 // indirect
30+
github.com/pkg/errors v0.9.1 // indirect
31+
github.com/pmezard/go-difflib v1.0.0 // indirect
32+
github.com/stretchr/objx v0.5.0 // indirect
33+
github.com/vmihailenco/tagparser v0.1.1 // indirect
34+
golang.org/x/crypto v0.6.0 // indirect
35+
golang.org/x/net v0.6.0 // indirect
36+
golang.org/x/sys v0.5.0 // indirect
37+
golang.org/x/text v0.7.0 // indirect
38+
google.golang.org/appengine v1.6.5 // indirect
39+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
40+
gopkg.in/yaml.v3 v3.0.1 // indirect
41+
)

0 commit comments

Comments
 (0)