Skip to content

Commit

Permalink
chore: add a new cache type to make code readable
Browse files Browse the repository at this point in the history
Signed-off-by: Aris Boutselis <arisboutselis08@gmail.com>
  • Loading branch information
arbreezy committed Oct 8, 2023
1 parent 0a811b4 commit 4a919a1
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import (
"github.com/spf13/viper"
)

type CacheType string

const (
Azure CacheType = "azure"
S3 CacheType = "s3"
FileBased CacheType = "file"
)

type ICache interface {
Store(key string, data string) error
Load(key string) (string, error)
Expand All @@ -15,12 +23,16 @@ type ICache interface {
IsCacheDisabled() bool
}

func New(noCache bool, remoteCache string) ICache {
func New(noCache bool, remoteCache CacheType) ICache {
switch remoteCache {
case "s3":
case S3:
return NewS3Cache(noCache)
case "azure":
case Azure:
return NewAzureCache(noCache)
case FileBased:
return &FileBasedCache{
noCache: noCache,
}
default:
return &FileBasedCache{
noCache: noCache,
Expand All @@ -47,19 +59,19 @@ func NewCacheProvider(bucketname, region, storageaccount, containername string)
}

// If we have set a remote cache, return the remote cache type
func RemoteCacheEnabled() (string, error) {
func RemoteCacheEnabled() (CacheType, error) {
// load remote cache if it is configured
var cache CacheProvider
err := viper.UnmarshalKey("cache", &cache)
if err != nil {
return "", err
}
if cache.BucketName != "" && cache.Region != "" {
return "s3", nil
return S3, nil
} else if cache.StorageAccount != "" && cache.ContainerName != "" {
return "azure", nil
return Azure, nil
}
return "", nil
return FileBased, nil
}

func AddRemoteCache(cache CacheProvider) error {
Expand Down

0 comments on commit 4a919a1

Please sign in to comment.