mirror of
https://github.com/eosswedenorg/thalos
synced 2026-06-17 04:30:03 +02:00
app/cache: implement store interface.
This commit is contained in:
parent
8b3202e4d3
commit
6106481417
4 changed files with 30 additions and 29 deletions
7
app/cache/memory_store.go
vendored
7
app/cache/memory_store.go
vendored
|
|
@ -1,6 +1,7 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"time"
|
||||
|
|
@ -21,7 +22,7 @@ func NewMemoryStore() *MemoryStore {
|
|||
return &MemoryStore{make(map[string]memoryStoreItem)}
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Get(key string, value any) error {
|
||||
func (s *MemoryStore) Get(ctx context.Context, key string, value any) error {
|
||||
if item, ok := s.data[key]; ok {
|
||||
|
||||
if item.expired.Before(now()) {
|
||||
|
|
@ -41,12 +42,12 @@ func (s *MemoryStore) Get(key string, value any) error {
|
|||
return fmt.Errorf("key: %s does not exist", key)
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Has(key string) bool {
|
||||
func (s *MemoryStore) Has(ctx context.Context, key string) bool {
|
||||
_, hit := s.data[key]
|
||||
return hit
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Set(key string, value any, ttl time.Duration) error {
|
||||
func (s *MemoryStore) Set(ctx context.Context, key string, value any, ttl time.Duration) error {
|
||||
s.data[key] = memoryStoreItem{
|
||||
value: value,
|
||||
expired: now().Add(ttl),
|
||||
|
|
|
|||
19
app/cache/memory_store_test.go
vendored
19
app/cache/memory_store_test.go
vendored
|
|
@ -1,6 +1,7 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -30,7 +31,7 @@ func TestMemoryStore_Set(t *testing.T) {
|
|||
}
|
||||
|
||||
store := NewMemoryStore()
|
||||
err := store.Set("key1", item, time.Hour)
|
||||
err := store.Set(context.Background(), "key1", item, time.Hour)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, expected, store.data)
|
||||
|
|
@ -40,7 +41,7 @@ func TestMemoryStore_GetMiss(t *testing.T) {
|
|||
store := NewMemoryStore()
|
||||
|
||||
var v any
|
||||
err := store.Get("Key2", &v)
|
||||
err := store.Get(context.Background(), "Key2", &v)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
|
|
@ -52,11 +53,11 @@ func TestMemoryStore_GetHit(t *testing.T) {
|
|||
}
|
||||
|
||||
store := NewMemoryStore()
|
||||
err := store.Set("key1", expected, time.Hour)
|
||||
err := store.Set(context.Background(), "key1", expected, time.Hour)
|
||||
assert.NoError(t, err)
|
||||
|
||||
var actual memoryTestItem
|
||||
err = store.Get("key1", &actual)
|
||||
err = store.Get(context.Background(), "key1", &actual)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, actual)
|
||||
}
|
||||
|
|
@ -69,19 +70,19 @@ func TestMemoryStore_GetNonPointer(t *testing.T) {
|
|||
}
|
||||
|
||||
store := NewMemoryStore()
|
||||
err := store.Set("key1", expected, time.Hour)
|
||||
err := store.Set(context.Background(), "key1", expected, time.Hour)
|
||||
assert.NoError(t, err)
|
||||
|
||||
var actual string
|
||||
err = store.Get("key1", actual)
|
||||
err = store.Get(context.Background(), "key1", actual)
|
||||
assert.EqualError(t, err, "value must be of pointer type, 'string' passed")
|
||||
}
|
||||
|
||||
func TestMemoryStore_Has(t *testing.T) {
|
||||
store := NewMemoryStore()
|
||||
err := store.Set("key1", "value", time.Hour)
|
||||
err := store.Set(context.Background(), "key1", "value", time.Hour)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.True(t, store.Has("key1"))
|
||||
assert.False(t, store.Has("key2"))
|
||||
assert.True(t, store.Has(context.Background(), "key1"))
|
||||
assert.False(t, store.Has(context.Background(), "key2"))
|
||||
}
|
||||
|
|
|
|||
18
app/cache/redis_store.go
vendored
18
app/cache/redis_store.go
vendored
|
|
@ -8,28 +8,26 @@ import (
|
|||
)
|
||||
|
||||
type RedisStore struct {
|
||||
c *cache.Cache
|
||||
ctx context.Context
|
||||
c *cache.Cache
|
||||
}
|
||||
|
||||
func NewRedisStore(options *cache.Options) *RedisStore {
|
||||
return &RedisStore{
|
||||
c: cache.New(options),
|
||||
ctx: context.Background(),
|
||||
c: cache.New(options),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *RedisStore) Get(key string, value interface{}) error {
|
||||
return s.c.Get(s.ctx, key, value)
|
||||
func (s *RedisStore) Get(ctx context.Context, key string, value interface{}) error {
|
||||
return s.c.Get(ctx, key, value)
|
||||
}
|
||||
|
||||
func (s *RedisStore) Has(key string) bool {
|
||||
return s.c.Exists(s.ctx, key)
|
||||
func (s *RedisStore) Has(ctx context.Context, key string) bool {
|
||||
return s.c.Exists(ctx, key)
|
||||
}
|
||||
|
||||
func (s *RedisStore) Set(key string, value any, ttl time.Duration) error {
|
||||
func (s *RedisStore) Set(ctx context.Context, key string, value any, ttl time.Duration) error {
|
||||
return s.c.Set(&cache.Item{
|
||||
Ctx: s.ctx,
|
||||
Ctx: ctx,
|
||||
Key: key,
|
||||
Value: value,
|
||||
TTL: ttl,
|
||||
|
|
|
|||
15
app/cache/redis_store_test.go
vendored
15
app/cache/redis_store_test.go
vendored
|
|
@ -1,6 +1,7 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -32,7 +33,7 @@ func TestRedisStore_Set(t *testing.T) {
|
|||
|
||||
mock.ExpectSet("mykey", bytes, time.Minute).SetVal("OK")
|
||||
|
||||
err = store.Set("mykey", expected, time.Minute)
|
||||
err = store.Set(context.Background(), "mykey", expected, time.Minute)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.NoError(t, mock.ExpectationsWereMet())
|
||||
|
|
@ -48,7 +49,7 @@ func TestRedisStore_GetMiss(t *testing.T) {
|
|||
mock.ExpectGet("mykey").SetErr(redis_cache.ErrCacheMiss)
|
||||
|
||||
expected := testItem{}
|
||||
err := store.Get("mykey", &expected)
|
||||
err := store.Get(context.Background(), "mykey", &expected)
|
||||
assert.ErrorIs(t, err, redis_cache.ErrCacheMiss)
|
||||
assert.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
|
@ -71,11 +72,11 @@ func TestRedisStore_GetHit(t *testing.T) {
|
|||
mock.ExpectSet("mykey2", bytes, time.Second*20).SetVal("OK")
|
||||
mock.ExpectGet("mykey2").SetVal(string(bytes))
|
||||
|
||||
err = store.Set("mykey2", expected, time.Second*20)
|
||||
err = store.Set(context.Background(), "mykey2", expected, time.Second*20)
|
||||
assert.NoError(t, err)
|
||||
|
||||
actual := testItem{}
|
||||
err = store.Get("mykey2", &actual)
|
||||
err = store.Get(context.Background(), "mykey2", &actual)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, expected, actual)
|
||||
assert.NoError(t, mock.ExpectationsWereMet())
|
||||
|
|
@ -95,9 +96,9 @@ func TestRedisStore_Has(t *testing.T) {
|
|||
mock.ExpectGet("key1").SetVal(string(bytes))
|
||||
mock.ExpectGet("key2").RedisNil()
|
||||
|
||||
err = store.Set("key1", "value", time.Minute*15)
|
||||
err = store.Set(context.Background(), "key1", "value", time.Minute*15)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, store.Has("key1"))
|
||||
assert.False(t, store.Has("key2"))
|
||||
assert.True(t, store.Has(context.Background(), "key1"))
|
||||
assert.False(t, store.Has(context.Background(), "key2"))
|
||||
assert.NoError(t, mock.ExpectationsWereMet())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue