Skip to content

Commit

Permalink
feat(redis): reorganize code in pkg/storage/cache.go
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Aaron <[email protected]>
  • Loading branch information
andaaron committed Jan 26, 2025
1 parent da1e6f5 commit d5b1188
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
15 changes: 13 additions & 2 deletions pkg/api/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,23 @@ func TestCreateCacheDatabaseDriver(t *testing.T) {

log := log.NewLogger("debug", "")

// fail create db, no perm
dir := t.TempDir()
conf := config.New()
conf.Storage.RootDirectory = dir
conf.Storage.Dedupe = true
conf.Storage.RemoteCache = true

// test error on invalid redis client config
conf.Storage.CacheDriver = map[string]interface{}{
"name": "redis",
"url": false,
}

driver, err := storage.CreateCacheDatabaseDriver(conf.Storage.StorageConfig, log)
So(err, ShouldNotBeNil)
So(driver, ShouldBeNil)

// test valid redis client config
conf.Storage.CacheDriver = map[string]interface{}{
"name": "redis",
"url": "redis://" + miniRedis.Addr(),
Expand All @@ -178,7 +189,7 @@ func TestCreateCacheDatabaseDriver(t *testing.T) {
"url": "us-east-2",
}

driver, err := storage.CreateCacheDatabaseDriver(conf.Storage.StorageConfig, log)
driver, err = storage.CreateCacheDatabaseDriver(conf.Storage.StorageConfig, log)
So(err, ShouldBeNil)
So(driver, ShouldNotBeNil)
So(driver.Name(), ShouldEqual, "redis")
Expand Down
40 changes: 27 additions & 13 deletions pkg/storage/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,16 @@ func CreateCacheDatabaseDriver(storageConfig config.StorageConfig, log zlog.Logg

if name == constants.DynamoDBDriverName {
// dynamodb
dynamoParams := cache.DynamoDBDriverParameters{}
dynamoParams.Endpoint, _ = storageConfig.CacheDriver["endpoint"].(string)
dynamoParams.Region, _ = storageConfig.CacheDriver["region"].(string)
dynamoParams.TableName, _ = storageConfig.CacheDriver["cachetablename"].(string)

return Create(name, dynamoParams, log)
return Create(name, getDynamoParams(&storageConfig), log)
}

if name == constants.RedisDriverName {
// redis
client, err := rediscfg.GetRedisClient(storageConfig.CacheDriver, log)
redisParams, err := getRedisParams(&storageConfig, log)
if err != nil {
return nil, err
}

redisParams := cache.RedisDriverParameters{}
redisParams.RootDir = storageConfig.RootDirectory
redisParams.Client = client
redisParams.KeyPrefix, _ = storageConfig.CacheDriver["keyprefix"].(string)
redisParams.UseRelPaths = getUseRelPaths(&storageConfig)

return Create(name, redisParams, log)
}
}
Expand Down Expand Up @@ -96,3 +85,28 @@ func getUseRelPaths(storageConfig *config.StorageConfig) bool {
// In case of local storage we use rel paths, in case of S3 we don't
return storageConfig.StorageDriver == nil
}

func getDynamoParams(storageConfig *config.StorageConfig) cache.DynamoDBDriverParameters {
dynamoParams := cache.DynamoDBDriverParameters{}
dynamoParams.Endpoint, _ = storageConfig.CacheDriver["endpoint"].(string)
dynamoParams.Region, _ = storageConfig.CacheDriver["region"].(string)
dynamoParams.TableName, _ = storageConfig.CacheDriver["cachetablename"].(string)

return dynamoParams
}

func getRedisParams(storageConfig *config.StorageConfig, log zlog.Logger) (cache.RedisDriverParameters, error) {
redisParams := cache.RedisDriverParameters{}

client, err := rediscfg.GetRedisClient(storageConfig.CacheDriver, log)
if err != nil {
return redisParams, err
}

redisParams.RootDir = storageConfig.RootDirectory
redisParams.Client = client
redisParams.KeyPrefix, _ = storageConfig.CacheDriver["keyprefix"].(string)
redisParams.UseRelPaths = getUseRelPaths(storageConfig)

return redisParams, nil
}

0 comments on commit d5b1188

Please sign in to comment.