mirror of
https://github.com/eosswedenorg/thalos
synced 2026-07-04 12:03:41 +02:00
Adding app/cache/redis_store.go
This commit is contained in:
parent
ed1009062d
commit
c6cb26d543
2 changed files with 140 additions and 0 deletions
37
app/cache/redis_store.go
vendored
Normal file
37
app/cache/redis_store.go
vendored
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-redis/cache/v9"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RedisStore struct {
|
||||||
|
c *cache.Cache
|
||||||
|
ctx context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRedisStore(options *cache.Options) *RedisStore {
|
||||||
|
return &RedisStore{
|
||||||
|
c: cache.New(options),
|
||||||
|
ctx: context.Background(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *RedisStore) Get(key string, value interface{}) error {
|
||||||
|
return s.c.Get(s.ctx, key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *RedisStore) Has(key string) bool {
|
||||||
|
return s.c.Exists(s.ctx, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *RedisStore) Set(key string, value any, ttl time.Duration) error {
|
||||||
|
return s.c.Set(&cache.Item{
|
||||||
|
Ctx: s.ctx,
|
||||||
|
Key: key,
|
||||||
|
Value: value,
|
||||||
|
TTL: ttl,
|
||||||
|
})
|
||||||
|
}
|
||||||
103
app/cache/redis_store_test.go
vendored
Normal file
103
app/cache/redis_store_test.go
vendored
Normal file
|
|
@ -0,0 +1,103 @@
|
||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-redis/redismock/v9"
|
||||||
|
|
||||||
|
redis_cache "github.com/go-redis/cache/v9"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
type testItem struct {
|
||||||
|
Num uint32
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRedisStore_Set(t *testing.T) {
|
||||||
|
client, mock := redismock.NewClientMock()
|
||||||
|
|
||||||
|
store := NewRedisStore(&redis_cache.Options{
|
||||||
|
Redis: client,
|
||||||
|
})
|
||||||
|
|
||||||
|
expected := testItem{
|
||||||
|
Num: 24,
|
||||||
|
Name: "Some Name",
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := store.c.Marshal(expected)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
mock.ExpectSet("mykey", bytes, time.Minute).SetVal("OK")
|
||||||
|
|
||||||
|
err = store.Set("mykey", expected, time.Minute)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.NoError(t, mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRedisStore_GetMiss(t *testing.T) {
|
||||||
|
client, mock := redismock.NewClientMock()
|
||||||
|
|
||||||
|
store := NewRedisStore(&redis_cache.Options{
|
||||||
|
Redis: client,
|
||||||
|
})
|
||||||
|
|
||||||
|
mock.ExpectGet("mykey").SetErr(redis_cache.ErrCacheMiss)
|
||||||
|
|
||||||
|
expected := testItem{}
|
||||||
|
err := store.Get("mykey", &expected)
|
||||||
|
assert.ErrorIs(t, err, redis_cache.ErrCacheMiss)
|
||||||
|
assert.NoError(t, mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRedisStore_GetHit(t *testing.T) {
|
||||||
|
client, mock := redismock.NewClientMock()
|
||||||
|
|
||||||
|
store := NewRedisStore(&redis_cache.Options{
|
||||||
|
Redis: client,
|
||||||
|
})
|
||||||
|
|
||||||
|
expected := testItem{
|
||||||
|
Num: 42,
|
||||||
|
Name: "MyName",
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := store.c.Marshal(expected)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
mock.ExpectSet("mykey2", bytes, time.Second*20).SetVal("OK")
|
||||||
|
mock.ExpectGet("mykey2").SetVal(string(bytes))
|
||||||
|
|
||||||
|
err = store.Set("mykey2", expected, time.Second*20)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
actual := testItem{}
|
||||||
|
err = store.Get("mykey2", &actual)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, expected, actual)
|
||||||
|
assert.NoError(t, mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRedisStore_Has(t *testing.T) {
|
||||||
|
client, mock := redismock.NewClientMock()
|
||||||
|
|
||||||
|
store := NewRedisStore(&redis_cache.Options{
|
||||||
|
Redis: client,
|
||||||
|
})
|
||||||
|
|
||||||
|
bytes, err := store.c.Marshal("value")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
mock.ExpectSet("key1", bytes, time.Minute*15).SetVal("OK")
|
||||||
|
mock.ExpectGet("key1").SetVal(string(bytes))
|
||||||
|
mock.ExpectGet("key2").RedisNil()
|
||||||
|
|
||||||
|
err = store.Set("key1", "value", time.Minute*15)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, store.Has("key1"))
|
||||||
|
assert.False(t, store.Has("key2"))
|
||||||
|
assert.NoError(t, mock.ExpectationsWereMet())
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue