-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ramadani/redis-universal-client
Redis universal client
- Loading branch information
Showing
6 changed files
with
246 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"github.com/go-redis/redis/v8" | ||
"github.com/ramadani/andromeda" | ||
"time" | ||
) | ||
|
||
type cacheRedisUniversal struct { | ||
client redis.UniversalClient | ||
} | ||
|
||
func (c *cacheRedisUniversal) IncrBy(ctx context.Context, key string, value int64) (int64, error) { | ||
return c.client.IncrBy(ctx, key, value).Result() | ||
} | ||
|
||
func (c *cacheRedisUniversal) DecrBy(ctx context.Context, key string, decrement int64) (int64, error) { | ||
return c.client.DecrBy(ctx, key, decrement).Result() | ||
} | ||
|
||
func (c *cacheRedisUniversal) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) (string, error) { | ||
return c.client.Set(ctx, key, value, expiration).Result() | ||
} | ||
|
||
func (c *cacheRedisUniversal) Get(ctx context.Context, key string) (string, error) { | ||
val, err := c.client.Get(ctx, key).Result() | ||
if err == redis.Nil { | ||
err = andromeda.ErrCacheNotFound | ||
} | ||
return val, err | ||
} | ||
|
||
func (c *cacheRedisUniversal) SetNX(ctx context.Context, key string, value interface{}, expiration time.Duration) (bool, error) { | ||
return c.client.SetNX(ctx, key, value, expiration).Result() | ||
} | ||
|
||
func (c *cacheRedisUniversal) Exists(ctx context.Context, keys ...string) (int64, error) { | ||
return c.client.Exists(ctx, keys...).Result() | ||
} | ||
|
||
func (c *cacheRedisUniversal) Del(ctx context.Context, keys ...string) (int64, error) { | ||
return c.client.Del(ctx, keys...).Result() | ||
} | ||
|
||
// NewCacheRedisUniversal cache using redis | ||
func NewCacheRedisUniversal(client redis.UniversalClient) andromeda.Cache { | ||
return &cacheRedisUniversal{client: client} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package cache_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/alicebob/miniredis/v2" | ||
"github.com/go-redis/redis/v8" | ||
"github.com/ramadani/andromeda" | ||
"github.com/ramadani/andromeda/cache" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCacheRedisUniversal(t *testing.T) { | ||
ctx := context.TODO() | ||
miniRedis, err := miniredis.Run() | ||
assert.Nil(t, err) | ||
|
||
redisCache := cache.NewCacheRedisUniversal(redis.NewUniversalClient(&redis.UniversalOptions{ | ||
Addrs: []string{miniRedis.Addr()}, | ||
})) | ||
|
||
t.Run("IncrByAndDecrBy", func(t *testing.T) { | ||
key := "123-1" | ||
|
||
res, err := redisCache.IncrBy(ctx, key, 1) | ||
|
||
assert.Equal(t, int64(1), res) | ||
assert.Nil(t, err) | ||
|
||
res, err = redisCache.DecrBy(ctx, key, 1) | ||
|
||
assert.Equal(t, int64(0), res) | ||
assert.Nil(t, err) | ||
}) | ||
|
||
t.Run("SetAndGet", func(t *testing.T) { | ||
key := "123-2" | ||
ttl := 5 * time.Second | ||
|
||
res, err := redisCache.Set(ctx, key, "1", ttl) | ||
|
||
assert.Equal(t, "OK", res) | ||
assert.Nil(t, err) | ||
|
||
res, err = redisCache.Get(ctx, key) | ||
|
||
assert.Equal(t, "1", res) | ||
assert.Nil(t, err) | ||
}) | ||
|
||
t.Run("ErrCacheNotFound", func(t *testing.T) { | ||
key := "123-3" | ||
res, err := redisCache.Get(ctx, key) | ||
|
||
assert.Equal(t, "", res) | ||
assert.Equal(t, andromeda.ErrCacheNotFound, err) | ||
}) | ||
|
||
t.Run("SetNX", func(t *testing.T) { | ||
key := "123-4" | ||
ttl := 5 * time.Second | ||
|
||
res, err := redisCache.SetNX(ctx, key, "1", ttl) | ||
|
||
assert.True(t, res) | ||
assert.Nil(t, err) | ||
|
||
res, err = redisCache.SetNX(ctx, key, "1", ttl) | ||
|
||
assert.False(t, res) | ||
assert.Nil(t, err) | ||
}) | ||
|
||
t.Run("Exists", func(t *testing.T) { | ||
key := "123-5" | ||
ttl := 5 * time.Second | ||
|
||
res, err := redisCache.Set(ctx, key, "1", ttl) | ||
|
||
assert.Equal(t, "OK", res) | ||
assert.Nil(t, err) | ||
|
||
exists, err := redisCache.Exists(ctx, key) | ||
|
||
assert.Equal(t, int64(1), exists) | ||
assert.Nil(t, err) | ||
|
||
exists, err = redisCache.Exists(ctx, fmt.Sprintf("test-%s", key)) | ||
|
||
assert.Equal(t, int64(0), exists) | ||
assert.Nil(t, err) | ||
}) | ||
|
||
t.Run("Delete", func(t *testing.T) { | ||
key := "123-6" | ||
ttl := 5 * time.Second | ||
|
||
res, err := redisCache.Set(ctx, key, "1", ttl) | ||
|
||
assert.Equal(t, "OK", res) | ||
assert.Nil(t, err) | ||
|
||
exists, err := redisCache.Del(ctx, key) | ||
|
||
assert.Equal(t, int64(1), exists) | ||
assert.Nil(t, err) | ||
|
||
exists, err = redisCache.Del(ctx, key) | ||
|
||
assert.Equal(t, int64(0), exists) | ||
assert.Nil(t, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters