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
}
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}
data, err := encodeGob(item)
if err != nil {

View File

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

View File

@ -31,7 +31,7 @@ func (ds *DistributedCache) Init() error {
return nil
}
func createClient(opts *setting.CacheOpts, sqlstore *sqlstore.SqlStore) cacheStorage {
func createClient(opts *setting.CacheOpts, sqlstore *sqlstore.SqlStore) CacheStorage {
if opts.Name == "redis" {
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
type DistributedCache struct {
log log.Logger
Client cacheStorage
Client CacheStorage
SQLStore *sqlstore.SqlStore `inject:""`
Cfg *setting.Cfg `inject:""`
}
@ -66,12 +66,16 @@ func decodeGob(data []byte, out *cachedItem) error {
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(key string) (interface{}, error)
// Puts an object into the cache
Put(key string, value interface{}, expire time.Duration) error
// Set sets an object into the cache
Set(key string, value interface{}, expire time.Duration) error
// Delete object from cache
Delete(key string) error

View File

@ -20,7 +20,7 @@ func init() {
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()
dc := &DistributedCache{
@ -50,16 +50,16 @@ func TestCachedBasedOnConfig(t *testing.T) {
runTestsForClient(t, client)
}
func runTestsForClient(t *testing.T, client cacheStorage) {
func runTestsForClient(t *testing.T, client CacheStorage) {
canPutGetAndDeleteCachedObjects(t, client)
canNotFetchExpiredItems(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}
err := client.Put("key", cacheableStruct, 0)
err := client.Set("key", cacheableStruct, 0)
assert.Equal(t, err, nil)
data, err := client.Get("key")
@ -76,10 +76,10 @@ func canPutGetAndDeleteCachedObjects(t *testing.T, client cacheStorage) {
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}
err := client.Put("key", cacheableStruct, time.Second)
err := client.Set("key", cacheableStruct, time.Second)
assert.Equal(t, err, nil)
//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)
}
func canSetInfiniteCacheExpiration(t *testing.T, client cacheStorage) {
func canSetInfiniteCacheExpiration(t *testing.T, client CacheStorage) {
cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
// insert cache item one day back
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)
// 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.
func (s *memcachedStorage) Put(key string, val interface{}, expires time.Duration) error {
// Set sets value to given key in the cache.
func (s *memcachedStorage) Set(key string, val interface{}, expires time.Duration) error {
item := &cachedItem{Val: val}
bytes, err := encodeGob(item)

View File

@ -20,7 +20,7 @@ func newRedisStorage(opts *setting.CacheOpts) *redisStorage {
}
// 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}
value, err := encodeGob(item)
if err != nil {