return error if cache type is invalid

This commit is contained in:
bergquist 2019-03-14 08:57:38 +01:00
parent 7aeab0a235
commit 5186273731
4 changed files with 24 additions and 11 deletions

View File

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

View File

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

View File

@ -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
}
return newDatabaseCache(sqlstore)
if opts.Name == "database" {
return newDatabaseCache(sqlstore), nil
}
return nil, ErrInvalidCacheType
}
// Register records a type, identified by a value for that type, under its

View File

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