A lightweight go package in-memory caching with a queued (FIFO) timeout. Every entry added to the cache is also "queued" and at a set interval an entry is popped from the queue.
When an entry is timed out (popped out of the queue), it can be collected in a read-only channel.
go get github.com/wlwanpan/delay-cache
- Basic example
dcache = dcache.New(5 * time.Second)
err := dcache.Set("key", "val")
...
val, err := dcache.Get("key")
...
has := dcache.Has("key")
...
- Collecting delayed entry
dcache = dcache.New(5 * time.Second)
dcache.Set("key1", "val1")
dcache.Set("key2", "val2")
dcache.Set("key3", "val3")
dcache.StartCycle()
for {
select {
case entry := <- dcache.Collect():
// 1st select to be collected: &Collect{key: "key1", val: "val1"}
// 2nd select after 5 seconds: &Collect{key: "key2", val: "val3"}
// ...
}
}