avoid exposing cache client directly

This commit is contained in:
bergquist 2019-03-11 10:44:16 +01:00
parent 085b631099
commit b2967fbb37
2 changed files with 17 additions and 5 deletions

View File

@ -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)
}

View File

@ -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) {