| 1 | package gcache |
| 2 | |
| 3 | import ( |
| 4 | "container/list" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | // Constantly balances between LRU and LFU, to improve the combined result. |
| 9 | type ARC struct { |
| 10 | baseCache |
| 11 | items map[interface{}]*arcItem |
| 12 | |
| 13 | part int |
| 14 | t1 *arcList |
| 15 | t2 *arcList |
| 16 | b1 *arcList |
| 17 | b2 *arcList |
| 18 | } |
| 19 | |
| 20 | func newARC(cb *CacheBuilder) *ARC { |
| 21 | c := &ARC{} |
| 22 | buildCache(&c.baseCache, cb) |
| 23 | |
| 24 | c.init() |
| 25 | c.loadGroup.cache = c |
| 26 | return c |
| 27 | } |
| 28 | |
| 29 | func (c *ARC) init() { |
| 30 | c.items = make(map[interface{}]*arcItem) |
| 31 | c.t1 = newARCList() |
| 32 | c.t2 = newARCList() |
| 33 | c.b1 = newARCList() |
| 34 | c.b2 = newARCList() |
| 35 | } |
| 36 | |
| 37 | func (c *ARC) replace(key interface{}) { |
| 38 | if !c.isCacheFull() { |
| 39 | return |
| 40 | } |
| 41 | var old interface{} |
| 42 | if c.t1.Len() > 0 && ((c.b2.Has(key) && c.t1.Len() == c.part) || (c.t1.Len() > c.part)) { |
| 43 | old = c.t1.RemoveTail() |
| 44 | c.b1.PushFront(old) |
| 45 | } else if c.t2.Len() > 0 { |
| 46 | old = c.t2.RemoveTail() |
| 47 | c.b2.PushFront(old) |
| 48 | } else { |
| 49 | old = c.t1.RemoveTail() |
| 50 | c.b1.PushFront(old) |
| 51 | } |
| 52 | item, ok := c.items[old] |
| 53 | if ok { |
| 54 | delete(c.items, old) |
| 55 | if c.evictedFunc != nil { |
| 56 | c.evictedFunc(item.key, item.value) |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func (c *ARC) Set(key, value interface{}) error { |
| 62 | c.mu.Lock() |
| 63 | defer c.mu.Unlock() |
| 64 | _, err := c.set(key, value) |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | // Set a new key-value pair with an expiration time |
| 69 | func (c *ARC) SetWithExpire(key, value interface{}, expiration time.Duration) error { |
| 70 | c.mu.Lock() |
| 71 | defer c.mu.Unlock() |
| 72 | item, err := c.set(key, value) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | |
| 77 | t := c.clock.Now().Add(expiration) |
| 78 | item.(*arcItem).expiration = &t |
| 79 | return nil |
| 80 | } |
| 81 | |
| 82 | func (c *ARC) set(key, value interface{}) (interface{}, error) { |
| 83 | var err error |
| 84 | if c.serializeFunc != nil { |
| 85 | value, err = c.serializeFunc(key, value) |
| 86 | if err != nil { |
| 87 | return nil, err |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | item, ok := c.items[key] |
| 92 | if ok { |
| 93 | item.value = value |
| 94 | } else { |
| 95 | item = &arcItem{ |
| 96 | clock: c.clock, |
| 97 | key: key, |
| 98 | value: value, |
| 99 | } |
| 100 | c.items[key] = item |
| 101 | } |
| 102 | |
| 103 | if c.expiration != nil { |
| 104 | t := c.clock.Now().Add(*c.expiration) |
| 105 | item.expiration = &t |
| 106 | } |
| 107 | |
| 108 | defer func() { |
| 109 | if c.addedFunc != nil { |
| 110 | c.addedFunc(key, value) |
| 111 | } |
| 112 | }() |
| 113 | |
| 114 | if c.t1.Has(key) || c.t2.Has(key) { |
| 115 | return item, nil |
| 116 | } |
| 117 | |
| 118 | if elt := c.b1.Lookup(key); elt != nil { |
| 119 | c.setPart(minInt(c.size, c.part+maxInt(c.b2.Len()/c.b1.Len(), 1))) |
| 120 | c.replace(key) |
| 121 | c.b1.Remove(key, elt) |
| 122 | c.t2.PushFront(key) |
| 123 | return item, nil |
| 124 | } |
| 125 | |
| 126 | if elt := c.b2.Lookup(key); elt != nil { |
| 127 | c.setPart(maxInt(0, c.part-maxInt(c.b1.Len()/c.b2.Len(), 1))) |
| 128 | c.replace(key) |
| 129 | c.b2.Remove(key, elt) |
| 130 | c.t2.PushFront(key) |
| 131 | return item, nil |
| 132 | } |
| 133 | |
| 134 | if c.isCacheFull() && c.t1.Len()+c.b1.Len() == c.size { |
| 135 | if c.t1.Len() < c.size { |
| 136 | c.b1.RemoveTail() |
| 137 | c.replace(key) |
| 138 | } else { |
| 139 | pop := c.t1.RemoveTail() |
| 140 | item, ok := c.items[pop] |
| 141 | if ok { |
| 142 | delete(c.items, pop) |
| 143 | if c.evictedFunc != nil { |
| 144 | c.evictedFunc(item.key, item.value) |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } else { |
| 149 | total := c.t1.Len() + c.b1.Len() + c.t2.Len() + c.b2.Len() |
| 150 | if total >= c.size { |
| 151 | if total == (2 * c.size) { |
| 152 | if c.b2.Len() > 0 { |
| 153 | c.b2.RemoveTail() |
| 154 | } else { |
| 155 | c.b1.RemoveTail() |
| 156 | } |
| 157 | } |
| 158 | c.replace(key) |
| 159 | } |
| 160 | } |
| 161 | c.t1.PushFront(key) |
| 162 | return item, nil |
| 163 | } |
| 164 | |
| 165 | // Get a value from cache pool using key if it exists. If not exists and it has LoaderFunc, it will generate the value using you have specified LoaderFunc method returns value. |
| 166 | func (c *ARC) Get(key interface{}) (interface{}, error) { |
| 167 | v, err := c.get(key, false) |
| 168 | if err == KeyNotFoundError { |
| 169 | return c.getWithLoader(key, true) |
| 170 | } |
| 171 | return v, err |
| 172 | } |
| 173 | |
| 174 | // GetIFPresent gets a value from cache pool using key if it exists. |
| 175 | // If it dose not exists key, returns KeyNotFoundError. |
| 176 | // And send a request which refresh value for specified key if cache object has LoaderFunc. |
| 177 | func (c *ARC) GetIFPresent(key interface{}) (interface{}, error) { |
| 178 | v, err := c.get(key, false) |
| 179 | if err == KeyNotFoundError { |
| 180 | return c.getWithLoader(key, false) |
| 181 | } |
| 182 | return v, err |
| 183 | } |
| 184 | |
| 185 | func (c *ARC) get(key interface{}, onLoad bool) (interface{}, error) { |
| 186 | v, err := c.getValue(key, onLoad) |
| 187 | if err != nil { |
| 188 | return nil, err |
| 189 | } |
| 190 | if c.deserializeFunc != nil { |
| 191 | return c.deserializeFunc(key, v) |
| 192 | } |
| 193 | return v, nil |
| 194 | } |
| 195 | |
| 196 | func (c *ARC) getValue(key interface{}, onLoad bool) (interface{}, error) { |
| 197 | c.mu.Lock() |
| 198 | defer c.mu.Unlock() |
| 199 | if elt := c.t1.Lookup(key); elt != nil { |
| 200 | c.t1.Remove(key, elt) |
| 201 | item := c.items[key] |
| 202 | if !item.IsExpired(nil) { |
| 203 | c.t2.PushFront(key) |
| 204 | if !onLoad { |
| 205 | c.stats.IncrHitCount() |
| 206 | } |
| 207 | return item.value, nil |
| 208 | } else { |
| 209 | delete(c.items, key) |
| 210 | c.b1.PushFront(key) |
| 211 | if c.evictedFunc != nil { |
| 212 | c.evictedFunc(item.key, item.value) |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | if elt := c.t2.Lookup(key); elt != nil { |
| 217 | item := c.items[key] |
| 218 | if !item.IsExpired(nil) { |
| 219 | c.t2.MoveToFront(elt) |
| 220 | if !onLoad { |
| 221 | c.stats.IncrHitCount() |
| 222 | } |
| 223 | return item.value, nil |
| 224 | } else { |
| 225 | delete(c.items, key) |
| 226 | c.t2.Remove(key, elt) |
| 227 | c.b2.PushFront(key) |
| 228 | if c.evictedFunc != nil { |
| 229 | c.evictedFunc(item.key, item.value) |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | if !onLoad { |
| 235 | c.stats.IncrMissCount() |
| 236 | } |
| 237 | return nil, KeyNotFoundError |
| 238 | } |
| 239 | |
| 240 | func (c *ARC) getWithLoader(key interface{}, isWait bool) (interface{}, error) { |
| 241 | if c.loaderExpireFunc == nil { |
| 242 | return nil, KeyNotFoundError |
| 243 | } |
| 244 | value, _, err := c.load(key, func(v interface{}, expiration *time.Duration, e error) (interface{}, error) { |
| 245 | if e != nil { |
| 246 | return nil, e |
| 247 | } |
| 248 | c.mu.Lock() |
| 249 | defer c.mu.Unlock() |
| 250 | item, err := c.set(key, v) |
| 251 | if err != nil { |
| 252 | return nil, err |
| 253 | } |
| 254 | if expiration != nil { |
| 255 | t := c.clock.Now().Add(*expiration) |
| 256 | item.(*arcItem).expiration = &t |
| 257 | } |
| 258 | return v, nil |
| 259 | }, isWait) |
| 260 | if err != nil { |
| 261 | return nil, err |
| 262 | } |
| 263 | return value, nil |
| 264 | } |
| 265 | |
| 266 | // Has checks if key exists in cache |
| 267 | func (c *ARC) Has(key interface{}) bool { |
| 268 | c.mu.RLock() |
| 269 | defer c.mu.RUnlock() |
| 270 | now := time.Now() |
| 271 | return c.has(key, &now) |
| 272 | } |
| 273 | |
| 274 | func (c *ARC) has(key interface{}, now *time.Time) bool { |
| 275 | item, ok := c.items[key] |
| 276 | if !ok { |
| 277 | return false |
| 278 | } |
| 279 | return !item.IsExpired(now) |
| 280 | } |
| 281 | |
| 282 | // Remove removes the provided key from the cache. |
| 283 | func (c *ARC) Remove(key interface{}) bool { |
| 284 | c.mu.Lock() |
| 285 | defer c.mu.Unlock() |
| 286 | |
| 287 | return c.remove(key) |
| 288 | } |
| 289 | |
| 290 | func (c *ARC) remove(key interface{}) bool { |
| 291 | if elt := c.t1.Lookup(key); elt != nil { |
| 292 | c.t1.Remove(key, elt) |
| 293 | item := c.items[key] |
| 294 | delete(c.items, key) |
| 295 | c.b1.PushFront(key) |
| 296 | if c.evictedFunc != nil { |
| 297 | c.evictedFunc(key, item.value) |
| 298 | } |
| 299 | return true |
| 300 | } |
| 301 | |
| 302 | if elt := c.t2.Lookup(key); elt != nil { |
| 303 | c.t2.Remove(key, elt) |
| 304 | item := c.items[key] |
| 305 | delete(c.items, key) |
| 306 | c.b2.PushFront(key) |
| 307 | if c.evictedFunc != nil { |
| 308 | c.evictedFunc(key, item.value) |
| 309 | } |
| 310 | return true |
| 311 | } |
| 312 | |
| 313 | return false |
| 314 | } |
| 315 | |
| 316 | // GetALL returns all key-value pairs in the cache. |
| 317 | func (c *ARC) GetALL(checkExpired bool) map[interface{}]interface{} { |
| 318 | c.mu.RLock() |
| 319 | defer c.mu.RUnlock() |
| 320 | items := make(map[interface{}]interface{}, len(c.items)) |
| 321 | now := time.Now() |
| 322 | for k, item := range c.items { |
| 323 | if !checkExpired || c.has(k, &now) { |
| 324 | items[k] = item.value |
| 325 | } |
| 326 | } |
| 327 | return items |
| 328 | } |
| 329 | |
| 330 | // Keys returns a slice of the keys in the cache. |
| 331 | func (c *ARC) Keys(checkExpired bool) []interface{} { |
| 332 | c.mu.RLock() |
| 333 | defer c.mu.RUnlock() |
| 334 | keys := make([]interface{}, 0, len(c.items)) |
| 335 | now := time.Now() |
| 336 | for k := range c.items { |
| 337 | if !checkExpired || c.has(k, &now) { |
| 338 | keys = append(keys, k) |
| 339 | } |
| 340 | } |
| 341 | return keys |
| 342 | } |
| 343 | |
| 344 | // Len returns the number of items in the cache. |
| 345 | func (c *ARC) Len(checkExpired bool) int { |
| 346 | c.mu.RLock() |
| 347 | defer c.mu.RUnlock() |
| 348 | if !checkExpired { |
| 349 | return len(c.items) |
| 350 | } |
| 351 | var length int |
| 352 | now := time.Now() |
| 353 | for k := range c.items { |
| 354 | if c.has(k, &now) { |
| 355 | length++ |
| 356 | } |
| 357 | } |
| 358 | return length |
| 359 | } |
| 360 | |
| 361 | // Purge is used to completely clear the cache |
| 362 | func (c *ARC) Purge() { |
| 363 | c.mu.Lock() |
| 364 | defer c.mu.Unlock() |
| 365 | |
| 366 | if c.purgeVisitorFunc != nil { |
| 367 | for _, item := range c.items { |
| 368 | c.purgeVisitorFunc(item.key, item.value) |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | c.init() |
| 373 | } |
| 374 | |
| 375 | func (c *ARC) setPart(p int) { |
| 376 | if c.isCacheFull() { |
| 377 | c.part = p |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | func (c *ARC) isCacheFull() bool { |
| 382 | return (c.t1.Len() + c.t2.Len()) == c.size |
| 383 | } |
| 384 | |
| 385 | // IsExpired returns boolean value whether this item is expired or not. |
| 386 | func (it *arcItem) IsExpired(now *time.Time) bool { |
| 387 | if it.expiration == nil { |
| 388 | return false |
| 389 | } |
| 390 | if now == nil { |
| 391 | t := it.clock.Now() |
| 392 | now = &t |
| 393 | } |
| 394 | return it.expiration.Before(*now) |
| 395 | } |
| 396 | |
| 397 | type arcList struct { |
| 398 | l *list.List |
| 399 | keys map[interface{}]*list.Element |
| 400 | } |
| 401 | |
| 402 | type arcItem struct { |
| 403 | clock Clock |
| 404 | key interface{} |
| 405 | value interface{} |
| 406 | expiration *time.Time |
| 407 | } |
| 408 | |
| 409 | func newARCList() *arcList { |
| 410 | return &arcList{ |
| 411 | l: list.New(), |
| 412 | keys: make(map[interface{}]*list.Element), |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | func (al *arcList) Has(key interface{}) bool { |
| 417 | _, ok := al.keys[key] |
| 418 | return ok |
| 419 | } |
| 420 | |
| 421 | func (al *arcList) Lookup(key interface{}) *list.Element { |
| 422 | elt := al.keys[key] |
| 423 | return elt |
| 424 | } |
| 425 | |
| 426 | func (al *arcList) MoveToFront(elt *list.Element) { |
| 427 | al.l.MoveToFront(elt) |
| 428 | } |
| 429 | |
| 430 | func (al *arcList) PushFront(key interface{}) { |
| 431 | if elt, ok := al.keys[key]; ok { |
| 432 | al.l.MoveToFront(elt) |
| 433 | return |
| 434 | } |
| 435 | elt := al.l.PushFront(key) |
| 436 | al.keys[key] = elt |
| 437 | } |
| 438 | |
| 439 | func (al *arcList) Remove(key interface{}, elt *list.Element) { |
| 440 | delete(al.keys, key) |
| 441 | al.l.Remove(elt) |
| 442 | } |
| 443 | |
| 444 | func (al *arcList) RemoveTail() interface{} { |
| 445 | elt := al.l.Back() |
| 446 | al.l.Remove(elt) |
| 447 | |
| 448 | key := elt.Value |
| 449 | delete(al.keys, key) |
| 450 | |
| 451 | return key |
| 452 | } |
| 453 | |
| 454 | func (al *arcList) Len() int { |
| 455 | return al.l.Len() |
| 456 | } |