Skip to content

Commit 724008f

Browse files
committed
small adjustments
1 parent e9d28bb commit 724008f

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

cmd/cache-offloader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import (
1919
func getInMemoryStorage(cfg config.Config) http.Cacher {
2020
switch strings.ToLower(cfg.CacheConfig.Strategy) {
2121
case "lru":
22-
return storage.NewLRUCache(cfg.CacheConfig.Size, 5*60 /*5 minutes*/)
22+
return storage.NewLRUCache(cfg.CacheConfig.Size, cfg.CacheConfig.StaleInSeconds)
2323
case "lfu":
24-
storage.NewLFUCache(cfg.CacheConfig.Size, 5*60 /*5 minutes*/)
24+
storage.NewLFUCache(cfg.CacheConfig.Size, cfg.CacheConfig.StaleInSeconds)
2525
default:
2626
log.Error().Msgf("Unknown cache strategy: %s", cfg.CacheConfig.Strategy)
2727
log.Fatal().Msg("Supported cache strategies are LRU and LFU")

pkg/storage/helpers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ func getSize(value model.Response) float64 {
1313
return sizeMB
1414
}
1515

16-
func getStaleStatus(timeStamp int64, staleDuration int64) uint8 {
17-
if (time.Now().Unix() - timeStamp) >= staleDuration {
16+
func getStaleStatus(timeStamp int64, staleDuration int) uint8 {
17+
if (time.Now().Unix() - timeStamp) >= int64(staleDuration) {
1818
return model.StaleValue
19-
} else {
20-
return model.FreshValue
2119
}
20+
21+
return model.FreshValue
2222
}

pkg/storage/memory-lfu.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type LFUCache struct {
1919
lookupTimeout time.Duration
2020
lists map[uint]*FrequencyList
2121
cache map[string]*list.Element
22-
staleDuration int64
22+
staleDuration int
2323
}
2424

2525
type FrequencyList struct {
@@ -34,7 +34,7 @@ type LfuNode struct {
3434
key string
3535
}
3636

37-
func NewLFUCache(maxSizeMB float64, staleInSeconds int64) *LFUCache {
37+
func NewLFUCache(maxSizeMB float64, staleInSeconds int) *LFUCache {
3838
if maxSizeMB <= 0 {
3939
maxSizeMB = 50.0
4040
}
@@ -66,7 +66,10 @@ func (lfu *LFUCache) Store(ctx context.Context, key string, value *model.Respons
6666

6767
if found {
6868
bodySizeMB -= getSize(*val.Value.(*LfuNode).value)
69-
node := val.Value.(*LfuNode)
69+
node, ok := val.Value.(*LfuNode)
70+
if !ok {
71+
log.Warn().Msg("The node is not a LfuNode")
72+
}
7073
node.value = value
7174
node.timeStamp = time.Now().Unix()
7275
lfu.update(val)
@@ -105,7 +108,10 @@ func (lfu *LFUCache) LookUp(ctx context.Context, key string) (*model.Response, e
105108

106109
if val, found := lfu.cache[key]; found {
107110
lfu.update(val)
108-
node := val.Value.(*LfuNode)
111+
node, ok := val.Value.(*LfuNode)
112+
if !ok {
113+
log.Warn().Msg("The node is not a LfuNode")
114+
}
109115
response := node.value
110116
response.StaleValue = getStaleStatus(node.timeStamp, lfu.staleDuration)
111117

pkg/storage/memory-lru.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type LRUCache struct {
1717
capacityMB float64
1818
sizeMB float64
1919
lookupTimeout time.Duration
20-
staleDuration int64
20+
staleDuration int
2121
}
2222

2323
type LRUNode struct {
@@ -26,7 +26,7 @@ type LRUNode struct {
2626
timeStamp int64
2727
}
2828

29-
func NewLRUCache(maxSizeMB float64, staleInSeconds int64) *LRUCache {
29+
func NewLRUCache(maxSizeMB float64, staleInSeconds int) *LRUCache {
3030
if maxSizeMB <= 0 {
3131
maxSizeMB = 50.0
3232
}
@@ -55,7 +55,10 @@ func (lru *LRUCache) Store(ctx context.Context, key string, value *model.Respons
5555

5656
if val, found := lru.cache[key]; found {
5757
bodySizeMB -= getSize(*val.Value.(*LRUNode).value)
58-
node := val.Value.(*LRUNode)
58+
node, ok := val.Value.(*LRUNode)
59+
if !ok {
60+
log.Warn().Msg("The node is not a LRUNode")
61+
}
5962
node.value = value
6063
node.timeStamp = time.Now().Unix()
6164

@@ -86,7 +89,10 @@ func (lru *LRUCache) LookUp(ctx context.Context, key string) (*model.Response, e
8689

8790
if value, found := lru.cache[key]; found {
8891
lru.responses.MoveToFront(value)
89-
node := value.Value.(*LRUNode)
92+
node, ok := value.Value.(*LRUNode)
93+
if !ok {
94+
log.Warn().Msg("The node is not a LRUNode")
95+
}
9096
response := node.value
9197
response.StaleValue = getStaleStatus(node.timeStamp, lru.staleDuration)
9298

0 commit comments

Comments
 (0)