| 1 | package gcache |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | ) |
| 6 | |
| 7 | // SimpleCache has no clear priority for evict cache. It depends on key-value map order. |
| 8 | type SimpleCache struct { |
| 9 | baseCache |
| 10 | items map[interface{}]*simpleItem |
| 11 | } |
| 12 | |
| 13 | func newSimpleCache(cb *CacheBuilder) *SimpleCache { |
| 14 | c := &SimpleCache{} |
| 15 | buildCache(&c.baseCache, cb) |
| 16 | |
| 17 | c.init() |
| 18 | c.loadGroup.cache = c |
| 19 | return c |
| 20 | } |
| 21 | |
| 22 | func (c *SimpleCache) init() { |
| 23 | if c.size <= 0 { |
| 24 | c.items = make(map[interface{}]*simpleItem) |
| 25 | } else { |
| 26 | c.items = make(map[interface{}]*simpleItem, c.size) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // Set a new key-value pair |
| 31 | func (c *SimpleCache) Set(key, value interface{}) error { |
| 32 | c.mu.Lock() |
| 33 | defer c.mu.Unlock() |
| 34 | _, err := c.set(key, value) |
| 35 | return err |
| 36 | } |
| 37 | |
| 38 | // Set a new key-value pair with an expiration time |
| 39 | func (c *SimpleCache) SetWithExpire(key, value interface{}, expiration time.Duration) error { |
| 40 | c.mu.Lock() |
| 41 | defer c.mu.Unlock() |
| 42 | item, err := c.set(key, value) |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | |
| 47 | t := c.clock.Now().Add(expiration) |
| 48 | item.(*simpleItem).expiration = &t |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | func (c *SimpleCache) set(key, value interface{}) (interface{}, error) { |
| 53 | var err error |
| 54 | if c.serializeFunc != nil { |
| 55 | value, err = c.serializeFunc(key, value) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Check for existing item |
| 62 | item, ok := c.items[key] |
| 63 | if ok { |
| 64 | item.value = value |
| 65 | } else { |
| 66 | // Verify size not exceeded |
| 67 | if (len(c.items) >= c.size) && c.size > 0 { |
| 68 | c.evict(1) |
| 69 | } |
| 70 | item = &simpleItem{ |
| 71 | clock: c.clock, |
| 72 | value: value, |
| 73 | } |
| 74 | c.items[key] = item |
| 75 | } |
| 76 | |
| 77 | if c.expiration != nil { |
| 78 | t := c.clock.Now().Add(*c.expiration) |
| 79 | item.expiration = &t |
| 80 | } |
| 81 | |
| 82 | if c.addedFunc != nil { |
| 83 | c.addedFunc(key, value) |
| 84 | } |
| 85 | |
| 86 | return item, nil |
| 87 | } |
| 88 | |
| 89 | // Get a value from cache pool using key if it exists. |
| 90 | // If it dose not exists key and has LoaderFunc, |
| 91 | // generate a value using `LoaderFunc` method returns value. |
| 92 | func (c *SimpleCache) Get(key interface{}) (interface{}, error) { |
| 93 | v, err := c.get(key, false) |
| 94 | if err == KeyNotFoundError { |
| 95 | return c.getWithLoader(key, true) |
| 96 | } |
| 97 | return v, err |
| 98 | } |
| 99 | |
| 100 | // GetIFPresent gets a value from cache pool using key if it exists. |
| 101 | // If it dose not exists key, returns KeyNotFoundError. |
| 102 | // And send a request which refresh value for specified key if cache object has LoaderFunc. |
| 103 | func (c *SimpleCache) GetIFPresent(key interface{}) (interface{}, error) { |
| 104 | v, err := c.get(key, false) |
| 105 | if err == KeyNotFoundError { |
| 106 | return c.getWithLoader(key, false) |
| 107 | } |
| 108 | return v, nil |
| 109 | } |
| 110 | |
| 111 | func (c *SimpleCache) get(key interface{}, onLoad bool) (interface{}, error) { |
| 112 | v, err := c.getValue(key, onLoad) |
| 113 | if err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | if c.deserializeFunc != nil { |
| 117 | return c.deserializeFunc(key, v) |
| 118 | } |
| 119 | return v, nil |
| 120 | } |
| 121 | |
| 122 | func (c *SimpleCache) getValue(key interface{}, onLoad bool) (interface{}, error) { |
| 123 | c.mu.Lock() |
| 124 | item, ok := c.items[key] |
| 125 | if ok { |
| 126 | if !item.IsExpired(nil) { |
| 127 | v := item.value |
| 128 | c.mu.Unlock() |
| 129 | if !onLoad { |
| 130 | c.stats.IncrHitCount() |
| 131 | } |
| 132 | return v, nil |
| 133 | } |
| 134 | c.remove(key) |
| 135 | } |
| 136 | c.mu.Unlock() |
| 137 | if !onLoad { |
| 138 | c.stats.IncrMissCount() |
| 139 | } |
| 140 | return nil, KeyNotFoundError |
| 141 | } |
| 142 | |
| 143 | func (c *SimpleCache) getWithLoader(key interface{}, isWait bool) (interface{}, error) { |
| 144 | if c.loaderExpireFunc == nil { |
| 145 | return nil, KeyNotFoundError |
| 146 | } |
| 147 | value, _, err := c.load(key, func(v interface{}, expiration *time.Duration, e error) (interface{}, error) { |
| 148 | if e != nil { |
| 149 | return nil, e |
| 150 | } |
| 151 | c.mu.Lock() |
| 152 | defer c.mu.Unlock() |
| 153 | item, err := c.set(key, v) |
| 154 | if err != nil { |
| 155 | return nil, err |
| 156 | } |
| 157 | if expiration != nil { |
| 158 | t := c.clock.Now().Add(*expiration) |
| 159 | item.(*simpleItem).expiration = &t |
| 160 | } |
| 161 | return v, nil |
| 162 | }, isWait) |
| 163 | if err != nil { |
| 164 | return nil, err |
| 165 | } |
| 166 | return value, nil |
| 167 | } |
| 168 | |
| 169 | func (c *SimpleCache) evict(count int) { |
| 170 | now := c.clock.Now() |
| 171 | current := 0 |
| 172 | for key, item := range c.items { |
| 173 | if current >= count { |
| 174 | return |
| 175 | } |
| 176 | if item.expiration == nil || now.After(*item.expiration) { |
| 177 | defer c.remove(key) |
| 178 | current++ |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // Has checks if key exists in cache |
| 184 | func (c *SimpleCache) Has(key interface{}) bool { |
| 185 | c.mu.RLock() |
| 186 | defer c.mu.RUnlock() |
| 187 | now := time.Now() |
| 188 | return c.has(key, &now) |
| 189 | } |
| 190 | |
| 191 | func (c *SimpleCache) has(key interface{}, now *time.Time) bool { |
| 192 | item, ok := c.items[key] |
| 193 | if !ok { |
| 194 | return false |
| 195 | } |
| 196 | return !item.IsExpired(now) |
| 197 | } |
| 198 | |
| 199 | // Remove removes the provided key from the cache. |
| 200 | func (c *SimpleCache) Remove(key interface{}) bool { |
| 201 | c.mu.Lock() |
| 202 | defer c.mu.Unlock() |
| 203 | |
| 204 | return c.remove(key) |
| 205 | } |
| 206 | |
| 207 | func (c *SimpleCache) remove(key interface{}) bool { |
| 208 | item, ok := c.items[key] |
| 209 | if ok { |
| 210 | delete(c.items, key) |
| 211 | if c.evictedFunc != nil { |
| 212 | c.evictedFunc(key, item.value) |
| 213 | } |
| 214 | return true |
| 215 | } |
| 216 | return false |
| 217 | } |
| 218 | |
| 219 | // Returns a slice of the keys in the cache. |
| 220 | func (c *SimpleCache) keys() []interface{} { |
| 221 | c.mu.RLock() |
| 222 | defer c.mu.RUnlock() |
| 223 | keys := make([]interface{}, len(c.items)) |
| 224 | var i = 0 |
| 225 | for k := range c.items { |
| 226 | keys[i] = k |
| 227 | i++ |
| 228 | } |
| 229 | return keys |
| 230 | } |
| 231 | |
| 232 | // GetALL returns all key-value pairs in the cache. |
| 233 | func (c *SimpleCache) GetALL(checkExpired bool) map[interface{}]interface{} { |
| 234 | c.mu.RLock() |
| 235 | defer c.mu.RUnlock() |
| 236 | items := make(map[interface{}]interface{}, len(c.items)) |
| 237 | now := time.Now() |
| 238 | for k, item := range c.items { |
| 239 | if !checkExpired || c.has(k, &now) { |
| 240 | items[k] = item.value |
| 241 | } |
| 242 | } |
| 243 | return items |
| 244 | } |
| 245 | |
| 246 | // Keys returns a slice of the keys in the cache. |
| 247 | func (c *SimpleCache) Keys(checkExpired bool) []interface{} { |
| 248 | c.mu.RLock() |
| 249 | defer c.mu.RUnlock() |
| 250 | keys := make([]interface{}, 0, len(c.items)) |
| 251 | now := time.Now() |
| 252 | for k := range c.items { |
| 253 | if !checkExpired || c.has(k, &now) { |
| 254 | keys = append(keys, k) |
| 255 | } |
| 256 | } |
| 257 | return keys |
| 258 | } |
| 259 | |
| 260 | // Len returns the number of items in the cache. |
| 261 | func (c *SimpleCache) Len(checkExpired bool) int { |
| 262 | c.mu.RLock() |
| 263 | defer c.mu.RUnlock() |
| 264 | if !checkExpired { |
| 265 | return len(c.items) |
| 266 | } |
| 267 | var length int |
| 268 | now := time.Now() |
| 269 | for k := range c.items { |
| 270 | if c.has(k, &now) { |
| 271 | length++ |
| 272 | } |
| 273 | } |
| 274 | return length |
| 275 | } |
| 276 | |
| 277 | // Completely clear the cache |
| 278 | func (c *SimpleCache) Purge() { |
| 279 | c.mu.Lock() |
| 280 | defer c.mu.Unlock() |
| 281 | |
| 282 | if c.purgeVisitorFunc != nil { |
| 283 | for key, item := range c.items { |
| 284 | c.purgeVisitorFunc(key, item.value) |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | c.init() |
| 289 | } |
| 290 | |
| 291 | type simpleItem struct { |
| 292 | clock Clock |
| 293 | value interface{} |
| 294 | expiration *time.Time |
| 295 | } |
| 296 | |
| 297 | // IsExpired returns boolean value whether this item is expired or not. |
| 298 | func (si *simpleItem) IsExpired(now *time.Time) bool { |
| 299 | if si.expiration == nil { |
| 300 | return false |
| 301 | } |
| 302 | if now == nil { |
| 303 | t := si.clock.Now() |
| 304 | now = &t |
| 305 | } |
| 306 | return si.expiration.Before(*now) |
| 307 | } |