• Stars
    star
    7,433
  • Rank 5,202 (Top 0.2 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 13 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

More Repositories

1

cpuburn

Utilize 100% CPU on all available cores from the command line.
Go
119
star
2

sortutil

Nested, case-insensitive, and reverse sorting for Go
Go
71
star
3

osg

Universal sitemap generator
Go
60
star
4

ocp

Smart cache preloader for websites with XML sitemaps
Go
56
star
5

go-bloom

Boolean, counting and layered bloom filters for Go
Go
22
star
6

one.sh

Singleton command execution for Linux/BSD
Shell
18
star
7

go-hmaccrypt

Very strong password digests for Go
Go
18
star
8

go-bitset

Efficient map[uint32|64]bool-like bitset for Go
Go
13
star
9

define

An always up-to-date command-line dictionary that supports over 50 languages
Go
13
star
10

go-wikimedia

A Go interface to the Wikimedia (Wikipedia, Wiktionary, etc.) API
Go
10
star
11

picugen

A general-purpose hash/checksum generator
Go
10
star
12

emailsfromfile

Extract all email addresses from a file
Python
8
star
13

minilyzer

Makes postmortem analysis of Windows Minidumps easier
6
star
14

scrutinize

Misconfiguration scanner
Python
5
star
15

sniffy

HTTP/Mail proxy/gateway/load balancer with security testing features like request interception (even for SSL) and reshaping (e.g. hiding clients' user-agents)
JavaScript
5
star
16

adventofcode-haskell

Advent of Code (http://adventofcode.com/) solutions in Haskell
Haskell
4
star
17

adventofkoka

Advent of Code solutions in Koka
3
star
18

urlreveal

Reveals the page behind short URL and redirects. (Google App Engine application.)
Python
3
star
19

euler-go

Some Project Euler solutions in Go
Go
2
star
20

senka

Secure, portable tunneling/proxy server
Python
2
star
21

haveabit

Inspiring, controversial, and humorous โ€œbitsโ€ (quotes/images/video) by great people. (Google App Engine application.)
Python
2
star
22

getter

User-friendly retrieval and extraction of archives using curl
Shell
2
star
23

euler-haskell

Some Project Euler solutions in Haskell
Haskell
2
star
24

propscale

Get altered image dimensions whilst maintaining the original aspect ratio
Python
1
star
25

euler-python

Some Project Euler solutions in Python
Python
1
star
26

piculet

A not-particularly-fast cryptographic hash auditor
Go
1
star
27

fsmsi

Enables the Windows Installer service in fail safe mode
1
star
28

teaman

Simple console app that alerts you (with sound) when your cup of tea is ready
Python
1
star
29

filesbyweek

Counts the number of files in a folder created in a certain week
Shell
1
star