cache

package module
v2.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2019 License: MIT Imports: 4 Imported by: 0

README

Codacy Badge Build Status Maintainability Test Coverage Go Report Card codecov

cache

cache is an in-memory key:value store/cache 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.

Installation

go get -u github.com/efureev/cache/v2

Usage
package main
import (
	"fmt"
	"github.com/efureev/cache/v2"
	"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)
	c.Set(333, "bar", cache.DefaultExpiration)
	pnt = true
	c.Set(&pnt, "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(333, 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(bar.(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)
			// ...
	}
}

Documentation

Index

Constants

View Source
const (
	// NoExpiration - without Expiration
	NoExpiration time.Duration = -1

	// DefaultExpiration - 0
	DefaultExpiration time.Duration = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Cache struct

func New

func New(defaultExpiration, cleanupInterval time.Duration) *Cache

New create new instance

func NewFrom

func NewFrom(defaultExpiration, cleanupInterval time.Duration, items map[interface{}]Item) *Cache

NewFrom create new instance from item list

func (*Cache) Count

func (c *Cache) Count() int

Count items

func (*Cache) Delete

func (c *Cache) Delete(key interface{}) error

Delete item by key

func (*Cache) DeleteExpired

func (c *Cache) DeleteExpired()

DeleteExpired delete expired items

func (*Cache) Flush

func (c *Cache) Flush()

Flush clear items

func (*Cache) Get

func (c *Cache) Get(key interface{}) (interface{}, bool)

Get item by key

func (*Cache) Set

func (c *Cache) Set(key interface{}, value interface{}, duration time.Duration)

Set item to list by key

type Item

type Item struct {
	Value      interface{}
	Created    time.Time
	Expiration int64
}

Item struct

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL