cache

package module
v0.0.0-...-4a62e97 Latest Latest
Warning

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

Go to latest
Published: May 9, 2023 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

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.

Installation

go get github.com/wyyadd/go-cache

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

func main() {
	// Create a cache with a default expiration time of 5 minutes, and which
	// purges expired items every 10 minutes
	// This project offer three different maps and one LRU Cache to store key:value; (See BenchMark)
	c := cache.New(5*time.Minute, 10*time.Minute, NewRwmMap())
	c := cache.New(5*time.Minute, 10*time.Minute, NewSyncMap())
	c := cache.New(5*time.Minute, 10*time.Minute, NewConcurrentMap())

	// 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)
			// ...
	}
}
BenchMark
goos: windows
goarch: amd64
pkg: github.com/wyyadd/go-cache
cpu: AMD Ryzen 5 5600 6-Core Processor
BenchmarkGetExpiring_RwmMap-12                                  97032516                11.90 ns/op            0 B/op         0 allocs/op
BenchmarkGetExpiring_SyncMap-12                                 47984644                23.69 ns/op            0 B/op          0 allocs/op
BenchmarkGetExpiring_ConcurrentMap-12                           82739790                14.66 ns/op            0 B/op          0 allocs/op
BenchmarkGetNotExpiring_RwmMap-12                               155887176               11.24 ns/op            0 B/op          0 allocs/op
BenchmarkGetNotExpiring_SyncMap-12                              59958028                19.88 ns/op            0 B/op          0 allocs/op
BenchmarkGetNotExpiring_ConcurrentMap-12                        100000000               10.28 ns/op            0 B/op          0 allocs/op
BenchmarkGet_RWMutex-12                                         292061973                4.112 ns/op           0 B/op          0 allocs/op
BenchmarkGetConcurrentExpiring_RwmMap-12                        45670094                26.23 ns/op            0 B/op          0 allocs/op
BenchmarkGetConcurrentExpiring_SyncMap-12                       298293536                4.008 ns/op           0 B/op          0 allocs/op
BenchmarkGetConcurrentExpiring_ConcurrentMap-12                 46130602                25.54 ns/op            0 B/op          0 allocs/op
BenchmarkGetConcurrentNotExpiring_RwmMap-12                     35811273                35.54 ns/op            0 B/op          0 allocs/op
BenchmarkGetConcurrentNotExpiring_SyncMap-12                    374984414                3.167 ns/op           0 B/op          0 allocs/op
BenchmarkGetConcurrentNotExpiring_ConcurrentMap-12              47858529                24.87 ns/op            0 B/op          0 allocs/op
BenchmarkGetConcurrent_RWMutex-12                               47170923                24.97 ns/op            0 B/op          0 allocs/op
BenchmarkGetManyConcurrentExpiring_RwmMap-12                    100000000               21.88 ns/op            0 B/op          0 allocs/op
BenchmarkGetManyConcurrentExpiring_SyncMap-12                   290337565                4.174 ns/op           0 B/op          0 allocs/op
BenchmarkGetManyConcurrentExpiring_ConcurrentMap-12             246717678                4.958 ns/op           0 B/op          0 allocs/op
BenchmarkGetManyConcurrentNotExpiring_RwmMap-12                 36202042                36.76 ns/op            0 B/op          0 allocs/op
BenchmarkGetManyConcurrentNotExpiring_SyncMap-12                356510050                3.398 ns/op           0 B/op          0 allocs/op
BenchmarkGetManyConcurrentNotExpiring_ConcurrentMap-12          270097808                4.345 ns/op           0 B/op          0 allocs/op
BenchmarkSetExpiring_RwmMap-12                                  23313872                51.17 ns/op           24 B/op          1 allocs/op
BenchmarkSetExpiring_SyncMap-12                                 10110098               115.9 ns/op            56 B/op          3 allocs/op
BenchmarkSetExpiring_ConcurrentMap-12                           21408118                55.15 ns/op           24 B/op          1 allocs/op
BenchmarkSetNotExpiring_RwmMap-12                               29246250                45.86 ns/op           24 B/op          1 allocs/op
BenchmarkSetNotExpiring_SyncMap-12                              11419250               108.4 ns/op            56 B/op          3 allocs/op
BenchmarkSetNotExpiring_ConcurrentMap-12                        26648071                46.37 ns/op           24 B/op          1 allocs/op
BenchmarkSet_RWMutex-12                                         88821778                14.10 ns/op            0 B/op          0 allocs/op
BenchmarkSetDelete_RwmMap-12                                    18248229                82.52 ns/op           24 B/op          1 allocs/op
BenchmarkSetDelete_SyncMap-12                                    2575394               442.7 ns/op           368 B/op          9 allocs/op
BenchmarkSetDelete_ConcurrentMap-12                             16538492                69.65 ns/op           24 B/op          1 allocs/op
BenchmarkSetDelete_RWMutex-12                                   42067917                28.70 ns/op            0 B/op          0 allocs/op
BenchmarkDeleteExpiredLoop_RwmMap-12                                1201           1130260 ns/op              24 B/op          1 allocs/op
BenchmarkDeleteExpiredLoop_SyncMap-12                                576           1933035 ns/op              40 B/op          2 allocs/op
BenchmarkDeleteExpiredLoop_ConcurrentMap-12                           91          14652297 ns/op         6617957 B/op        201 allocs/op
PASS
ok      github.com/wyyadd/go-cache      50.179s

