add support for redis storage

This commit is contained in:
bergquist 2019-02-23 16:12:37 +01:00
parent d99af23946
commit 5ced863f75
7 changed files with 110 additions and 24 deletions

View File

@ -142,11 +142,6 @@
"gui:release": "ts-node --project ./scripts/cli/tsconfig.json ./scripts/cli/index.ts gui:release -p",
"cli": "ts-node --project ./scripts/cli/tsconfig.json ./scripts/cli/index.ts"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged && grunt precommit"
}
},
"lint-staged": {
"*.{ts,tsx,json,scss}": [
"prettier --write",

View File

@ -18,7 +18,7 @@ func newDatabaseCache(sqlstore *sqlstore.SqlStore) *databaseCache {
log: log.New("distcache.database"),
}
go dc.StartGC()
//go dc.StartGC() //TODO: start the GC somehow
return dc
}
@ -79,7 +79,7 @@ type CacheData struct {
CreatedAt int64
}
func (dc *databaseCache) Put(key string, value interface{}, expire int64) error {
func (dc *databaseCache) Put(key string, value interface{}, expire time.Duration) error {
item := &Item{Val: value}
data, err := EncodeGob(item)
if err != nil {
@ -94,10 +94,15 @@ func (dc *databaseCache) Put(key string, value interface{}, expire int64) error
return err
}
var expiresInEpoch int64
if expire != 0 {
expiresInEpoch = int64(expire) / int64(time.Second)
}
if len(cacheHits) > 0 {
_, err = dc.SQLStore.NewSession().Exec("UPDATE cache_data SET data=?, created=?, expire=? WHERE key=?", data, now, expire, key)
_, err = dc.SQLStore.NewSession().Exec("UPDATE cache_data SET data=?, created=?, expire=? WHERE key=?", data, now, expiresInEpoch, key)
} else {
_, err = dc.SQLStore.NewSession().Exec("INSERT INTO cache_data(key,data,created_at,expires) VALUES(?,?,?,?)", key, data, now, expire)
_, err = dc.SQLStore.NewSession().Exec("INSERT INTO cache_data(key,data,created_at,expires) VALUES(?,?,?,?)", key, data, now, expiresInEpoch)
}
return err

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)
db.Put("key2", obj, 1000)
db.Put("key3", obj, 1000)
db.Put("key1", obj, 1000*time.Second)
db.Put("key2", obj, 1000*time.Second)
db.Put("key3", obj, 1000*time.Second)
// insert object that should never expire
db.Put("key4", obj, 0)
getTime = time.Now
db.Put("key5", obj, 1000)
db.Put("key5", obj, 1000*time.Second)
//run GC
db.internalRunGC()

View File

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/gob"
"errors"
"time"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/services/sqlstore"
@ -34,7 +35,7 @@ type CacheOpts struct {
func createClient(opts CacheOpts, sqlstore *sqlstore.SqlStore) cacheStorage {
if opts.name == "redis" {
return nil
return newRedisStorage(nil)
}
if opts.name == "memcache" {
@ -45,7 +46,7 @@ func createClient(opts CacheOpts, sqlstore *sqlstore.SqlStore) cacheStorage {
return nil
}
return &databaseCache{SQLStore: sqlstore}
return newDatabaseCache(sqlstore) //&databaseCache{SQLStore: sqlstore}
}
// DistributedCache allows Grafana to cache data outside its own process
@ -77,7 +78,7 @@ type cacheStorage interface {
Get(key string) (interface{}, error)
// Puts an object into the cache
Put(key string, value interface{}, expire int64) error
Put(key string, value interface{}, expire time.Duration) error
// Delete object from cache
Delete(key string) error

View File

@ -27,18 +27,18 @@ func createTestClient(t *testing.T, name string) cacheStorage {
}
func TestAllCacheClients(t *testing.T) {
clients := []string{"database"} // add redis, memcache, memory
clients := []string{"database", "redis"} // add redis, memcache, memory
for _, v := range clients {
client := createTestClient(t, v)
CanPutGetAndDeleteCachedObjects(t, client)
CanNotFetchExpiredItems(t, client)
CanSetInfiniteCacheExpiration(t, client)
CanPutGetAndDeleteCachedObjects(t, v, client)
CanNotFetchExpiredItems(t, v, client)
CanSetInfiniteCacheExpiration(t, v, client)
}
}
func CanPutGetAndDeleteCachedObjects(t *testing.T, client cacheStorage) {
func CanPutGetAndDeleteCachedObjects(t *testing.T, name string, client cacheStorage) {
cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
err := client.Put("key", cacheableStruct, 0)
@ -58,12 +58,16 @@ func CanPutGetAndDeleteCachedObjects(t *testing.T, client cacheStorage) {
assert.Equal(t, err, ErrCacheItemNotFound)
}
func CanNotFetchExpiredItems(t *testing.T, client cacheStorage) {
func CanNotFetchExpiredItems(t *testing.T, name string, client cacheStorage) {
if name == "redis" {
t.Skip() //this test does not work with redis since it uses its own getTime fn
}
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, 10000)
err := client.Put("key", cacheableStruct, 10000*time.Second)
assert.Equal(t, err, nil)
// should not be able to read that value since its expired
@ -72,7 +76,7 @@ func CanNotFetchExpiredItems(t *testing.T, client cacheStorage) {
assert.Equal(t, err, ErrCacheItemNotFound)
}
func CanSetInfiniteCacheExpiration(t *testing.T, client cacheStorage) {
func CanSetInfiniteCacheExpiration(t *testing.T, name string, client cacheStorage) {
cacheableStruct := CacheableStruct{String: "hej", Int64: 2000}
// insert cache item one day back

View File

@ -0,0 +1,80 @@
package distcache
import (
"time"
redis "gopkg.in/redis.v2"
)
type redisStorage struct {
c *redis.Client
}
func newRedisStorage(c *redis.Client) *redisStorage {
opt := &redis.Options{
Network: "tcp",
Addr: "localhost:6379",
}
return &redisStorage{
c: redis.NewClient(opt),
}
}
// Set sets value to given key in session.
func (s *redisStorage) Put(key string, val interface{}, expires time.Duration) error {
item := &Item{Created: getTime().Unix(), Val: val}
value, err := EncodeGob(item)
if err != nil {
return err
}
var status *redis.StatusCmd
if expires == 0 {
status = s.c.Set(key, string(value))
} else {
status = s.c.SetEx(key, expires, string(value))
}
return status.Err()
}
// Get gets value by given key in session.
func (s *redisStorage) Get(key string) (interface{}, error) {
v := s.c.Get(key)
item := &Item{}
err := DecodeGob([]byte(v.Val()), item)
if err == nil {
return item.Val, nil
}
if err.Error() == "EOF" {
return nil, ErrCacheItemNotFound
}
if err != nil {
return nil, err
}
return item.Val, nil
}
// Delete delete a key from session.
func (s *redisStorage) Delete(key string) error {
cmd := s.c.Del(key)
return cmd.Err()
}
// RedisProvider represents a redis session provider implementation.
type RedisProvider struct {
c *redis.Client
duration time.Duration
prefix string
}
// Exist returns true if session with given ID exists.
func (p *RedisProvider) Exist(sid string) bool {
has, err := p.c.Exists(p.prefix + sid).Result()
return err == nil && has
}

View File

@ -0,0 +1 @@
package distcache