All Projects → patrickmn → Go Cache

patrickmn / Go Cache

Licence: mit
An in-memory key:value store/cache (similar to Memcached) library for Go, suitable for single-machine applications.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Go Cache

Libcache
A Lightweight in-memory key:value cache library for Go.
Stars: ✭ 152 (-97.32%)
Mutual labels:  cache, library
Python Common Cache
This project is a cache component based on the memory and it is lightweight, simple and customizable. 🐍 😃
Stars: ✭ 21 (-99.63%)
Mutual labels:  cache, library
Cacache Rs
💩💵 but for your 🦀
Stars: ✭ 116 (-97.96%)
Mutual labels:  cache, library
Holster
A place to keep useful golang functions and small libraries
Stars: ✭ 166 (-97.08%)
Mutual labels:  cache, library
Golib
Go Library [DEPRECATED]
Stars: ✭ 194 (-96.58%)
Mutual labels:  cache, library
Torchdata
PyTorch dataset extended with map, cache etc. (tensorflow.data like)
Stars: ✭ 226 (-96.02%)
Mutual labels:  cache, library
Pottery
Redis for humans. 🌎🌍🌏
Stars: ✭ 204 (-96.41%)
Mutual labels:  cache, library
Ristretto
A high performance memory-bound Go cache
Stars: ✭ 3,584 (-36.86%)
Mutual labels:  cache, library
School Management System
Another School Management System
Stars: ✭ 520 (-90.84%)
Mutual labels:  library
Pyptt
支援 PTT 還有 PTT2 的 PTT API
Stars: ✭ 527 (-90.72%)
Mutual labels:  library
Telethon
Pure Python 3 MTProto API Telegram client library, for bots too!
Stars: ✭ 5,805 (+2.27%)
Mutual labels:  library
Accord
Accord: A sane validation library for Scala
Stars: ✭ 519 (-90.86%)
Mutual labels:  library
Nve
Run any command on specific Node.js versions
Stars: ✭ 531 (-90.64%)
Mutual labels:  library
Pymunk
Pymunk is a easy-to-use pythonic 2d physics library that can be used whenever you need 2d rigid body physics from Python
Stars: ✭ 513 (-90.96%)
Mutual labels:  library
Jikan
Unofficial MyAnimeList PHP+REST API which provides functions other than the official API
Stars: ✭ 531 (-90.64%)
Mutual labels:  library
Discord.io
A small, single-file library for creating DiscordApp clients from Node.js or the browser
Stars: ✭ 511 (-91%)
Mutual labels:  library
Iconfontcppheaders
C, C++ headers and C# classes for icon fonts: Font Awesome, Fork Awesome, Material Design, Kenney game icons and Fontaudio
Stars: ✭ 509 (-91.03%)
Mutual labels:  library
Python Holidays
Generate and work with holidays in Python
Stars: ✭ 534 (-90.59%)
Mutual labels:  library
Laravel Eloquent Query Cache
Adding cache on your Laravel Eloquent queries' results is now a breeze.
Stars: ✭ 529 (-90.68%)
Mutual labels:  cache
Bigcache
Efficient cache for gigabytes of data written in Go.
Stars: ✭ 5,304 (-6.55%)
Mutual labels:  cache

go-cache

go-cache is an in-memory key:value store/cache similar to memcached that is suitable for applications running on a single machine. Its major advantage is that, being essentially a thread-safe map[string]interface{} with expiration times, it doesn't need to serialize or transmit its contents over the network.

Any object can be stored, for a given duration or forever, and the cache can be safely used by multiple goroutines.

Although go-cache isn't meant to be used as a persistent datastore, the entire cache can be saved to and loaded from a file (using c.Items() to retrieve the items map to serialize, and NewFrom() to create a cache from a deserialized one) to recover from downtime quickly. (See the docs for NewFrom() for caveats.)

Installation

go get github.com/patrickmn/go-cache

Usage

import (
	"fmt"
	"github.com/patrickmn/go-cache"
	"time"
)

func main() {
	// Create a cache with a default expiration time of 5 minutes, and which
	// purges expired items every 10 minutes
	c := cache.New(5*time.Minute, 10*time.Minute)

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

	// Set the value of the key "baz" to 42, with no expiration time
	// (the item won't be removed until it is re-set, or removed using
	// c.Delete("baz")
	c.Set("baz", 42, cache.NoExpiration)

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

	// Since Go is statically typed, and cache values can be anything, type
	// assertion is needed when values are being passed to functions that don't
	// take arbitrary types, (i.e. interface{}). The simplest way to do this for
	// values which will only be used once--e.g. for passing to another
	// function--is:
	foo, found := c.Get("foo")
	if found {
		MyFunction(foo.(string))
	}

	// This gets tedious if the value is used several times in the same function.
	// You might do either of the following instead:
	if x, found := c.Get("foo"); found {
		foo := x.(string)
		// ...
	}
	// or
	var foo string
	if x, found := c.Get("foo"); found {
		foo = x.(string)
	}
	// ...
	// foo can then be passed around freely as a string

	// Want performance? Store pointers!
	c.Set("foo", &MyStruct, cache.DefaultExpiration)
	if x, found := c.Get("foo"); found {
		foo := x.(*MyStruct)
			// ...
	}
}

Reference

godoc or http://godoc.org/github.com/patrickmn/go-cache

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].