diff --git a/pkg/infra/remotecache/memcached_storage_integration_test.go b/pkg/infra/remotecache/memcached_storage_integration_test.go index d55d78ff482..de7692e25d5 100644 --- a/pkg/infra/remotecache/memcached_storage_integration_test.go +++ b/pkg/infra/remotecache/memcached_storage_integration_test.go @@ -10,5 +10,6 @@ import ( func TestMemcachedCacheStorage(t *testing.T) { opts := &setting.RemoteCacheOptions{Name: "memcached", ConnStr: "localhost:11211"} - runTestsForClient(t, createTestClient(t, opts, nil)) + client := createTestClient(t, opts, nil) + runTestsForClient(t, client) } diff --git a/pkg/infra/remotecache/redis_storage_integration_test.go b/pkg/infra/remotecache/redis_storage_integration_test.go index bd834fb89ff..0a63fbe31ec 100644 --- a/pkg/infra/remotecache/redis_storage_integration_test.go +++ b/pkg/infra/remotecache/redis_storage_integration_test.go @@ -11,5 +11,6 @@ import ( func TestRedisCacheStorage(t *testing.T) { opts := &setting.RemoteCacheOptions{Name: "redis", ConnStr: "localhost:6379"} - runTestsForClient(t, createTestClient(t, opts, nil)) + client := createTestClient(t, opts, nil) + runTestsForClient(t, client) } diff --git a/pkg/infra/remotecache/remotecache.go b/pkg/infra/remotecache/remotecache.go index 1b9d67b9358..bd85529df0a 100644 --- a/pkg/infra/remotecache/remotecache.go +++ b/pkg/infra/remotecache/remotecache.go @@ -16,7 +16,11 @@ import ( ) var ( + // ErrCacheItemNotFound is returned if cache does not exist ErrCacheItemNotFound = errors.New("cache item not found") + + // ErrInvalidCacheType is returned if the type is invalid + ErrInvalidCacheType = errors.New("invalid remote cache name") ) func init() { @@ -61,10 +65,9 @@ func (ds *RemoteCache) Delete(key string) error { // Init initializes the service func (ds *RemoteCache) Init() error { ds.log = log.New("cache.remote") - - ds.client = createClient(ds.Cfg.RemoteCacheOptions, ds.SQLStore) - - return nil + var err error + ds.client, err = createClient(ds.Cfg.RemoteCacheOptions, ds.SQLStore) + return err } // Run start the backend processes for cache clients @@ -79,16 +82,20 @@ func (ds *RemoteCache) Run(ctx context.Context) error { return ctx.Err() } -func createClient(opts *setting.RemoteCacheOptions, sqlstore *sqlstore.SqlStore) CacheStorage { +func createClient(opts *setting.RemoteCacheOptions, sqlstore *sqlstore.SqlStore) (CacheStorage, error) { if opts.Name == "redis" { - return newRedisStorage(opts) + return newRedisStorage(opts), nil } if opts.Name == "memcached" { - return newMemcachedStorage(opts) + return newMemcachedStorage(opts), nil + } + + if opts.Name == "database" { + return newDatabaseCache(sqlstore), nil } - return newDatabaseCache(sqlstore) + return nil, ErrInvalidCacheType } // Register records a type, identified by a value for that type, under its diff --git a/pkg/infra/remotecache/remotecache_test.go b/pkg/infra/remotecache/remotecache_test.go index ac22607ee70..efeb75620ee 100644 --- a/pkg/infra/remotecache/remotecache_test.go +++ b/pkg/infra/remotecache/remotecache_test.go @@ -45,10 +45,14 @@ func TestCachedBasedOnConfig(t *testing.T) { }) client := createTestClient(t, cfg.RemoteCacheOptions, sqlstore.InitTestDB(t)) - runTestsForClient(t, client) } +func TestInvalidCacheTypeReturnsError(t *testing.T) { + _, err := createClient(&setting.RemoteCacheOptions{Name: "invalid"}, nil) + assert.Equal(t, err, ErrInvalidCacheType) +} + func runTestsForClient(t *testing.T, client CacheStorage) { canPutGetAndDeleteCachedObjects(t, client) canNotFetchExpiredItems(t, client)