فهرست منبع

avoid exposing cache client directly

bergquist 6 سال پیش
والد
کامیت
b2967fbb37
2فایلهای تغییر یافته به همراه17 افزوده شده و 5 حذف شده
  1. 16 4
      pkg/infra/remotecache/remotecache.go
  2. 1 1
      pkg/infra/remotecache/remotecache_test.go

+ 16 - 4
pkg/infra/remotecache/remotecache.go

@@ -31,7 +31,7 @@ type CacheStorage interface {
 	// Get reads object from Cache
 	Get(key string) (interface{}, error)
 
-	// Set sets an object into the cache
+	// Set sets an object into the cache. if `expire` is set to zero it never expires.
 	Set(key string, value interface{}, expire time.Duration) error
 
 	// Delete object from cache
@@ -41,16 +41,28 @@ type CacheStorage interface {
 // RemoteCache allows Grafana to cache data outside its own process
 type RemoteCache struct {
 	log      log.Logger
-	Client   CacheStorage
+	client   CacheStorage
 	SQLStore *sqlstore.SqlStore `inject:""`
 	Cfg      *setting.Cfg       `inject:""`
 }
 
+func (ds *RemoteCache) Get(key string) (interface{}, error) {
+	return ds.client.Get(key)
+}
+
+func (ds *RemoteCache) Set(key string, value interface{}, expire time.Duration) error {
+	return ds.client.Set(key, value, expire)
+}
+
+func (ds *RemoteCache) Delete(key string) error {
+	return ds.client.Delete(key)
+}
+
 // Init initializes the service
 func (ds *RemoteCache) Init() error {
 	ds.log = log.New("cache.remote")
 
-	ds.Client = createClient(ds.Cfg.RemoteCacheOptions, ds.SQLStore)
+	ds.client = createClient(ds.Cfg.RemoteCacheOptions, ds.SQLStore)
 
 	return nil
 }
@@ -58,7 +70,7 @@ func (ds *RemoteCache) Init() error {
 // Run start the backend processes for cache clients
 func (ds *RemoteCache) Run(ctx context.Context) error {
 	//create new interface if more clients need GC jobs
-	backgroundjob, ok := ds.Client.(registry.BackgroundService)
+	backgroundjob, ok := ds.client.(registry.BackgroundService)
 	if ok {
 		return backgroundjob.Run(ctx)
 	}

+ 1 - 1
pkg/infra/remotecache/remotecache_test.go

@@ -34,7 +34,7 @@ func createTestClient(t *testing.T, opts *setting.RemoteCacheOptions, sqlstore *
 		t.Fatalf("failed to init client for test. error: %v", err)
 	}
 
-	return dc.Client
+	return dc.client
 }
 
 func TestCachedBasedOnConfig(t *testing.T) {