Skip to content

Commit 5a9f7fc

Browse files
committed
make entries private for the caching
1 parent 33e1f7d commit 5a9f7fc

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

cache.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type entry[V any] struct {
5858

5959
// Cache is a generic cache with a time-to-live (TTL) for each entry.
6060
type Cache[K comparable, V any] struct {
61-
Entries map[K]entry[V]
61+
entries map[K]entry[V]
6262
ttl int64
6363
cacheGroup *cacheGroup
6464
mu sync.RWMutex
@@ -68,7 +68,7 @@ type Cache[K comparable, V any] struct {
6868
// NewCache creates a new cache with the specified TTL.
6969
func NewCache[K comparable, V any](ttl int64) *Cache[K, V] {
7070
return &Cache[K, V]{
71-
Entries: make(map[K]entry[V]),
71+
entries: make(map[K]entry[V]),
7272
cacheGroup: cacheGroupInstance,
7373
ttl: ttl,
7474
zeroVal: zeroValue[V](),
@@ -78,7 +78,7 @@ func NewCache[K comparable, V any](ttl int64) *Cache[K, V] {
7878
// NewCacheSized creates a new cache with the specified size and TTL.
7979
func NewCacheSized[K comparable, V any](size int, ttl int64) *Cache[K, V] {
8080
return &Cache[K, V]{
81-
Entries: make(map[K]entry[V], size),
81+
entries: make(map[K]entry[V], size),
8282
cacheGroup: cacheGroupInstance,
8383
ttl: ttl,
8484
zeroVal: zeroValue[V](),
@@ -93,7 +93,7 @@ func (c *Cache[K, V]) NowUnix() int64 {
9393
// GetOrCompute retrieves the value for the given key or computes it using the provided function if not present or expired.
9494
func (c *Cache[K, V]) GetOrCompute(key K, computeFn func() V) V {
9595
c.mu.RLock()
96-
existingEntry, ok := c.Entries[key]
96+
existingEntry, ok := c.entries[key]
9797
c.mu.RUnlock()
9898

9999
now := c.NowUnix()
@@ -103,30 +103,30 @@ func (c *Cache[K, V]) GetOrCompute(key K, computeFn func() V) V {
103103

104104
c.mu.Lock()
105105
newVal := computeFn()
106-
c.Entries[key] = entry[V]{value: newVal, timeStamp: now}
106+
c.entries[key] = entry[V]{value: newVal, timeStamp: now}
107107
c.mu.Unlock()
108108
return newVal
109109
}
110110

111111
// Delete removes the entry for the given key from the cache.
112112
func (c *Cache[K, V]) Delete(key K) {
113113
c.mu.Lock()
114-
delete(c.Entries, key)
114+
delete(c.entries, key)
115115
c.mu.Unlock()
116116
}
117117

118118
// Set adds or updates the value for the given key in the cache.
119119
func (c *Cache[K, V]) Set(key K, value V) {
120120
timeStamp := c.NowUnix()
121121
c.mu.Lock()
122-
c.Entries[key] = entry[V]{value: value, timeStamp: timeStamp}
122+
c.entries[key] = entry[V]{value: value, timeStamp: timeStamp}
123123
c.mu.Unlock()
124124
}
125125

126126
// Get retrieves the value for the given key from the cache if present and not expired.
127127
func (c *Cache[K, V]) Get(key K) (V, bool) {
128128
c.mu.RLock()
129-
entry, ok := c.Entries[key]
129+
entry, ok := c.entries[key]
130130
c.mu.RUnlock()
131131

132132
if ok && (c.ttl == 0 || c.NowUnix()-entry.timeStamp < c.ttl) {

0 commit comments

Comments
 (0)