goramcache

package module
v0.0.0-...-0b5da00 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2024 License: MIT Imports: 19 Imported by: 2

README

goramcache

goramcache 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 goramcache 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/sonnt85/goramcache

Usage
import (
	"fmt"
	"github.com/sonnt85/goramcache"
	"time"
)

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

	// Set the value of the key "foo" to "bar", with the default expiration time
	c.Set("foo", "bar", goramcache.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, goramcache.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, goramcache.DefaultExpiration)
	if x, found := c.Get("foo"); found {
		foo := x.(*MyStruct)
			// ...
	}
}
Reference

godoc or http://godoc.org/github.com/sonnt85/goramcache

Documentation

Index

Constants

View Source
const (
	NoExpirationCheck time.Duration = -1
	// 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 NewCache() or
	// NewCacheFrom() 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[K comparable, T any] struct {
	// contains filtered or unexported fields
}

func NewCache

func NewCache[K comparable, T any](defaultExpiration, errorAllowTimeExpiration time.Duration) *Cache[K, T]

Return a new cache with a given default expiration duration (defaultExpiration) 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 errorAllowTimeExpiration is less than one, expired items are not deleted from the cache before calling c.DeleteExpired().

func NewCacheFrom

func NewCacheFrom[K comparable, T any](defaultExpiration, errorAllowTimeExpiration time.Duration, items map[K]Item[T]) *Cache[K, T]

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().

NewFrom() also accepts an items map which will serve as the underlying map for the cache. This is useful for starting from a deserialized cache (serialized using e.g. gob.Encode() on c.Items()), or passing in e.g. make(map[string]Item, 500) to improve startup performance when the cache is expected to reach a certain minimum size.

Only the cache's methods synchronize access to this map, so it is not recommended to keep any references to the map around after creating a cache. If need be, the map can be accessed at a later point using c.Items() (subject to the same caveat.)

Note regarding serialization: When using e.g. gob, make sure to gob.Register() the individual types stored in the cache before encoding a map retrieved with c.Items(), and to register those same types before decoding a blob containing an items map.

func (Cache) Add

func (c Cache) Add(k K, x T, d time.Duration) error

Add an item to the cache only if an item doesn't already exist for the given key, or if the existing item has expired. Returns an error otherwise.

func (Cache) Decrement

func (c Cache) Decrement(k K, n int64) (T, error)

Decrement and return an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer, if it was not found, or if it is not possible to decrement it by n.

func (Cache) Delete

func (c Cache) Delete(k K)

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) DeleteRegex

func (c Cache) DeleteRegex(rule string)

func (Cache) Edit

func (c Cache) Edit(k K, x interface{}, apFunc func(T, interface{}) (T, error)) error

func (Cache) Flush

func (c Cache) Flush()

Delete all items from the cache.

func (Cache) Get

func (c Cache) Get(k K) (T, bool)

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

func (Cache) GetMultipleItems

func (c Cache) GetMultipleItems(keys []K) []T

GetMultipleItems returns an array of items corresponding to the input array

func (Cache) GetOrCreateNew

func (c Cache) GetOrCreateNew(k K) (T, bool)

func (Cache) GetRandom

func (c Cache) GetRandom() (key K, val T, b bool)

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

func (Cache) GetRandomThenDelete

func (c Cache) GetRandomThenDelete() (key K, val T, b bool)

func (Cache) GetThenDelete

func (c Cache) GetThenDelete(k K) (T, bool)

func (Cache) GetWithDefaultExpirationUpdate

func (c Cache) GetWithDefaultExpirationUpdate(k K) (T, bool)

func (Cache) GetWithExpiration

func (c Cache) GetWithExpiration(k K) (T, 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) GetWithExpirationUpdate

func (c Cache) GetWithExpirationUpdate(k K, d time.Duration) (T, bool)

GetWithExpirationUpdate returns item and updates its cache expiration time 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) Increment

func (c Cache) Increment(k K, n int64) (T, error)

Increment and return an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer, if it was not found, or if it is not possible to increment it by n.

func (Cache) IncrementExpiration

func (c Cache) IncrementExpiration(k K, d time.Duration) error

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[K]Item[T]

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

func (Cache) Iterate

func (c Cache) Iterate(f func(key K, item T) bool) bool

Iterate every item by item handle items from cache,and if the handle returns to false, it will be interrupted and return false.

