memeorycache

package
v0.0.0-...-cc150fd Latest Latest
Warning

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

Go to latest
Published: May 30, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyExists = errors.New("cache: item already exists")
	ErrCacheMiss = errors.New("cache: item not found")
)

Functions

This section is empty.

Types

type MemeoryCache

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

func NewMemeoryCache

func NewMemeoryCache(defaultExpiration, cleanupInterval time.Duration) *MemeoryCache

Return a new cache with a given default expiration duration and cleanup interval. If the expiration duration is less than 1, 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 their next lookup or before calling DeleteExpired.

func (MemeoryCache) Add

func (c MemeoryCache) Add(k string, x interface{}, 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 (MemeoryCache) Decrement

func (c MemeoryCache) Decrement(k string, n uint64) (uint64, error)

Decrement an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, uint32, or uint64 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. Stops at 0 on underflow.

func (MemeoryCache) Delete

func (c MemeoryCache) Delete(k string) (found bool)

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

func (MemeoryCache) DeleteExpired

func (c MemeoryCache) DeleteExpired()

Delete all expired items from the cache.

func (MemeoryCache) Flush

func (c MemeoryCache) Flush()

Delete all items from the cache.

func (MemeoryCache) Get

func (c MemeoryCache) 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 (MemeoryCache) Increment

func (c MemeoryCache) Increment(k string, n uint64) (uint64, error)

Increment an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, uint32, or uint64 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. Wraps around on overlow.

func (MemeoryCache) IncrementFloat

func (c MemeoryCache) IncrementFloat(k string, n float64) error

Increment an item of type float32 or float64 by n. Returns an error if the item's value is not floating point, if it was not found, or if it is not possible to increment it by n. Pass a negative number to decrement the value.

func (MemeoryCache) Load

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

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

func (MemeoryCache) LoadFile

func (c MemeoryCache) 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 (MemeoryCache) Replace

func (c MemeoryCache) Replace(k string, x interface{}, d time.Duration) error

Set a new value for the cache key only if it already exists. Returns an error if it does not.

func (MemeoryCache) Save

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

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

func (MemeoryCache) SaveFile

func (c MemeoryCache) 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.

func (MemeoryCache) Set

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

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

Jump to

Keyboard shortcuts

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