-
Notifications
You must be signed in to change notification settings - Fork 0
/
gocache.go
114 lines (95 loc) · 2.87 KB
/
gocache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Package gocache provides a data race-free cache implementation in Go.
//
// Usage:
//
// cache := gocache.NewCache(time.Minute * 2) // with 2 minutes interval cleaning
// cache.Set("key", "value", time.Minute)
// value, found := cache.Get("key")
// cache.Delete("key")
// cache.Clear()
// size := cache.Size()
package gocache
import (
"sync"
"time"
)
// Cache represents a data race-free cache.
type Cache struct {
items map[string]cacheItem
mutex sync.RWMutex
cleanupExpiredPeriod time.Duration
}
type cacheItem struct {
value interface{}
expiration time.Time
}
// NewCache creates a new Cache instance.
func NewCache(cleanupExpiredPeriod time.Duration) *Cache {
cache := &Cache{
items: make(map[string]cacheItem),
}
cache.cleanupExpiredPeriod = cleanupExpiredPeriod
// Start a goroutine to periodically check for expired items and remove them
go cache.deleteExpiredItems()
return cache
}
// Get retrieves the value associated with the specified key from the cache.
// It returns the value and a boolean indicating whether the key was found or not.
// If the key is found but the associated item has expired, the value will be nil
// and the boolean will be false.
func (c *Cache) Get(key string) (interface{}, bool) {
c.mutex.RLock()
defer c.mutex.RUnlock()
item, found := c.items[key]
if !found {
return nil, false
}
if item.expiration.Before(time.Now()) {
return nil, false
}
return item.value, true
}
// Set adds or updates a key-value pair in the cache with the specified expiration duration.
// If the key already exists, its value and expiration are updated.
func (c *Cache) Set(key string, value interface{}, expiration time.Duration) {
c.mutex.Lock()
defer c.mutex.Unlock()
expirationTime := time.Now().Add(expiration)
c.items[key] = cacheItem{
value: value,
expiration: expirationTime,
}
}
// Delete removes the specified key and its associated value from the cache.
// If the key does not exist in the cache, the function does nothing.
func (c *Cache) Delete(key string) {
c.mutex.Lock()
defer c.mutex.Unlock()
delete(c.items, key)
}
// Clear removes all items from the cache, making it empty.
func (c *Cache) Clear() {
c.mutex.Lock()
defer c.mutex.Unlock()
c.items = make(map[string]cacheItem)
}
// Size returns the number of items currently stored in the cache.
func (c *Cache) Size() int {
c.mutex.RLock()
defer c.mutex.RUnlock()
return len(c.items)
}
// deleteExpiredItems is a background goroutine that periodically checks for expired items in the cache
// and removes them. It runs indefinitely after the Cache is created.
func (c *Cache) deleteExpiredItems() {
for {
<-time.After(c.cleanupExpiredPeriod) // Adjust the time interval for checking expired items
c.mutex.Lock()
for key, item := range c.items {
if item.expiration.Before(time.Now()) {
delete(c.items, key)
}
}
c.mutex.Unlock()
}
}