TTLCache is a simple key/value cache in golang with the following functions:
- Thread-safe
- Individual expiring time or global expiring time, you can choose
- Auto-Extending expiration on
Get
-or- DNS style TTL, seeSkipTtlExtensionOnHit(bool)
- Fast and memory efficient
- Can trigger callback on key expiration
- Cleanup resources by calling
Close()
at end of lifecycle.
import (
"time"
"fmt"
"github.com/ReneKroon/ttlcache"
)
func main () {
newItemCallback := func(key string, value interface{}) {
fmt.Printf("New key(%s) added\n", key)
}
checkExpirationCallback := func(key string, value interface{}) bool {
if key == "key1" {
// if the key equals "key1", the value
// will not be allowed to expire
return false
}
// all other values are allowed to expire
return true
}
expirationCallback := func(key string, value interface{}) {
fmt.Printf("This key(%s) has expired\n", key)
}
cache := ttlcache.NewCache()
defer cache.Close()
cache.SetTTL(time.Duration(10 * time.Second))
cache.SetExpirationCallback(expirationCallback)
cache.Set("key", "value")
cache.SetWithTTL("keyWithTTL", "value", 10 * time.Second)
value, exists := cache.Get("key")
count := cache.Count()
result := cache.Remove("key")
}
- The complexity of the current cache is already quite high. Therefore i will not add 'convenience' features like an interface to supply a function to get missing keys.
- The locking should be done only in the functions of the Cache struct. Else data races can occur or recursive locks are needed, which are both unwanted.
- I prefer correct functionality over fast tests. It's ok for new tests to take seconds to proof something.
TTLCache was forked from wunderlist/ttlcache to add extra functions not avaiable in the original scope. The main differences are:
- A item can store any kind of object, previously, only strings could be saved
- Optionally, you can add callbacks to: check if a value should expire, be notified if a value expires, and be notified when new values are added to the cache
- The expiration can be either global or per item
- Can exist items without expiration time
- Expirations and callbacks are realtime. Don't have a pooling time to check anymore, now it's done with a heap.