Skip to content

go-cache is an in-memory key-value store that is suitable for running single-machine applications.

License

Notifications You must be signed in to change notification settings

guardian360/go-cache

Repository files navigation

go-cache

go-cache is an in-memory key-value store that is suitable for running single-machine applications. It is essentially a thread-safe map[string]interface{} with expiration times. Any object can be stored for a given duration or forever and the cache can safely be used by multiple goroutines.

Installation

go-cache can be installed by using the go get command.

go get github.com/guardian360/go-cache

Usage

package main

import (
	"fmt"
	"time"

	"github.com/guardian360/go-cache"
)

func main() {
	// Create a cache with a default expiration time of 5 minutes and a
	// cleanup interval of 10 minutes.
	c := cache.New(
		cache.Expiration(5 * time.Minute),
		cache.CleanupInterval(10 * time.Minute),
	)

	// Set the value of the key "foo" to "bar" with the default expiration
	// time.
	c.Put("foo", "bar", 0)

	// Set the value of the key "baz" to 42 with no expiration time.
	c.Put("baz", 42, cache.NoExpiration)

	// Get the string associated with the key "foo" from the cache.
	foo, err := c.Get("foo")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(foo)

	// Delete a key from the cache.
	c.Delete("foo")
}

Reference

Documentation can be found using the go doc command or at pkg.go.dev.

Credits

About

go-cache is an in-memory key-value store that is suitable for running single-machine applications.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages