cache

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 4 Imported by: 6

README

cache

Test GoDoc

Basic generic cache

Authors

  • Christophe Lambin

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

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 a generic cache with expiration timers for entries, with optional removal of expired items.

func New

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

New creates a new Cache for the specified key and value types. The expiration parameter specifies the default time an entry can live in the cache before expiring. The cleanup parameters 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

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 expiration timer

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(key K) (V, 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

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

GetDefaultExpiration returns the default expiration time of the cache

func (*Cache[K, V]) GetKeys

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

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

func (*Cache[K, V]) Remove added in v0.4.0

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

Remove removes the element with the provided key from the cache.

func (*Cache[K, V]) Size

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

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

Jump to

Keyboard shortcuts

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