Skip to content

Commit

Permalink
bump go modules
Browse files Browse the repository at this point in the history
  • Loading branch information
paskal committed Aug 4, 2022
1 parent 5c36e4b commit eaa2cb3
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 61 deletions.
4 changes: 2 additions & 2 deletions cache_test.go
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
"time"

"github.com/go-redis/redis/v7"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -576,7 +576,7 @@ func ExampleLoadingCache_Delete() {

// nolint:govet //false positive due to example name
// ExampleLoadingCacheMutability illustrates changing mutable stored item outside of cache, works only for non-Redis cache.
func ExampleLoadingCacheMutability() {
func Example_loadingCacheMutability() {
c, err := NewExpirableCache(MaxKeys(10), TTL(time.Minute*30)) // make expirable cache (30m TTL) with up to 10 keys
if err != nil {
panic("can' make cache")
Expand Down
9 changes: 5 additions & 4 deletions eventbus/redis.go
@@ -1,19 +1,20 @@
package eventbus

import (
"context"
"fmt"
"strings"
"time"

"github.com/go-redis/redis/v7"
"github.com/go-redis/redis/v8"
"github.com/hashicorp/go-multierror"
)

// NewRedisPubSub creates new RedisPubSub with given parameters.
// Returns an error in case of problems with creating PubSub client for specified channel.
func NewRedisPubSub(addr, channel string) (*RedisPubSub, error) {
client := redis.NewClient(&redis.Options{Addr: addr})
pubSub := client.Subscribe(channel)
pubSub := client.Subscribe(context.Background(), channel)
// wait for subscription to be created and ignore the message
if _, err := pubSub.Receive(context.Background()); err != nil {
_ = client.Close()
Expand Down Expand Up @@ -41,7 +42,7 @@ func (m *RedisPubSub) Subscribe(fn func(fromID, key string)) error {
return
default:
}
msg, err := pubsub.ReceiveTimeout(time.Second * 10)
msg, err := pubsub.ReceiveTimeout(context.Background(), time.Second*10)
if err != nil {
continue
}
Expand All @@ -59,7 +60,7 @@ func (m *RedisPubSub) Subscribe(fn func(fromID, key string)) error {

// Publish publishes provided message to channel provided on new RedisPubSub instance creation
func (m *RedisPubSub) Publish(fromID, key string) error {
return m.client.Publish(m.channel, fromID+"$"+key).Err()
return m.client.Publish(context.Background(), m.channel, fromID+"$"+key).Err()
}

// Close cleans up running goroutines and closes Redis clients
Expand Down
12 changes: 7 additions & 5 deletions go.mod
Expand Up @@ -3,10 +3,12 @@ module github.com/go-pkgz/lcw
go 1.15

require (
github.com/alicebob/miniredis/v2 v2.11.4
github.com/go-redis/redis/v7 v7.4.0
github.com/google/uuid v1.1.2
github.com/hashicorp/go-multierror v1.1.0
github.com/alicebob/miniredis/v2 v2.22.0
github.com/go-redis/redis/v8 v8.11.5
github.com/google/uuid v1.3.0
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/golang-lru v0.5.4
github.com/stretchr/testify v1.6.1
github.com/kr/pretty v0.1.0 // indirect
github.com/stretchr/testify v1.8.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)
141 changes: 106 additions & 35 deletions go.sum

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions redis_cache.go
@@ -1,11 +1,12 @@
package lcw

import (
"context"
"fmt"
"sync/atomic"
"time"

"github.com/go-redis/redis/v7"
"github.com/go-redis/redis/v8"
)

// RedisValueSizeLimit is maximum allowed value size in Redis
Expand Down Expand Up @@ -42,7 +43,7 @@ func NewRedisCache(backend *redis.Client, opts ...Option) (*RedisCache, error) {

// Get gets value by key or load with fn if not found in cache
func (c *RedisCache) Get(key string, fn func() (interface{}, error)) (data interface{}, err error) {
v, getErr := c.backend.Get(key).Result()
v, getErr := c.backend.Get(context.Background(), key).Result()
switch getErr {
// RedisClient returns nil when find a key in DB
case nil:
Expand All @@ -65,7 +66,7 @@ func (c *RedisCache) Get(key string, fn func() (interface{}, error)) (data inter
return data, nil
}

_, setErr := c.backend.Set(key, data, c.ttl).Result()
_, setErr := c.backend.Set(context.Background(), key, data, c.ttl).Result()
if setErr != nil {
atomic.AddInt64(&c.Errors, 1)
return data, setErr
Expand All @@ -76,16 +77,16 @@ func (c *RedisCache) Get(key string, fn func() (interface{}, error)) (data inter

// Invalidate removes keys with passed predicate fn, i.e. fn(key) should be true to get evicted
func (c *RedisCache) Invalidate(fn func(key string) bool) {
for _, key := range c.backend.Keys("*").Val() { // Keys() returns copy of cache's key, safe to remove directly
for _, key := range c.backend.Keys(context.Background(), "*").Val() { // Keys() returns copy of cache's key, safe to remove directly
if fn(key) {
c.backend.Del(key)
c.backend.Del(context.Background(), key)
}
}
}

// Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
func (c *RedisCache) Peek(key string) (interface{}, bool) {
ret, err := c.backend.Get(key).Result()
ret, err := c.backend.Get(context.Background(), key).Result()
if err != nil {
return nil, false
}
Expand All @@ -94,18 +95,18 @@ func (c *RedisCache) Peek(key string) (interface{}, bool) {

// Purge clears the cache completely.
func (c *RedisCache) Purge() {
c.backend.FlushDB()
c.backend.FlushDB(context.Background())

}

// Delete cache item by key
func (c *RedisCache) Delete(key string) {
c.backend.Del(key)
c.backend.Del(context.Background(), key)
}

// Keys gets all keys for the cache
func (c *RedisCache) Keys() (res []string) {
return c.backend.Keys("*").Val()
return c.backend.Keys(context.Background(), "*").Val()
}

// Stat returns cache statistics
Expand All @@ -129,11 +130,11 @@ func (c *RedisCache) size() int64 {
}

func (c *RedisCache) keys() int {
return int(c.backend.DBSize().Val())
return int(c.backend.DBSize(context.Background()).Val())
}

func (c *RedisCache) allowed(key string, data interface{}) bool {
if c.maxKeys > 0 && c.backend.DBSize().Val() >= int64(c.maxKeys) {
if c.maxKeys > 0 && c.backend.DBSize(context.Background()).Val() >= int64(c.maxKeys) {
return false
}
if c.maxKeySize > 0 && len(key) > c.maxKeySize {
Expand Down
5 changes: 3 additions & 2 deletions redis_cache_test.go
@@ -1,14 +1,15 @@
package lcw

import (
"context"
"fmt"
"sort"
"sync/atomic"
"testing"
"time"

"github.com/alicebob/miniredis/v2"
"github.com/go-redis/redis/v7"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -102,7 +103,7 @@ func TestRedisCache(t *testing.T) {
})
assert.NoError(t, err)
assert.Equal(t, "result-X", res.(string))
assert.Equal(t, int64(5), rc.backend.DBSize().Val())
assert.Equal(t, int64(5), rc.backend.DBSize(context.Background()).Val())

// put to cache and make sure it cached
res, err = rc.Get("key-Z", func() (interface{}, error) {
Expand Down
2 changes: 1 addition & 1 deletion url.go
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
"time"

"github.com/go-redis/redis/v7"
"github.com/go-redis/redis/v8"
"github.com/hashicorp/go-multierror"
)

Expand Down
2 changes: 1 addition & 1 deletion url_test.go
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
"time"

"github.com/go-redis/redis/v7"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down

0 comments on commit eaa2cb3

Please sign in to comment.