Skip to content

Commit 8414dff

Browse files
committed
refactor config in cache
1 parent 7a955fb commit 8414dff

1 file changed

Lines changed: 11 additions & 13 deletions

File tree

mempot.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ type Cache[K comparable, T any] struct {
2727
mut sync.RWMutex
2828
data map[K]Item[T]
2929

30-
ctx context.Context
31-
defaultTTL time.Duration
32-
cleanupInterval time.Duration
30+
ctx context.Context
31+
cfg Config
3332
}
3433

3534
// Item is a unit of typed data which can be cached and has an expiration as Unix epoch.
@@ -47,18 +46,17 @@ func (i *Item[T]) Expired() bool {
4746
// If the context is canceled, the Cache will stop the cleanup goroutine.
4847
func NewCache[K comparable, T any](ctx context.Context, cfg Config) *Cache[K, T] {
4948
c := &Cache[K, T]{
50-
data: make(map[K]Item[T]),
51-
ctx: ctx,
52-
defaultTTL: DefaultConfig.DefaultTTL,
53-
cleanupInterval: DefaultConfig.CleanupInterval,
49+
data: make(map[K]Item[T]),
50+
ctx: ctx,
51+
cfg: DefaultConfig,
5452
}
5553

5654
if cfg.DefaultTTL > 0 {
57-
c.defaultTTL = cfg.DefaultTTL
55+
c.cfg.DefaultTTL = cfg.DefaultTTL
5856
}
5957

6058
if cfg.CleanupInterval > 0 {
61-
c.cleanupInterval = cfg.CleanupInterval
59+
c.cfg.CleanupInterval = cfg.CleanupInterval
6260
}
6361

6462
go c.cleanup()
@@ -68,7 +66,7 @@ func NewCache[K comparable, T any](ctx context.Context, cfg Config) *Cache[K, T]
6866

6967
// Set will add an Item to the Cache with the default time-to-live.
7068
func (c *Cache[K, T]) Set(key K, value T) {
71-
c.SetWithTTL(key, value, c.defaultTTL)
69+
c.SetWithTTL(key, value, c.cfg.DefaultTTL)
7270
}
7371

7472
// SetWithTTL will add an Item to the Cache with the given time-to-live.
@@ -98,7 +96,7 @@ type QueryFunc[K comparable, T any] func(key K) (T, error)
9896
// Remember tries to get the Item from the Cache, if the Item is not found or expired QueryFunc is called
9997
// to retrieve the data from source and put it into the Cache.
10098
func (c *Cache[K, T]) Remember(key K, query QueryFunc[K, T]) (Item[T], error) {
101-
return c.RememberWithTTL(key, query, c.defaultTTL)
99+
return c.RememberWithTTL(key, query, c.cfg.DefaultTTL)
102100
}
103101

104102
// RememberWithTTL tries to get the Item from the Cache, if the Item is not found or expired QueryFunc is called
@@ -116,7 +114,7 @@ func (c *Cache[K, T]) RememberWithTTL(key K, query QueryFunc[K, T], ttl time.Dur
116114

117115
c.SetWithTTL(key, data, ttl)
118116

119-
return Item[T]{Data: data, TTL: time.Now().Add(c.defaultTTL).Unix()}, nil
117+
return Item[T]{Data: data, TTL: time.Now().Add(c.cfg.DefaultTTL).Unix()}, nil
120118
}
121119

122120
// Delete removes an Item from the Cache.
@@ -134,7 +132,7 @@ func (c *Cache[K, T]) Reset() {
134132
}
135133

136134
func (c *Cache[K, T]) cleanup() {
137-
ticker := time.NewTicker(c.cleanupInterval)
135+
ticker := time.NewTicker(c.cfg.CleanupInterval)
138136

139137
for {
140138
select {

0 commit comments

Comments
 (0)