Thread-safe in-memory cache for go
Go
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
LICENSE
README.md
cache.go
cache_test.go
options.go

README.md

Go Cache

MIT License Go Report Card Go Doc

Overview

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.

Example

package main

import (
        "fmt"
	"time"
        "github.com/zpatrick/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()
}

License

This work is published under the MIT license. Please see the LICENSE file for details.