Skip to content

Commit d12ed10

Browse files
authored
chore: add method to return cached config value (#1944)
* chore: add method to return cached config value * fix: add check for relay urls when starting NWC wallet service * fix: mocks * chore: always use cache for config entries
1 parent db11670 commit d12ed10

3 files changed

Lines changed: 121 additions & 93 deletions

File tree

config/config.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"path"
1010
"strings"
11+
"sync"
1112

1213
"github.com/getAlby/hub/constants"
1314
"github.com/getAlby/hub/db"
@@ -18,8 +19,10 @@ import (
1819
)
1920

2021
type config struct {
21-
Env *AppConfig
22-
db *gorm.DB
22+
Env *AppConfig
23+
db *gorm.DB
24+
cache map[string]string
25+
cacheMutex sync.Mutex
2326
}
2427

2528
const (
@@ -28,7 +31,8 @@ const (
2831

2932
func NewConfig(env *AppConfig, db *gorm.DB) (*config, error) {
3033
cfg := &config{
31-
db: db,
34+
db: db,
35+
cache: map[string]string{},
3236
}
3337
err := cfg.init(env)
3438
if err != nil {
@@ -177,7 +181,22 @@ func (cfg *config) GetMempoolUrl() string {
177181
}
178182

179183
func (cfg *config) Get(key string, encryptionKey string) (string, error) {
180-
return cfg.get(key, encryptionKey, cfg.db)
184+
cfg.cacheMutex.Lock()
185+
defer cfg.cacheMutex.Unlock()
186+
cachedValue, ok := cfg.cache[key]
187+
if ok {
188+
logger.Logger.WithField("key", key).Debug("hit config cache")
189+
return cachedValue, nil
190+
}
191+
logger.Logger.WithField("key", key).Debug("missed config cache")
192+
193+
value, err := cfg.get(key, encryptionKey, cfg.db)
194+
if err != nil {
195+
return "", err
196+
}
197+
cfg.cache[key] = value
198+
logger.Logger.WithField("key", key).Debug("set config cache")
199+
return value, nil
181200
}
182201

183202
func (cfg *config) get(key string, encryptionKey string, gormDB *gorm.DB) (string, error) {
@@ -212,6 +231,12 @@ func (cfg *config) set(key string, value string, clauses clause.OnConflict, encr
212231
if result.Error != nil {
213232
return fmt.Errorf("failed to save key to config: %v", result.Error)
214233
}
234+
235+
logger.Logger.WithField("key", key).Debug("clearing config cache")
236+
cfg.cacheMutex.Lock()
237+
defer cfg.cacheMutex.Unlock()
238+
delete(cfg.cache, key)
239+
215240
return nil
216241
}
217242

service/start.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ import (
3030

3131
func (svc *service) startNostr(ctx context.Context) error {
3232
relayUrls := svc.cfg.GetRelayUrls()
33+
if len(relayUrls) == 0 {
34+
return errors.New("No relay URLs found")
35+
}
3336

3437
npub, err := nip19.EncodePublicKey(svc.keys.GetNostrPublicKey())
3538
if err != nil {

tests/mocks/Config.go

Lines changed: 89 additions & 89 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)