gcache

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// NoExpiration for use with functions that take an expiration time.
	NoExpiration time.Duration = -1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache a cache struct

func NewCache

func NewCache(cleanupInterval time.Duration) *Cache

NewCache return a new cache with a given cleanup interval. If the cleanup interval is less than one, expired items are not deleted from the cache before calling c.DeleteExpired().

func (Cache) Add

func (c Cache) 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 (Cache) Clear

func (c Cache) Clear()

Delete all items from the cache.

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

func (c Cache) ItemCount() int

ItemCount returns the number of items in the cache. This may include items that have expired, but have not yet been cleaned up. Equivalent to len(c.Items()).

func (Cache) OnEvicted

func (c Cache) OnEvicted(f func(string, interface{}))

OnEvicted 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 string, x interface{}, d time.Duration) error

Replace 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) Set

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

Set Add an item to the cache, replacing any existing item. If it is -1 (NoExpiration), the item never expires.

func (Cache) UpdateExpiration

func (c Cache) UpdateExpiration(k string, Expiration int64) error

UpdateExpiration

type Item

type Item struct {
	Object     interface{}
	Expiration int64
}

Item cached item

func (Item) Expired

func (item Item) Expired() bool

Expired Returns true if the item has expired.

Jump to

Keyboard shortcuts

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