cache

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: May 22, 2022 License: MIT Imports: 3 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Cache implements the Cacher interface

func New

func New[K comparable, V any](expiration, cleanup time.Duration) (c *Cache[K, V])

New creates a new Cache for the specified key and value types. expiration specifies the default time an entry can live in the cache before expiring. cleanup specifies how often the cache will remove expired items.

func (*Cache[K, V]) Add

func (c *Cache[K, V]) Add(key K, value V)

Add adds a key/value pair to the cache, using the default expiry time

func (*Cache[K, V]) AddWithExpiry added in v0.0.2

func (c *Cache[K, V]) AddWithExpiry(key K, value V, expiry time.Duration)

AddWithExpiry adds a key/value pair to the cache with a specified expiry time

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(key K) (result V, found bool)

Get returns the value from the cache for the provided key. If the item is not found, or expired, found will be false

func (Cache[K, V]) GetDefaultExpiration added in v0.0.4

func (c Cache[K, V]) GetDefaultExpiration() time.Duration

GetDefaultExpiration returns the default expiration time of the cache

func (*Cache[K, V]) GetKeys added in v0.0.5

func (c *Cache[K, V]) GetKeys() (keys []K)

GetKeys returns all keys in the cache.

func (*Cache[K, V]) Len

func (c *Cache[K, V]) Len() (count int)

Len returns the number of non-expired items in the case

func (*Cache[K, V]) Size

func (c *Cache[K, V]) Size() int

Size returns the current size of the cache. Expired items are counted

type Cacher

type Cacher[K comparable, V any] interface {
	Add(key K, value V)
	AddWithExpiry(key K, value V, expiry time.Duration)
	Get(key K) (value V, found bool)
	GetKeys() (keys []K)
	GetDefaultExpiration() time.Duration
}

Cacher implements a cache interface

Jump to

Keyboard shortcuts

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