Skip to content

Commit

Permalink
Merge pull request #1 from cgauge/cache-concurrence
Browse files Browse the repository at this point in the history
Avoid map access concurrence
  • Loading branch information
abdala authored Apr 12, 2022
2 parents c5b719a + 5aacb76 commit dc7e86f
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions secrets/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"sync"
"time"
)

Expand All @@ -26,6 +27,8 @@ type CacheData struct {
CacheExpiry time.Time
}

var mutex = &sync.Mutex{}

func IsExpired(cacheExpiry time.Time) bool {
return cacheExpiry.Before(time.Now())
}
Expand All @@ -48,12 +51,14 @@ func GetSecretCache(name string, refresh string) string {
secret := secretCache[name]

if IsExpired(secret.CacheData.CacheExpiry) || refresh == "1" {
mutex.Lock()
secretCache[name] = Secret{
CacheData: CacheData{
Data: GetSecret(name),
CacheExpiry: GetCacheExpiry(),
},
}
mutex.Unlock()
}

return secretCache[name].CacheData.Data
Expand Down

0 comments on commit dc7e86f

Please sign in to comment.