func (Cache) Keys

func (c Cache) Keys() []K

Keys returns a sorted slice of all the keys in the cache.

func (Cache) Length

func (c Cache) Length() int

func (Cache) Load

func (c Cache) Load(r io.Reader) error

Add (Gob-serialized) cache items from an io.Reader, excluding any items with keys that already exist (and haven't expired) in the current cache.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Cache) LoadFile

func (c Cache) LoadFile(fname string) error

Load and add cache items from the given filename, excluding any items with keys that already exist in the current cache.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Cache) OnEvicted

func (c Cache) OnEvicted(f func(K, T))

Sets an (optional) function that is called with the key and value when an item is evicted from the cache. (Including when it is deleted manually, but not when it is overwritten.) Set to nil to disable.

func (Cache) Replace

func (c Cache) Replace(k K, x T, d time.Duration) error

Set a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns an error otherwise.

func (Cache) Save

func (c Cache) Save(w io.Writer) (err error)

Write the cache's items (using Gob) to an io.Writer.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Cache) SaveFile

func (c Cache) SaveFile(fname string) error

Save the cache's items to the given filename, creating the file if it doesn't exist, and overwriting it if it does.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (Cache) Set

func (c Cache) Set(k K, x T, 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 K, x T)

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

func (*Cache[K, T]) SetNextCheckExpireate

func (c *Cache[K, T]) SetNextCheckExpireate(d time.Duration)

func (Cache) Values

func (c Cache) Values() []T

type CacheFiles

type CacheFiles struct {
	*Cache[string, string]
	// contains filtered or unexported fields
}

func NewCacheFiles

func NewCacheFiles(rootDir string, defaultExpiration, errorAllowTimeExpiration time.Duration) *CacheFiles

func (CacheFiles) Add

func (c CacheFiles) Add(k K, x T, d time.Duration) error

Add an item to the cache only if an item doesn't already exist for the given key, or if the existing item has expired. Returns an error otherwise.

func (CacheFiles) Decrement

func (c CacheFiles) Decrement(k K, n int64) (T, error)

Decrement and return an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer, if it was not found, or if it is not possible to decrement it by n.

func (CacheFiles) Delete

func (c CacheFiles) Delete(k K)

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

func (CacheFiles) DeleteExpired

func (c CacheFiles) DeleteExpired()

Delete all expired items from the cache.

func (CacheFiles) DeleteRegex

func (c CacheFiles) DeleteRegex(rule string)

func (CacheFiles) Edit

func (c CacheFiles) Edit(k K, x interface{}, apFunc func(T, interface{}) (T, error)) error

func (CacheFiles) Flush

func (c CacheFiles) Flush()

Delete all items from the cache.

func (CacheFiles) Get

func (c CacheFiles) Get(k K) (T, bool)

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

func (*CacheFiles) GetCacheFile

func (cf *CacheFiles) GetCacheFile(fname string) (string, bool)

func (*CacheFiles) GetCacheFileContent

func (cf *CacheFiles) GetCacheFileContent(fname string) (content []byte, err error)

func (*CacheFiles) GetCacheFileContentString

func (cf *CacheFiles) GetCacheFileContentString(fname string) (string, error)

func (*CacheFiles) GetCacheFileOrCreate

func (cf *CacheFiles) GetCacheFileOrCreate(fname string) (pathCacheFile string, isOldFile bool)

func (*CacheFiles) GetCacheFileOrCreateWithExpiration

func (cf *CacheFiles) GetCacheFileOrCreateWithExpiration(fname string, d time.Duration) (pathCacheFile string, isOldFile bool)

func (*CacheFiles) GetCacheFromUrl

func (cf *CacheFiles) GetCacheFromUrl(fname, url, user, password string) (pathCacheFile string, err error)

func (CacheFiles) GetMultipleItems

func (c CacheFiles) GetMultipleItems(keys []K) []T

GetMultipleItems returns an array of items corresponding to the input array

func (CacheFiles) GetOrCreateNew

func (c CacheFiles) GetOrCreateNew(k K) (T, bool)

func (CacheFiles) GetRandom

func (c CacheFiles) GetRandom() (key K, val T, b bool)

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

func (CacheFiles) GetRandomThenDelete

func (c CacheFiles) GetRandomThenDelete() (key K, val T, b bool)

