[go: up one dir, main page]

File: TTL.md

package info (click to toggle)
core-cache-clojure 0.6.5-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 240 kB
  • ctags: 6
  • sloc: xml: 89; makefile: 32
file content (59 lines) | stat: -rw-r--r-- 1,994 bytes parent folder | download
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
TTL cache
==========

The time-to-live cache is one that evicts items that are older than a time-to-live threshold (in milliseconds).

*Before reading this page please make sure that you've read and understand the [Basic Usage patterns](./Using.md).*

General usage
-------------

To create a core.cache `TTLCache` instance you should *always* use its associated constructor function `ttl-cache-factory` with an optional `:ttl` parameter:

```clojure
    (ns your.lib 
      (:require [clojure.core.cache :as cache]))
	
    (def C (cache/ttl-cache-factory {} :ttl 1000))
    
    (-> C (assoc :a 1) (assoc :b 2))
	;=> {:a 1, :b 2}
```

*note: the default `:ttl` value is 2 seconds*

At this point the cache is fresh and younger than one second (that is, depending on how fast you read), but if you execute yet another call the story will change:

```clojure
    (def sleepy #(do (Thread/sleep %2) %))
    
    (-> C (assoc :a 1) 
	      (assoc :b 2)
		  (sleepy 1500)
	      (assoc :c 3))
	;=> {:c 3}
```

At this point the operation of the TTL cache is exposed.  As you see, sleeping in between adding the keys `:a` and `:b` causes them to be evicted on the next insertion.

All caveats apply regarding the [proper usage patterns](./Using.md).

TTL cache use cases
--------------------

The TTL cache eviction policy is very simple to understand and works well for certain scenarios.  However, because the policy requires "age" information, the implementation is somewhat subtle and moderately memory intensive.

### Advantages to using an TTL cache

There are a few reasons why you might want to use a TTL cache:

 * Its logic is easy to understand
 * It's reasonably fast
 * It works well with data subject to temporal concerns

### Disadvantages to using an TTL cache

 * It requires more historical data to operate
 * Cache size does not generally help its efficiency
 
As always, you should measure your system's characteristics to determine the best eviction strategy for your purposes.