| 1 | # GCache |
| 2 | |
| 3 |  |
| 4 | [](https://pkg.go.dev/github.com/bluele/gcache?tab=doc) |
| 5 | |
| 6 | Cache library for golang. It supports expirable Cache, LFU, LRU and ARC. |
| 7 | |
| 8 | ## Features |
| 9 | |
| 10 | * Supports expirable Cache, LFU, LRU and ARC. |
| 11 | |
| 12 | * Goroutine safe. |
| 13 | |
| 14 | * Supports event handlers which evict, purge, and add entry. (Optional) |
| 15 | |
| 16 | * Automatically load cache if it doesn't exists. (Optional) |
| 17 | |
| 18 | ## Install |
| 19 | |
| 20 | ``` |
| 21 | $ go get github.com/bluele/gcache |
| 22 | ``` |
| 23 | |
| 24 | ## Example |
| 25 | |
| 26 | ### Manually set a key-value pair. |
| 27 | |
| 28 | ```go |
| 29 | package main |
| 30 | |
| 31 | import ( |
| 32 | "github.com/bluele/gcache" |
| 33 | "fmt" |
| 34 | ) |
| 35 | |
| 36 | func main() { |
| 37 | gc := gcache.New(20). |
| 38 | LRU(). |
| 39 | Build() |
| 40 | gc.Set("key", "ok") |
| 41 | value, err := gc.Get("key") |
| 42 | if err != nil { |
| 43 | panic(err) |
| 44 | } |
| 45 | fmt.Println("Get:", value) |
| 46 | } |
| 47 | ``` |
| 48 | |
| 49 | ``` |
| 50 | Get: ok |
| 51 | ``` |
| 52 | |
| 53 | ### Manually set a key-value pair, with an expiration time. |
| 54 | |
| 55 | ```go |
| 56 | package main |
| 57 | |
| 58 | import ( |
| 59 | "github.com/bluele/gcache" |
| 60 | "fmt" |
| 61 | "time" |
| 62 | ) |
| 63 | |
| 64 | func main() { |
| 65 | gc := gcache.New(20). |
| 66 | LRU(). |
| 67 | Build() |
| 68 | gc.SetWithExpire("key", "ok", time.Second*10) |
| 69 | value, _ := gc.Get("key") |
| 70 | fmt.Println("Get:", value) |
| 71 | |
| 72 | // Wait for value to expire |
| 73 | time.Sleep(time.Second*10) |
| 74 | |
| 75 | value, err = gc.Get("key") |
| 76 | if err != nil { |
| 77 | panic(err) |
| 78 | } |
| 79 | fmt.Println("Get:", value) |
| 80 | } |
| 81 | ``` |
| 82 | |
| 83 | ``` |
| 84 | Get: ok |
| 85 | // 10 seconds later, new attempt: |
| 86 | panic: ErrKeyNotFound |
| 87 | ``` |
| 88 | |
| 89 | |
| 90 | ### Automatically load value |
| 91 | |
| 92 | ```go |
| 93 | package main |
| 94 | |
| 95 | import ( |
| 96 | "github.com/bluele/gcache" |
| 97 | "fmt" |
| 98 | ) |
| 99 | |
| 100 | func main() { |
| 101 | gc := gcache.New(20). |
| 102 | LRU(). |
| 103 | LoaderFunc(func(key interface{}) (interface{}, error) { |
| 104 | return "ok", nil |
| 105 | }). |
| 106 | Build() |
| 107 | value, err := gc.Get("key") |
| 108 | if err != nil { |
| 109 | panic(err) |
| 110 | } |
| 111 | fmt.Println("Get:", value) |
| 112 | } |
| 113 | ``` |
| 114 | |
| 115 | ``` |
| 116 | Get: ok |
| 117 | ``` |
| 118 | |
| 119 | ### Automatically load value with expiration |
| 120 | |
| 121 | ```go |
| 122 | package main |
| 123 | |
| 124 | import ( |
| 125 | "fmt" |
| 126 | "time" |
| 127 | |
| 128 | "github.com/bluele/gcache" |
| 129 | ) |
| 130 | |
| 131 | func main() { |
| 132 | var evictCounter, loaderCounter, purgeCounter int |
| 133 | gc := gcache.New(20). |
| 134 | LRU(). |
| 135 | LoaderExpireFunc(func(key interface{}) (interface{}, *time.Duration, error) { |
| 136 | loaderCounter++ |
| 137 | expire := 1 * time.Second |
| 138 | return "ok", &expire, nil |
| 139 | }). |
| 140 | EvictedFunc(func(key, value interface{}) { |
| 141 | evictCounter++ |
| 142 | fmt.Println("evicted key:", key) |
| 143 | }). |
| 144 | PurgeVisitorFunc(func(key, value interface{}) { |
| 145 | purgeCounter++ |
| 146 | fmt.Println("purged key:", key) |
| 147 | }). |
| 148 | Build() |
| 149 | value, err := gc.Get("key") |
| 150 | if err != nil { |
| 151 | panic(err) |
| 152 | } |
| 153 | fmt.Println("Get:", value) |
| 154 | time.Sleep(1 * time.Second) |
| 155 | value, err = gc.Get("key") |
| 156 | if err != nil { |
| 157 | panic(err) |
| 158 | } |
| 159 | fmt.Println("Get:", value) |
| 160 | gc.Purge() |
| 161 | if loaderCounter != evictCounter+purgeCounter { |
| 162 | panic("bad") |
| 163 | } |
| 164 | } |
| 165 | ``` |
| 166 | |
| 167 | ``` |
| 168 | Get: ok |
| 169 | evicted key: key |
| 170 | Get: ok |
| 171 | purged key: key |
| 172 | ``` |
| 173 | |
| 174 | |
| 175 | ## Cache Algorithm |
| 176 | |
| 177 | * Least-Frequently Used (LFU) |
| 178 | |
| 179 | Discards the least frequently used items first. |
| 180 | |
| 181 | ```go |
| 182 | func main() { |
| 183 | // size: 10 |
| 184 | gc := gcache.New(10). |
| 185 | LFU(). |
| 186 | Build() |
| 187 | gc.Set("key", "value") |
| 188 | } |
| 189 | ``` |
| 190 | |
| 191 | * Least Recently Used (LRU) |
| 192 | |
| 193 | Discards the least recently used items first. |
| 194 | |
| 195 | ```go |
| 196 | func main() { |
| 197 | // size: 10 |
| 198 | gc := gcache.New(10). |
| 199 | LRU(). |
| 200 | Build() |
| 201 | gc.Set("key", "value") |
| 202 | } |
| 203 | ``` |
| 204 | |
| 205 | * Adaptive Replacement Cache (ARC) |
| 206 | |
| 207 | Constantly balances between LRU and LFU, to improve the combined result. |
| 208 | |
| 209 | detail: http://en.wikipedia.org/wiki/Adaptive_replacement_cache |
| 210 | |
| 211 | ```go |
| 212 | func main() { |
| 213 | // size: 10 |
| 214 | gc := gcache.New(10). |
| 215 | ARC(). |
| 216 | Build() |
| 217 | gc.Set("key", "value") |
| 218 | } |
| 219 | ``` |
| 220 | |
| 221 | * SimpleCache (Default) |
| 222 | |
| 223 | SimpleCache has no clear priority for evict cache. It depends on key-value map order. |
| 224 | |
| 225 | ```go |
| 226 | func main() { |
| 227 | // size: 10 |
| 228 | gc := gcache.New(10).Build() |
| 229 | gc.Set("key", "value") |
| 230 | v, err := gc.Get("key") |
| 231 | if err != nil { |
| 232 | panic(err) |
| 233 | } |
| 234 | } |
| 235 | ``` |
| 236 | |
| 237 | ## Loading Cache |
| 238 | |
| 239 | If specified `LoaderFunc`, values are automatically loaded by the cache, and are stored in the cache until either evicted or manually invalidated. |
| 240 | |
| 241 | ```go |
| 242 | func main() { |
| 243 | gc := gcache.New(10). |
| 244 | LRU(). |
| 245 | LoaderFunc(func(key interface{}) (interface{}, error) { |
| 246 | return "value", nil |
| 247 | }). |
| 248 | Build() |
| 249 | v, _ := gc.Get("key") |
| 250 | // output: "value" |
| 251 | fmt.Println(v) |
| 252 | } |
| 253 | ``` |
| 254 | |
| 255 | GCache coordinates cache fills such that only one load in one process of an entire replicated set of processes populates the cache, then multiplexes the loaded value to all callers. |
| 256 | |
| 257 | ## Expirable cache |
| 258 | |
| 259 | ```go |
| 260 | func main() { |
| 261 | // LRU cache, size: 10, expiration: after a hour |
| 262 | gc := gcache.New(10). |
| 263 | LRU(). |
| 264 | Expiration(time.Hour). |
| 265 | Build() |
| 266 | } |
| 267 | ``` |
| 268 | |
| 269 | ## Event handlers |
| 270 | |
| 271 | ### Evicted handler |
| 272 | |
| 273 | Event handler for evict the entry. |
| 274 | |
| 275 | ```go |
| 276 | func main() { |
| 277 | gc := gcache.New(2). |
| 278 | EvictedFunc(func(key, value interface{}) { |
| 279 | fmt.Println("evicted key:", key) |
| 280 | }). |
| 281 | Build() |
| 282 | for i := 0; i < 3; i++ { |
| 283 | gc.Set(i, i*i) |
| 284 | } |
| 285 | } |
| 286 | ``` |
| 287 | |
| 288 | ``` |
| 289 | evicted key: 0 |
| 290 | ``` |
| 291 | |
| 292 | ### Added handler |
| 293 | |
| 294 | Event handler for add the entry. |
| 295 | |
| 296 | ```go |
| 297 | func main() { |
| 298 | gc := gcache.New(2). |
| 299 | AddedFunc(func(key, value interface{}) { |
| 300 | fmt.Println("added key:", key) |
| 301 | }). |
| 302 | Build() |
| 303 | for i := 0; i < 3; i++ { |
| 304 | gc.Set(i, i*i) |
| 305 | } |
| 306 | } |
| 307 | ``` |
| 308 | |
| 309 | ``` |
| 310 | added key: 0 |
| 311 | added key: 1 |
| 312 | added key: 2 |
| 313 | ``` |
| 314 | |
| 315 | # Author |
| 316 | |
| 317 | **Jun Kimura** |
| 318 | |
| 319 | * <http://github.com/bluele> |
| 320 | * <[email protected]> |