Explorar o código

avoid exposing internal structs and functions

bergquist %!s(int64=6) %!d(string=hai) anos
pai
achega
c8ff698d90

+ 8 - 8
pkg/infra/distcache/database_storage.go

@@ -45,13 +45,13 @@ func (dc *databaseCache) StartGC() {
 }
 }
 
 
 func (dc *databaseCache) Get(key string) (interface{}, error) {
 func (dc *databaseCache) Get(key string) (interface{}, error) {
-	cacheHits := []CacheData{}
+	cacheHits := []cacheData{}
 	err := dc.SQLStore.NewSession().Where(`key = ?`, key).Find(&cacheHits)
 	err := dc.SQLStore.NewSession().Where(`key = ?`, key).Find(&cacheHits)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}
 
 
-	var cacheHit CacheData
+	var cacheHit cacheData
 	if len(cacheHits) == 0 {
 	if len(cacheHits) == 0 {
 		return nil, ErrCacheItemNotFound
 		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 nil, err
 	}
 	}
 
 
 	return item.Val, nil
 	return item.Val, nil
 }
 }
 
 
-type CacheData struct {
+type cacheData struct {
 	Key       string
 	Key       string
 	Data      []byte
 	Data      []byte
 	Expires   int64
 	Expires   int64
@@ -80,15 +80,15 @@ type CacheData struct {
 }
 }
 
 
 func (dc *databaseCache) Put(key string, value interface{}, expire time.Duration) error {
 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 {
 	if err != nil {
 		return err
 		return err
 	}
 	}
 
 
 	now := getTime().Unix()
 	now := getTime().Unix()
 
 
-	cacheHits := []CacheData{}
+	cacheHits := []cacheData{}
 	err = dc.SQLStore.NewSession().Where(`key = ?`, key).Find(&cacheHits)
 	err = dc.SQLStore.NewSession().Where(`key = ?`, key).Find(&cacheHits)
 	if err != nil {
 	if err != nil {
 		return err
 		return err

+ 5 - 7
pkg/infra/distcache/distcache.go

@@ -46,7 +46,7 @@ func createClient(opts CacheOpts, sqlstore *sqlstore.SqlStore) cacheStorage {
 	// 	return nil
 	// 	return nil
 	// }
 	// }
 
 
-	return newDatabaseCache(sqlstore) //&databaseCache{SQLStore: sqlstore}
+	return newDatabaseCache(sqlstore)
 }
 }
 
 
 // DistributedCache allows Grafana to cache data outside its own process
 // DistributedCache allows Grafana to cache data outside its own process
@@ -56,19 +56,17 @@ type DistributedCache struct {
 	SQLStore *sqlstore.SqlStore `inject:""`
 	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)
 	buf := bytes.NewBuffer(nil)
 	err := gob.NewEncoder(buf).Encode(item)
 	err := gob.NewEncoder(buf).Encode(item)
 	return buf.Bytes(), err
 	return buf.Bytes(), err
 }
 }
 
 
-func DecodeGob(data []byte, out *Item) error {
+func decodeGob(data []byte, out *cachedItem) error {
 	buf := bytes.NewBuffer(data)
 	buf := bytes.NewBuffer(data)
 	return gob.NewDecoder(buf).Decode(&out)
 	return gob.NewDecoder(buf).Decode(&out)
 }
 }

+ 5 - 9
pkg/infra/distcache/distcache_test.go

@@ -27,7 +27,7 @@ func createTestClient(t *testing.T, name string) cacheStorage {
 }
 }
 
 
 func TestAllCacheClients(t *testing.T) {
 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 {
 	for _, v := range clients {
 		client := createTestClient(t, v)
 		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) {
 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}
 	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)
 	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
 	// should not be able to read that value since its expired
-	getTime = time.Now
 	_, err = client.Get("key")
 	_, err = client.Get("key")
 	assert.Equal(t, err, ErrCacheItemNotFound)
 	assert.Equal(t, err, ErrCacheItemNotFound)
 }
 }

+ 6 - 6
pkg/infra/distcache/memcached_storage.go

@@ -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{
 	return &memcache.Item{
 		Key:        sid,
 		Key:        sid,
 		Value:      data,
 		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.
 // Set sets value to given key in the cache.
 func (s *memcacheStorage) Put(key string, val interface{}, expires time.Duration) error {
 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 {
 	if err != nil {
 		return err
 		return err
 	}
 	}
 
 
-	memcacheItem := NewItem(key, bytes, int32(expires))
+	memcacheItem := newItem(key, bytes, int32(expires))
 
 
 	s.c.Add(memcacheItem)
 	s.c.Add(memcacheItem)
 	return nil
 	return nil
@@ -46,9 +46,9 @@ func (s *memcacheStorage) Get(key string) (interface{}, error) {
 		return nil, err
 		return nil, err
 	}
 	}
 
 
-	item := &Item{}
+	item := &cachedItem{}
 
 
-	err = DecodeGob(i.Value, item)
+	err = decodeGob(i.Value, item)
 	if err != nil {
 	if err != nil {
 		return nil, err
 		return nil, err
 	}
 	}

+ 4 - 17
pkg/infra/distcache/redis_storage.go

@@ -22,8 +22,8 @@ func newRedisStorage(c *redis.Client) *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) 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 {
 	if err != nil {
 		return err
 		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) {
 func (s *redisStorage) Get(key string) (interface{}, error) {
 	v := s.c.Get(key)
 	v := s.c.Get(key)
 
 
-	item := &Item{}
-	err := DecodeGob([]byte(v.Val()), item)
+	item := &cachedItem{}
+	err := decodeGob([]byte(v.Val()), item)
 
 
 	if err == nil {
 	if err == nil {
 		return item.Val, nil
 		return item.Val, nil
@@ -65,16 +65,3 @@ func (s *redisStorage) Delete(key string) error {
 	cmd := s.c.Del(key)
 	cmd := s.c.Del(key)
 	return cmd.Err()
 	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
-}