Documentation

Index

Constants

View Source
const (
	// For use with functions that take an expiration time.
	NoExpiration time.Duration = -1
	// For use with functions that take an expiration time. Equivalent to
	// passing in the same expiration duration as was given to New() or
	// NewFrom() when the cache was created (e.g. 5 minutes.)
	DefaultExpiration time.Duration = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

func New

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

Return a new cache with a given default expiration duration and cleanup interval. If the expiration duration is less than one (or NoExpiration), the items in the cache never expire (by default), and must be deleted manually. If the cleanup interval is less than one, expired items are not deleted from the cache before calling c.DeleteExpired().

func (Cache) Delete

func (c Cache) Delete(k string)

Delete an item from the cache. Does nothing if the key is not in the cache.

func (Cache) DeleteExpired

func (c Cache) DeleteExpired()

Delete all expired items from the cache.

func (Cache) Flush

func (c Cache) Flush()

Delete all items from the cache.

func (Cache) Get

func (c Cache) Get(k string) (interface{}, bool)

Get an item from the cache. Returns the item or nil, and a bool indicating whether the key was found.

func (Cache) GetWithExpiration

func (c Cache) GetWithExpiration(k string) (interface{}, time.Time, bool)

GetWithExpiration returns an item and its expiration time from the cache. It returns the item or nil, the expiration time if one is set (if the item never expires a zero value for time.Time is returned), and a bool indicating whether the key was found.

func (Cache) ItemCount

func (c Cache) ItemCount() int

Returns the number of items in the cache. This may include items that have expired, but have not yet been cleaned up.

func (Cache) Items

func (c Cache) Items() map[string]Item

Copies all unexpired items in the cache into a new map and returns it.

func (Cache) Set

func (c Cache) Set(k string, x interface{}, d time.Duration)

Add an item to the cache, replacing any existing item. If the duration is 0 (DefaultExpiration), the cache's default expiration time is used. If it is -1 (NoExpiration), the item never expires.

func (Cache) SetDefault

func (c Cache) SetDefault(k string, x interface{})

Add an item to the cache, replacing any existing item, using the default expiration.

type CacheItem

type CacheItem struct {
	// contains filtered or unexported fields
}

type CacheMap

type CacheMap interface {
	Get(k string) (interface{}, bool)
	Set(k string, x interface{})
	Delete(k string)
	Range(f func(k string, v any))
	Count() int
	Flush()
}

func NewConcurrentMap

func NewConcurrentMap() CacheMap

func NewRwmMap

func NewRwmMap() CacheMap

func NewSyncMap

func NewSyncMap() CacheMap

type ConcurrentMap

type ConcurrentMap struct {
	// contains filtered or unexported fields
}

func (*ConcurrentMap) Count

func (m *ConcurrentMap) Count() int

func (*ConcurrentMap) Delete

func (m *ConcurrentMap) Delete(k string)

func (*ConcurrentMap) Flush

func (m *ConcurrentMap) Flush()

func (*ConcurrentMap) Get

func (m *ConcurrentMap) Get(k string) (interface{}, bool)

func (*ConcurrentMap) Range

func (m *ConcurrentMap) Range(f func(k string, v any))

func (*ConcurrentMap) Set

func (m *ConcurrentMap) Set(k string, x interface{})

type Item

type Item struct {
	Object     interface{}
	Expiration int64
}

func (*Item) Expired

func (item *Item) Expired() bool

Returns true if the item has expired.

type LRUCache

type LRUCache struct {
	// contains filtered or unexported fields
}

func NewLRUCache

func NewLRUCache(maxItems int, expireTime time.Duration, cleanTime time.Duration) *LRUCache

func (*LRUCache) Delete

func (c *LRUCache) Delete(key string)

func (*LRUCache) Get

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

func (*LRUCache) Set

func (c *LRUCache) Set(key string, value interface{})

type RwmMap

type RwmMap struct {
	// contains filtered or unexported fields
}

func (*RwmMap) Count

func (m *RwmMap) Count() int

func (*RwmMap) Delete

func (m *RwmMap) Delete(k string)

func (*RwmMap) Flush

func (m *RwmMap) Flush()

func (*RwmMap) Get

func (m *RwmMap) Get(k string) (interface{}, bool)

func (*RwmMap) Range

func (m *RwmMap) Range(f func(k string, v any))

func (*RwmMap) Set

func (m *RwmMap) Set(k string, x interface{})

type SyncMap

type SyncMap struct {
	// contains filtered or unexported fields
}

func (*SyncMap) Count

func (m *SyncMap) Count() int

func (*SyncMap) Delete

func (m *SyncMap) Delete(k string)

func (*SyncMap) Flush

func (m *SyncMap) Flush()

func (*SyncMap) Get

func (m *SyncMap) Get(k string) (interface{}, bool)

func (*SyncMap) Range

func (m *SyncMap) Range(f func(k string, v any))

func (*SyncMap) Set

func (m *SyncMap) Set(k string, x interface{})

Jump to

Keyboard shortcuts

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