|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "runtime" |
| 6 | + "sync" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// NOTE: this is heavily based on patrickmn/go-cache: |
| 11 | +// https://github.com/patrickmn/go-cache |
| 12 | + |
| 13 | +// Cache is a thread-safe in-memory key/value store. |
| 14 | +type Cache struct { |
| 15 | + *cache |
| 16 | +} |
| 17 | + |
| 18 | +// Item is an item stored in the cache. |
| 19 | +type Item struct { |
| 20 | + Object interface{} |
| 21 | + Expiration int64 |
| 22 | +} |
| 23 | + |
| 24 | +type cache struct { |
| 25 | + // Items holds the elements in the cache. |
| 26 | + Items map[string]Item |
| 27 | + // Maximum number of items the cache can hold. |
| 28 | + MaxItems int |
| 29 | + mu sync.RWMutex |
| 30 | + janitor *janitor |
| 31 | +} |
| 32 | + |
| 33 | +// ItemCount returns the number of items in the cache. |
| 34 | +// This may include items that have expired, but have not yet been cleaned up. |
| 35 | +func (c *cache) ItemCount() int { |
| 36 | + c.mu.RLock() |
| 37 | + n := len(c.Items) |
| 38 | + c.mu.RUnlock() |
| 39 | + return n |
| 40 | +} |
| 41 | + |
| 42 | +func (c *cache) set(key string, value interface{}, expiration time.Duration) { |
| 43 | + var e int64 |
| 44 | + if expiration > 0 { |
| 45 | + e = time.Now().Add(expiration).UnixNano() |
| 46 | + } |
| 47 | + |
| 48 | + c.Items[key] = Item{ |
| 49 | + Object: value, |
| 50 | + Expiration: e, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Set adds an item to the cache, replacing any existing item. |
| 55 | +// If expiration is zero, the item never expires. |
| 56 | +// If the cache is full, Set will return an error. |
| 57 | +func (c *cache) Set(key string, value interface{}, expiration time.Duration) error { |
| 58 | + c.mu.Lock() |
| 59 | + _, found := c.Items[key] |
| 60 | + if found { |
| 61 | + c.set(key, value, expiration) |
| 62 | + c.mu.Unlock() |
| 63 | + return nil |
| 64 | + } |
| 65 | + |
| 66 | + if c.MaxItems > 0 && len(c.Items) < c.MaxItems { |
| 67 | + c.set(key, value, expiration) |
| 68 | + c.mu.Unlock() |
| 69 | + return nil |
| 70 | + } |
| 71 | + |
| 72 | + c.mu.Unlock() |
| 73 | + return fmt.Errorf("Cache is full") |
| 74 | +} |
| 75 | + |
| 76 | +func (c *cache) Add(key string, value interface{}, expiration time.Duration) error { |
| 77 | + c.mu.Lock() |
| 78 | + _, found := c.Items[key] |
| 79 | + if found { |
| 80 | + c.mu.Unlock() |
| 81 | + return fmt.Errorf("Item %s already exists", key) |
| 82 | + } |
| 83 | + |
| 84 | + if c.MaxItems > 0 && len(c.Items) < c.MaxItems { |
| 85 | + c.set(key, value, expiration) |
| 86 | + c.mu.Unlock() |
| 87 | + return nil |
| 88 | + } |
| 89 | + |
| 90 | + c.mu.Unlock() |
| 91 | + return fmt.Errorf("Cache is full") |
| 92 | +} |
| 93 | + |
| 94 | +func (c *cache) Get(key string) (interface{}, bool) { |
| 95 | + c.mu.RLock() |
| 96 | + item, found := c.Items[key] |
| 97 | + if !found { |
| 98 | + c.mu.RUnlock() |
| 99 | + return nil, false |
| 100 | + } |
| 101 | + if item.Expiration > 0 { |
| 102 | + if item.Expiration < time.Now().UnixNano() { |
| 103 | + c.mu.RUnlock() |
| 104 | + return nil, false |
| 105 | + } |
| 106 | + } |
| 107 | + c.mu.RUnlock() |
| 108 | + return item.Object, true |
| 109 | +} |
| 110 | + |
| 111 | +func (c *cache) Delete(key string) { |
| 112 | + c.mu.Lock() |
| 113 | + delete(c.Items, key) |
| 114 | + c.mu.Unlock() |
| 115 | +} |
| 116 | + |
| 117 | +func (c *cache) Clear() { |
| 118 | + c.mu.Lock() |
| 119 | + c.Items = make(map[string]Item) |
| 120 | + c.mu.Unlock() |
| 121 | +} |
| 122 | + |
| 123 | +func (c *cache) HasExpired(key string) bool { |
| 124 | + c.mu.RLock() |
| 125 | + item, ok := c.Items[key] |
| 126 | + if !ok { |
| 127 | + c.mu.RUnlock() |
| 128 | + return true |
| 129 | + } |
| 130 | + if item.Expiration > 0 { |
| 131 | + if item.Expiration < time.Now().UnixNano() { |
| 132 | + c.mu.RUnlock() |
| 133 | + return true |
| 134 | + } |
| 135 | + } |
| 136 | + c.mu.RUnlock() |
| 137 | + return false |
| 138 | +} |
| 139 | + |
| 140 | +func (c *cache) SetExpiration(key string, expiration time.Duration) { |
| 141 | + c.mu.Lock() |
| 142 | + item, ok := c.Items[key] |
| 143 | + if !ok { |
| 144 | + c.mu.Unlock() |
| 145 | + return |
| 146 | + } |
| 147 | + item.Expiration = time.Now().Add(expiration).UnixNano() |
| 148 | + c.mu.Unlock() |
| 149 | +} |
| 150 | + |
| 151 | +func (c *cache) GetExpiration(key string) time.Duration { |
| 152 | + c.mu.RLock() |
| 153 | + item, ok := c.Items[key] |
| 154 | + if !ok { |
| 155 | + c.mu.RUnlock() |
| 156 | + return 0 |
| 157 | + } |
| 158 | + if item.Expiration > 0 { |
| 159 | + if item.Expiration < time.Now().UnixNano() { |
| 160 | + c.mu.RUnlock() |
| 161 | + return 0 |
| 162 | + } |
| 163 | + } |
| 164 | + c.mu.RUnlock() |
| 165 | + return time.Duration(item.Expiration - time.Now().UnixNano()) |
| 166 | +} |
| 167 | + |
| 168 | +func (c *cache) DeleteExpired() { |
| 169 | + c.mu.Lock() |
| 170 | + for k, v := range c.Items { |
| 171 | + if v.Expiration > 0 && v.Expiration < time.Now().UnixNano() { |
| 172 | + delete(c.Items, k) |
| 173 | + } |
| 174 | + } |
| 175 | + c.mu.Unlock() |
| 176 | +} |
| 177 | + |
| 178 | +type janitor struct { |
| 179 | + Interval time.Duration |
| 180 | + stop chan bool |
| 181 | +} |
| 182 | + |
| 183 | +func (j *janitor) Run(c *cache) { |
| 184 | + ticker := time.NewTicker(j.Interval) |
| 185 | + for { |
| 186 | + select { |
| 187 | + case <-ticker.C: |
| 188 | + c.DeleteExpired() |
| 189 | + case <-j.stop: |
| 190 | + ticker.Stop() |
| 191 | + return |
| 192 | + } |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +func stopJanitor(c *Cache) { |
| 197 | + c.janitor.stop <- true |
| 198 | +} |
| 199 | + |
| 200 | +func New(maxItems int, interval time.Duration) *Cache { |
| 201 | + c := &cache{ |
| 202 | + Items: make(map[string]Item), |
| 203 | + MaxItems: maxItems, |
| 204 | + janitor: &janitor{ |
| 205 | + Interval: interval, |
| 206 | + stop: make(chan bool), |
| 207 | + }, |
| 208 | + } |
| 209 | + |
| 210 | + C := &Cache{c} |
| 211 | + |
| 212 | + if interval > 0 { |
| 213 | + go c.janitor.Run(c) |
| 214 | + runtime.SetFinalizer(C, stopJanitor) |
| 215 | + } |
| 216 | + |
| 217 | + return C |
| 218 | +} |
0 commit comments