| 1 | package gocache |
| 2 | |
| 3 | // EvictionPolicy is what dictates how evictions are handled |
| 4 | type EvictionPolicy string |
| 5 | |
| 6 | const ( |
| 7 | // LeastRecentlyUsed is an eviction policy that causes the most recently accessed cache entry to be moved to the |
| 8 | // head of the cache. Effectively, this causes the cache entries that have not been accessed for some time to |
| 9 | // gradually move closer and closer to the tail, and since the tail is the entry that gets deleted when an eviction |
| 10 | // is required, it allows less used cache entries to be evicted while keeping recently accessed entries at or close |
| 11 | // to the head. |
| 12 | // |
| 13 | // For instance, creating a Cache with a Cache.MaxSize of 3 and creating the entries 1, 2 and 3 in that order would |
| 14 | // put 3 at the head and 1 at the tail: |
| 15 | // 3 (head) -> 2 -> 1 (tail) |
| 16 | // If the cache entry 1 was then accessed, 1 would become the head and 2 the tail: |
| 17 | // 1 (head) -> 3 -> 2 (tail) |
| 18 | // If a cache entry 4 was then created, because the Cache.MaxSize is 3, the tail (2) would then be evicted: |
| 19 | // 4 (head) -> 1 -> 3 (tail) |
| 20 | LeastRecentlyUsed EvictionPolicy = "LeastRecentlyUsed" |
| 21 | |
| 22 | // FirstInFirstOut is an eviction policy that causes cache entries to be evicted in the same order that they are |
| 23 | // created. |
| 24 | // |
| 25 | // For instance, creating a Cache with a Cache.MaxSize of 3 and creating the entries 1, 2 and 3 in that order would |
| 26 | // put 3 at the head and 1 at the tail: |
| 27 | // 3 (head) -> 2 -> 1 (tail) |
| 28 | // If the cache entry 1 was then accessed, unlike with LeastRecentlyUsed, nothing would change: |
| 29 | // 3 (head) -> 2 -> 1 (tail) |
| 30 | // If a cache entry 4 was then created, because the Cache.MaxSize is 3, the tail (1) would then be evicted: |
| 31 | // 4 (head) -> 3 -> 2 (tail) |
| 32 | FirstInFirstOut EvictionPolicy = "FirstInFirstOut" |
| 33 | ) |