cache

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: Apache-2.0, MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is a thread-safe in-memory key/value store.

func New

func New(maxItems int, interval time.Duration) *Cache

New creates a new cache with the given configuration.

func (Cache) Add

func (c Cache) Add(key string, value interface{}, expiration time.Duration) error

Add an item to the cache, existing items will not be overwritten. To overwrite existing items, use Set. If the cache is full, Add will return an error.

func (Cache) Clear

func (c Cache) Clear()

Clear all items from the cache. This reallocate the inderlying array holding the items, so that the memory used by the items is reclaimed.

func (Cache) Delete

func (c Cache) Delete(key string)

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

func (Cache) DeleteExpired

func (c Cache) DeleteExpired()

DeleteExpired deletes all expired items from the cache.

func (Cache) Get

func (c Cache) Get(key 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) GetExpiration

func (c Cache) GetExpiration(key string) time.Duration

GetExpiration returns the expiration for the given key. Returns zero if the key is not in the cache or the item has already expired.

func (Cache) HasExpired

func (c Cache) HasExpired(key string) bool

HasExpired returns true if the item has expired.

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.

func (Cache) Set

func (c Cache) Set(key string, value interface{}, expiration time.Duration) error

Set adds an item to the cache, replacing any existing item. If expiration is zero, the item never expires. If the cache is full, Set will return an error.

func (Cache) SetExpiration

func (c Cache) SetExpiration(key string, expiration time.Duration)

SetExpiration sets the expiration for the given key. Does nothing if the key is not in the cache.

type Item

type Item struct {
	// Object is the item's value.
	Object interface{}
	// Expiration is the item's expiration time.
	Expiration int64
}

Item is an item stored in the cache.

Jump to

Keyboard shortcuts

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