func (CacheFiles) GetThenDelete

func (c CacheFiles) GetThenDelete(k K) (T, bool)

func (CacheFiles) GetWithDefaultExpirationUpdate

func (c CacheFiles) GetWithDefaultExpirationUpdate(k K) (T, bool)

func (CacheFiles) GetWithExpiration

func (c CacheFiles) GetWithExpiration(k K) (T, 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 (CacheFiles) GetWithExpirationUpdate

func (c CacheFiles) GetWithExpirationUpdate(k K, d time.Duration) (T, bool)

GetWithExpirationUpdate returns item and updates its cache expiration time 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 (CacheFiles) Increment

func (c CacheFiles) Increment(k K, n int64) (T, error)

Increment and return an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer, if it was not found, or if it is not possible to increment it by n.

func (CacheFiles) IncrementExpiration

func (c CacheFiles) IncrementExpiration(k K, d time.Duration) error

func (CacheFiles) ItemCount

func (c CacheFiles) 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 (CacheFiles) Items

func (c CacheFiles) Items() map[K]Item[T]

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

func (CacheFiles) Iterate

func (c CacheFiles) Iterate(f func(key K, item T) bool) bool

Iterate every item by item handle items from cache,and if the handle returns to false, it will be interrupted and return false.

func (CacheFiles) Keys

func (c CacheFiles) Keys() []K

Keys returns a sorted slice of all the keys in the cache.

func (CacheFiles) Length

func (c CacheFiles) Length() int

func (CacheFiles) Load

func (c CacheFiles) Load(r io.Reader) error

Add (Gob-serialized) cache items from an io.Reader, excluding any items with keys that already exist (and haven't expired) in the current cache.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (CacheFiles) LoadFile

func (c CacheFiles) LoadFile(fname string) error

Load and add cache items from the given filename, excluding any items with keys that already exist in the current cache.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (CacheFiles) OnEvicted

func (c CacheFiles) OnEvicted(f func(K, T))

Sets an (optional) function that is called with the key and value when an item is evicted from the cache. (Including when it is deleted manually, but not when it is overwritten.) Set to nil to disable.

func (CacheFiles) Replace

func (c CacheFiles) Replace(k K, x T, d time.Duration) error

Set a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns an error otherwise.

func (CacheFiles) Save

func (c CacheFiles) Save(w io.Writer) (err error)

Write the cache's items (using Gob) to an io.Writer.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (CacheFiles) SaveFile

func (c CacheFiles) SaveFile(fname string) error

Save the cache's items to the given filename, creating the file if it doesn't exist, and overwriting it if it does.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (CacheFiles) Set

func (c CacheFiles) Set(k K, x T, 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 (CacheFiles) SetDefault

func (c CacheFiles) SetDefault(k K, x T)

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

func (CacheFiles) Values

func (c CacheFiles) Values() []T

type CacheFuncs

type CacheFuncs struct {
	// *Cache[string, string]
	*Cache[string, *funcmap.Task[string]]
}

func NewCacheFuncs

func NewCacheFuncs(defaultExpiration, errorAllowTimeExpiration time.Duration) *CacheFuncs

func (CacheFuncs) Add

func (c CacheFuncs) Add(k K, x T, d time.Duration) error

Add an item to the cache only if an item doesn't already exist for the given key, or if the existing item has expired. Returns an error otherwise.

func (*CacheFuncs) AddFunc

func (cf *CacheFuncs) AddFunc(fname string, f interface{}, params ...interface{}) (task *funcmap.Task[string], err error)

func (*CacheFuncs) AddFuncIfNotExist

func (cf *CacheFuncs) AddFuncIfNotExist(fname string, f interface{}, params ...interface{}) (task *funcmap.Task[string], err error)

func (*CacheFuncs) CallFunc

func (cf *CacheFuncs) CallFunc(fname string) (results []interface{}, err error)

func (CacheFuncs) Decrement

func (c CacheFuncs) Decrement(k K, n int64) (T, error)

Decrement and return an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer, if it was not found, or if it is not possible to decrement it by n.

func (CacheFuncs) Delete

func (c CacheFuncs) Delete(k K)

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

func (CacheFuncs) DeleteExpired

func (c CacheFuncs) DeleteExpired()

Delete all expired items from the cache.

func (CacheFuncs) DeleteRegex

func (c CacheFuncs) DeleteRegex(rule string)

func (CacheFuncs) Edit

func (c CacheFuncs) Edit(k K, x interface{}, apFunc func(T, interface{}) (T, error)) error

func (CacheFuncs) Flush

func (c CacheFuncs) Flush()

Delete all items from the cache.

func (CacheFuncs) Get

func (c CacheFuncs) Get(k K) (T, bool)

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

func (*CacheFuncs) GetFunc

func (cf *CacheFuncs) GetFunc(fname string) (*funcmap.Task[string], bool)

func (CacheFuncs) GetMultipleItems

func (c CacheFuncs) GetMultipleItems(keys []K) []T

GetMultipleItems returns an array of items corresponding to the input array

func (CacheFuncs) GetOrCreateNew

func (c CacheFuncs) GetOrCreateNew(k K) (T, bool)

func (CacheFuncs) GetRandom

func (c CacheFuncs) GetRandom() (key K, val T, b bool)

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

func (CacheFuncs) GetRandomThenDelete

func (c CacheFuncs) GetRandomThenDelete() (key K, val T, b bool)

func (CacheFuncs) GetThenDelete

func (c CacheFuncs) GetThenDelete(k K) (T, bool)

func (CacheFuncs) GetWithDefaultExpirationUpdate

func (c CacheFuncs) GetWithDefaultExpirationUpdate(k K) (T, bool)

func (CacheFuncs) GetWithExpiration

func (c CacheFuncs) GetWithExpiration(k K) (T, 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 (CacheFuncs) GetWithExpirationUpdate

func (c CacheFuncs) GetWithExpirationUpdate(k K, d time.Duration) (T, bool)

GetWithExpirationUpdate returns item and updates its cache expiration time 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 (CacheFuncs) Increment

func (c CacheFuncs) Increment(k K, n int64) (T, error)

Increment and return an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, uint32, or uint64, float32 or float64 by n. Returns an error if the item's value is not an integer, if it was not found, or if it is not possible to increment it by n.

func (CacheFuncs) IncrementExpiration

func (c CacheFuncs) IncrementExpiration(k K, d time.Duration) error

func (CacheFuncs) ItemCount

func (c CacheFuncs) 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 (CacheFuncs) Items

func (c CacheFuncs) Items() map[K]Item[T]

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

func (CacheFuncs) Iterate

func (c CacheFuncs) Iterate(f func(key K, item T) bool) bool

Iterate every item by item handle items from cache,and if the handle returns to false, it will be interrupted and return false.

func (CacheFuncs) Keys

func (c CacheFuncs) Keys() []K

Keys returns a sorted slice of all the keys in the cache.

func (CacheFuncs) Length

func (c CacheFuncs) Length() int

func (CacheFuncs) Load

func (c CacheFuncs) Load(r io.Reader) error

Add (Gob-serialized) cache items from an io.Reader, excluding any items with keys that already exist (and haven't expired) in the current cache.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (CacheFuncs) LoadFile

func (c CacheFuncs) LoadFile(fname string) error

Load and add cache items from the given filename, excluding any items with keys that already exist in the current cache.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (CacheFuncs) OnEvicted

func (c CacheFuncs) OnEvicted(f func(K, T))

Sets an (optional) function that is called with the key and value when an item is evicted from the cache. (Including when it is deleted manually, but not when it is overwritten.) Set to nil to disable.

func (CacheFuncs) Replace

func (c CacheFuncs) Replace(k K, x T, d time.Duration) error

Set a new value for the cache key only if it already exists, and the existing item hasn't expired. Returns an error otherwise.

func (CacheFuncs) Save

func (c CacheFuncs) Save(w io.Writer) (err error)

Write the cache's items (using Gob) to an io.Writer.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (CacheFuncs) SaveFile

func (c CacheFuncs) SaveFile(fname string) error

Save the cache's items to the given filename, creating the file if it doesn't exist, and overwriting it if it does.

NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the documentation for NewFrom().)

func (CacheFuncs) Set

func (c CacheFuncs) Set(k K, x T, 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 (CacheFuncs) SetDefault

func (c CacheFuncs) SetDefault(k K, x T)

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

func (CacheFuncs) Values

func (c CacheFuncs) Values() []T

type CachePools

type CachePools[K constraints.Ordered, T any] struct {
	// contains filtered or unexported fields
}

func NewCachePools

func NewCachePools[K constraints.Ordered, T any](defaultExpiration, errorAllowTimeExpiration time.Duration, numpools int) *CachePools[K, T]

func (*CachePools[K, T]) Add

func (sc *CachePools[K, T]) Add(k K, x T, d time.Duration) error

func (*CachePools[K, T]) Decrement

func (sc *CachePools[K, T]) Decrement(k K, n int64) error

func (*CachePools[K, T]) Delete

func (sc *CachePools[K, T]) Delete(k K)

func (*CachePools[K, T]) DeleteExpired

func (sc *CachePools[K, T]) DeleteExpired()

func (*CachePools[K, T]) Edit

func (sc *CachePools[K, T]) Edit(k K, x interface{}, apFunc func(T, interface{}) (T, error)) error

func (*CachePools[K, T]) Flush

func (sc *CachePools[K, T]) Flush()

func (*CachePools[K, T]) Get

func (sc *CachePools[K, T]) Get(k K) (T, bool)

func (*CachePools[K, T]) GetWithDefaultExpirationUpdate

func (sc *CachePools[K, T]) GetWithDefaultExpirationUpdate(k K) (T, bool)

func (*CachePools[K, T]) GetWithExpirationGet

func (sc *CachePools[K, T]) GetWithExpirationGet(k K) (T, time.Time, bool)

func (*CachePools[K, T]) GetWithExpirationUpdate

func (sc *CachePools[K, T]) GetWithExpirationUpdate(k K, d time.Duration) (T, bool)

func (*CachePools[K, T]) Increment

func (sc *CachePools[K, T]) Increment(k K, n int64) error

func (*CachePools[K, T]) Items

func (sc *CachePools[K, T]) Items() []map[K]Item[T]

Returns the items in the cache. This may include items that have expired, but have not yet been cleaned up. If this is significant, the Expiration fields of the items should be checked. Note that explicit synchronization is needed to use a cache and its corresponding Items() return values at the same time, as the maps are shared.

func (*CachePools[K, T]) Keys

func (sc *CachePools[K, T]) Keys() (keys []K)

func (*CachePools[K, T]) Load

func (sc *CachePools[K, T]) Load(r io.Reader) error

func (*CachePools[K, T]) LoadFile

func (sc *CachePools[K, T]) LoadFile(fname string) error

Load and add cache items from the given filename, excluding any items with keys that already exist in the current cache.

func (*CachePools[K, T]) Replace

func (sc *CachePools[K, T]) Replace(k K, x T, d time.Duration) error

func (*CachePools[K, T]) Save

func (sc *CachePools[K, T]) Save(w io.Writer) (err error)

func (*CachePools[K, T]) SaveFile

func (sc *CachePools[K, T]) SaveFile(fname string) error

func (*CachePools[K, T]) Set

func (sc *CachePools[K, T]) Set(k K, x T, d time.Duration)

func (*CachePools[K, T]) SetDefault

func (sc *CachePools[K, T]) SetDefault(k K, x T)

type Item

type Item[T any] struct {
	Object     T
	Expiration int64
}

func (Item[T]) Expired

func (item Item[T]) Expired() bool

Returns true if the item has expired.

type Janitor

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

func NewJanitor

func NewJanitor(interval time.Duration) *Janitor

func (*Janitor) GetInterval

func (j *Janitor) GetInterval() time.Duration

func (*Janitor) ResetDefault

func (j *Janitor) ResetDefault()

func (*Janitor) SetInterval

func (j *Janitor) SetInterval(interval time.Duration)

func (*Janitor) SetNextCheckExpire

func (j *Janitor) SetNextCheckExpire(moreTime time.Duration)

func (*Janitor) Start

func (j *Janitor) Start(obj any, f func() (nextCheckAt time.Time, needUpdateForNext bool))

type Pool

type Pool[T any] struct {
	// contains filtered or unexported fields
}

func NewPool

func NewPool[T any](maxElement int, expireTime time.Duration, fn ...func() T) *Pool[T]

func (*Pool[T]) Get

func (p *Pool[T]) Get() T

func (*Pool[T]) Put

func (p *Pool[T]) Put(obj T) (ok bool)

Jump to

Keyboard shortcuts

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