cache

package
v1.61.1 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2022 License: MIT Imports: 3 Imported by: 11

Documentation

Overview

Package cache implements a simple cache where the entries are expired after a given time (5 minutes of disuse by default).

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 holds values indexed by string, but expired after a given (5 minutes by default).

func New

func New() *Cache

New creates a new cache with the default expire duration and interval

func (*Cache) Clear

func (c *Cache) Clear()

Clear removes everything from the cache

func (*Cache) Delete added in v1.55.0

func (c *Cache) Delete(key string) bool

Delete the entry passed in

Returns true if the entry was found

func (*Cache) DeletePrefix added in v1.55.0

func (c *Cache) DeletePrefix(prefix string) (deleted int)

DeletePrefix deletes all entries with the given prefix

Returns number of entries deleted

func (*Cache) Entries

func (c *Cache) Entries() int

Entries returns the number of entries in the cache

func (*Cache) Get

func (c *Cache) Get(key string, create CreateFunc) (value interface{}, err error)

Get gets a value named key either from the cache or creates it afresh with the create function.

func (*Cache) GetMaybe

func (c *Cache) GetMaybe(key string) (value interface{}, found bool)

GetMaybe returns the key and true if found, nil and false if not

func (*Cache) Pin added in v1.52.0

func (c *Cache) Pin(key string)

Pin a value in the cache if it exists

func (*Cache) Put

func (c *Cache) Put(key string, value interface{})

Put puts a value named key into the cache

func (*Cache) Rename added in v1.52.0

func (c *Cache) Rename(oldKey, newKey string) (value interface{}, found bool)

Rename renames the item at oldKey to newKey.

If there was an existing item at newKey then it takes precedence and is returned otherwise the item (if any) at oldKey is returned.

func (*Cache) SetExpireDuration added in v1.55.0

func (c *Cache) SetExpireDuration(d time.Duration) *Cache

SetExpireDuration sets the interval at which things expire

If it is less than or equal to 0 then things are never cached

func (*Cache) SetExpireInterval added in v1.55.0

func (c *Cache) SetExpireInterval(d time.Duration) *Cache

SetExpireInterval sets the interval at which the cache expiry runs

Set to 0 or a -ve number to disable

func (*Cache) SetFinalizer added in v1.59.0

func (c *Cache) SetFinalizer(finalize func(interface{}))

SetFinalizer sets a function to be called when a value drops out of the cache

func (*Cache) Unpin added in v1.52.0

func (c *Cache) Unpin(key string)

Unpin a value in the cache if it exists

type CreateFunc

type CreateFunc func(key string) (value interface{}, ok bool, error error)

CreateFunc is called to create new values. If the create function returns an error it will be cached if ok is true, otherwise the error will just be returned, allowing negative caching if required.

Jump to

Keyboard shortcuts

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