Go Cache is a simple package to provide thread-safe in-memory caching in Go. It's my attempt to practice some of the patterns/philosophies found in these articles:
The code is tested, although standard caveats of using interface{}
apply.
Personally, I'd recommend copying this package and replacing var T interface{}
with whatever type you need to cache.
I may add code generation in the future to make that process easier.
package main
import (
"fmt"
"time"
"github.com/ridgelines/go-cache"
)
func main() {
c := cache.New()
// empty the cache every hour
c.ClearEvery(time.Hour)
// add some items
c.Set("key1", 1)
c.Set("key2", 2)
// add some items that will expire after 5 minutes
c.Set("key3", 3, cache.Expire(time.Minute*5))
c.Set("key4", 4, cache.Expire(time.Minute*5))
fmt.Println(c.Get("key1"))
fmt.Println(c.Get("key2"))
for _, key := range c.Keys() {
fmt.Println(key)
}
for key, val := range c.Items() {
fmt.Printf("%s: %v", key, val)
}
c.Delete("key1")
if val, ok := c.GetOK("key2"); ok {
fmt.Println(val)
}
c.Clear()
}
This work is published under the MIT license.
Please see the LICENSE
file for details.