Commit c8ff698d by bergquist

avoid exposing internal structs and functions

parent 3890bd14
......@@ -45,13 +45,13 @@ func (dc *databaseCache) StartGC() {
}
func (dc *databaseCache) Get(key string) (interface{}, error) {
cacheHits := []CacheData{}
cacheHits := []cacheData{}
err := dc.SQLStore.NewSession().Where(`key = ?`, key).Find(&cacheHits)
if err != nil {
return nil, err
}
var cacheHit CacheData
var cacheHit cacheData
if len(cacheHits) == 0 {
return nil, ErrCacheItemNotFound
}
......@@ -64,15 +64,15 @@ func (dc *databaseCache) Get(key string) (interface{}, error) {
}
}
item := &Item{}
if err = DecodeGob(cacheHit.Data, item); err != nil {
item := &cachedItem{}
if err = decodeGob(cacheHit.Data, item); err != nil {
return nil, err
}
return item.Val, nil
}
type CacheData struct {
type cacheData struct {
Key string
Data []byte
Expires int64
......@@ -80,15 +80,15 @@ type CacheData struct {
}
func (dc *databaseCache) Put(key string, value interface{}, expire time.Duration) error {
item := &Item{Val: value}
data, err := EncodeGob(item)
item := &cachedItem{Val: value}
data, err := encodeGob(item)
if err != nil {
return err
}
now := getTime().Unix()
cacheHits := []CacheData{}
cacheHits := []cacheData{}
err = dc.SQLStore.NewSession().Where(`key = ?`, key).Find(&cacheHits)
if err != nil {
return err
......
......@@ -46,7 +46,7 @@ func createClient(opts CacheOpts, sqlstore *sqlstore.SqlStore) cacheStorage {
// return nil
// }
return newDatabaseCache(sqlstore) //&databaseCache{SQLStore: sqlstore}
return newDatabaseCache(sqlstore)
}
// DistributedCache allows Grafana to cache data outside its own process
......@@ -56,19 +56,17 @@ type DistributedCache struct {
SQLStore *sqlstore.SqlStore `inject:""`
}
type Item struct {
Val interface{}
Created int64
Expire int64
type cachedItem struct {
Val interface{}
}
func EncodeGob(item *Item) ([]byte, error) {
func encodeGob(item *cachedItem) ([]byte, error) {
buf := bytes.NewBuffer(nil)
err := gob.NewEncoder(buf).Encode(item)
return buf.Bytes(), err
}
func DecodeGob(data []byte, out *Item) error {
func decodeGob(data []byte, out *cachedItem) error {
buf := bytes.NewBuffer(data)
return gob.NewDecoder(buf).Decode(&out)
}
......
......@@ -27,7 +27,7 @@ func createTestClient(t *testing.T, name string) cacheStorage {
}
func TestAllCacheClients(t *testing.T) {
clients := []string{"database", "redis", "memcached"} // add redis, memcache, memory
clients := []string{"database", "redis"} // add redis, memcache, memory
for _, v := range clients {
client := createTestClient(t, v)
......@@ -59,19 +59,15 @@ func CanPutGetAndDeleteCachedObjects(t *testing.T, name string, client cacheStor
}
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*time.Second)
err := client.Put("key", cacheableStruct, time.Second)
assert.Equal(t, err, nil)
//not sure how this can be avoided when testing redis/memcached :/
<-time.After(time.Second + time.Millisecond)
// should not be able to read that value since its expired
getTime = time.Now
_, err = client.Get("key")
assert.Equal(t, err, ErrCacheItemNotFound)
}
......
......@@ -16,7 +16,7 @@ func newMemcacheStorage(connStr string) *memcacheStorage {
}
}
func NewItem(sid string, data []byte, expire int32) *memcache.Item {
func newItem(sid string, data []byte, expire int32) *memcache.Item {
return &memcache.Item{
Key: sid,
Value: data,
......@@ -26,14 +26,14 @@ func NewItem(sid string, data []byte, expire int32) *memcache.Item {
// Set sets value to given key in the cache.
func (s *memcacheStorage) Put(key string, val interface{}, expires time.Duration) error {
item := &Item{Val: val}
item := &cachedItem{Val: val}
bytes, err := EncodeGob(item)
bytes, err := encodeGob(item)
if err != nil {
return err
}
memcacheItem := NewItem(key, bytes, int32(expires))
memcacheItem := newItem(key, bytes, int32(expires))
s.c.Add(memcacheItem)
return nil
......@@ -46,9 +46,9 @@ func (s *memcacheStorage) Get(key string) (interface{}, error) {
return nil, err
}
item := &Item{}
item := &cachedItem{}
err = DecodeGob(i.Value, item)
err = decodeGob(i.Value, item)
if err != nil {
return nil, err
}
......
......@@ -22,8 +22,8 @@ func newRedisStorage(c *redis.Client) *redisStorage {
// 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)
item := &cachedItem{Val: val}
value, err := encodeGob(item)
if err != nil {
return err
}
......@@ -42,8 +42,8 @@ func (s *redisStorage) Put(key string, val interface{}, expires time.Duration) e
func (s *redisStorage) Get(key string) (interface{}, error) {
v := s.c.Get(key)
item := &Item{}
err := DecodeGob([]byte(v.Val()), item)
item := &cachedItem{}
err := decodeGob([]byte(v.Val()), item)
if err == nil {
return item.Val, nil
......@@ -65,16 +65,3 @@ 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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment