rename put -> set

This commit is contained in:
bergquist 2019-03-05 15:15:05 +01:00
parent 6231095f72
commit 9a78c23165
6 changed files with 26 additions and 22 deletions

View File

@ -79,7 +79,7 @@ type cacheData struct {
CreatedAt int64 CreatedAt int64
} }
func (dc *databaseCache) Put(key string, value interface{}, expire time.Duration) error { func (dc *databaseCache) Set(key string, value interface{}, expire time.Duration) error {
item := &cachedItem{Val: value} item := &cachedItem{Val: value}
data, err := encodeGob(item) data, err := encodeGob(item)
if err != nil { if err != nil {

View File

@ -22,15 +22,15 @@ func TestDatabaseStorageGarbageCollection(t *testing.T) {
//set time.now to 2 weeks ago //set time.now to 2 weeks ago
getTime = func() time.Time { return time.Now().AddDate(0, 0, -2) } getTime = func() time.Time { return time.Now().AddDate(0, 0, -2) }
db.Put("key1", obj, 1000*time.Second) db.Set("key1", obj, 1000*time.Second)
db.Put("key2", obj, 1000*time.Second) db.Set("key2", obj, 1000*time.Second)
db.Put("key3", obj, 1000*time.Second) db.Set("key3", obj, 1000*time.Second)
// insert object that should never expire // insert object that should never expire
db.Put("key4", obj, 0) db.Set("key4", obj, 0)
getTime = time.Now getTime = time.Now
db.Put("key5", obj, 1000*time.Second) db.Set("key5", obj, 1000*time.Second)
//run GC //run GC
db.internalRunGC() db.internalRunGC()

View File

@ -31,7 +31,7 @@ func (ds *DistributedCache) Init() error {
return nil return nil
} }
func createClient(opts *setting.CacheOpts, sqlstore *sqlstore.SqlStore) cacheStorage { func createClient(opts *setting.CacheOpts, sqlstore *sqlstore.SqlStore) CacheStorage {
if opts.Name == "redis" { if opts.Name == "redis" {
return newRedisStorage(opts) return newRedisStorage(opts)
} }
@ -46,7 +46,7 @@ func createClient(opts *setting.CacheOpts, sqlstore *sqlstore.SqlStore) cacheSto
// DistributedCache allows Grafana to cache data outside its own process // DistributedCache allows Grafana to cache data outside its own process
type DistributedCache struct { type DistributedCache struct {
log log.Logger log log.Logger
Client cacheStorage Client CacheStorage
SQLStore *sqlstore.SqlStore `inject:""` SQLStore *sqlstore.SqlStore `inject:""`
Cfg *setting.Cfg `inject:""` Cfg *setting.Cfg `inject:""`
} }
@ -66,12 +66,16 @@ func decodeGob(data []byte, out *cachedItem) error {
return gob.NewDecoder(buf).Decode(&out) return gob.NewDecoder(buf).Decode(&out)
} }
type cacheStorage interface { // CacheStorage allows the caller to set, get and delete items in the cache.
// Cached items are stored as byte arrays and marshalled using "encoding/gob"
// so any struct added to the cache needs to be registred with `gob.Register`
// ex `gob.Register(CacheableStruct{})``
type CacheStorage interface {
// Get reads object from Cache // Get reads object from Cache
Get(key string) (interface{}, error) Get(key string) (interface{}, error)
// Puts an object into the cache // Set sets an object into the cache
Put(key string, value interface{}, expire time.Duration) error Set(key string, value interface{}, expire time.Duration) error
// Delete object from cache // Delete object from cache
Delete(key string) error Delete(key string) error

View File

@ -20,7 +20,7 @@ func init() {
gob.Register(CacheableStruct{}) gob.Register(CacheableStruct{})
} }
func createTestClient(t *testing.T, opts *setting.CacheOpts, sqlstore *sqlstore.SqlStore) cacheStorage { func createTestClient(t *testing.T, opts *setting.CacheOpts, sqlstore *sqlstore.SqlStore) CacheStorage {
t.Helper() t.Helper()
dc := &DistributedCache{ dc := &DistributedCache{
@ -50,16 +50,16 @@ func TestCachedBasedOnConfig(t *testing.T) {
runTestsForClient(t, client) runTestsForClient(t, client)
} }
func runTestsForClient(t *testing.T, client cacheStorage) { func runTestsForClient(t *testing.T, client CacheStorage) {
canPutGetAndDeleteCachedObjects(t, client) canPutGetAndDeleteCachedObjects(t, client)
canNotFetchExpiredItems(t, client) canNotFetchExpiredItems(t, client)
canSetInfiniteCacheExpiration(t, client) canSetInfiniteCacheExpiration(t, client)
} }
func canPutGetAndDeleteCachedObjects(t *testing.T, client cacheStorage) { func canPutGetAndDeleteCachedObjects(t *testing.T, client CacheStorage) {
cacheableStruct := CacheableStruct{String: "hej", Int64: 2000} cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
err := client.Put("key", cacheableStruct, 0) err := client.Set("key", cacheableStruct, 0)
assert.Equal(t, err, nil) assert.Equal(t, err, nil)
data, err := client.Get("key") data, err := client.Get("key")
@ -76,10 +76,10 @@ func canPutGetAndDeleteCachedObjects(t *testing.T, client cacheStorage) {
assert.Equal(t, err, ErrCacheItemNotFound) assert.Equal(t, err, ErrCacheItemNotFound)
} }
func canNotFetchExpiredItems(t *testing.T, client cacheStorage) { func canNotFetchExpiredItems(t *testing.T, client CacheStorage) {
cacheableStruct := CacheableStruct{String: "hej", Int64: 2000} cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
err := client.Put("key", cacheableStruct, time.Second) err := client.Set("key", cacheableStruct, time.Second)
assert.Equal(t, err, nil) assert.Equal(t, err, nil)
//not sure how this can be avoided when testing redis/memcached :/ //not sure how this can be avoided when testing redis/memcached :/
@ -90,12 +90,12 @@ func canNotFetchExpiredItems(t *testing.T, client cacheStorage) {
assert.Equal(t, err, ErrCacheItemNotFound) assert.Equal(t, err, ErrCacheItemNotFound)
} }
func canSetInfiniteCacheExpiration(t *testing.T, client cacheStorage) { func canSetInfiniteCacheExpiration(t *testing.T, client CacheStorage) {
cacheableStruct := CacheableStruct{String: "hej", Int64: 2000} cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
// insert cache item one day back // insert cache item one day back
getTime = func() time.Time { return time.Now().AddDate(0, 0, -2) } getTime = func() time.Time { return time.Now().AddDate(0, 0, -2) }
err := client.Put("key", cacheableStruct, 0) err := client.Set("key", cacheableStruct, 0)
assert.Equal(t, err, nil) assert.Equal(t, err, nil)
// should not be able to read that value since its expired // should not be able to read that value since its expired

View File

@ -25,8 +25,8 @@ func newItem(sid string, data []byte, expire int32) *memcache.Item {
} }
} }
// Put sets value to given key in the cache. // Set sets value to given key in the cache.
func (s *memcachedStorage) Put(key string, val interface{}, expires time.Duration) error { func (s *memcachedStorage) Set(key string, val interface{}, expires time.Duration) error {
item := &cachedItem{Val: val} item := &cachedItem{Val: val}
bytes, err := encodeGob(item) bytes, err := encodeGob(item)

View File

@ -20,7 +20,7 @@ func newRedisStorage(opts *setting.CacheOpts) *redisStorage {
} }
// Set sets value to given key in session. // Set sets value to given key in session.
func (s *redisStorage) Put(key string, val interface{}, expires time.Duration) error { func (s *redisStorage) Set(key string, val interface{}, expires time.Duration) error {
item := &cachedItem{Val: val} item := &cachedItem{Val: val}
value, err := encodeGob(item) value, err := encodeGob(item)
if err != nil { if err != nil {