cache

package module
v0.0.0-...-ffe4a0a Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2023 License: BSD-3-Clause Imports: 5 Imported by: 1

Documentation

Overview

Cache works as thread-safe least recently used (LRU) cache with expiring items on a given TTL span. The TTL gets automatically re-cycled on each Get() call, which permits to stay longer in the cache.

Index

Constants

View Source
const (

	// Null disables the ttl
	Null time.Duration = time.Nanosecond * 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {

	// Logger defaults to stderr and adds a prefix of "cache: "
	Logger *log.Logger
	// contains filtered or unexported fields
}

Cache holds all items and configuration.

func New

func New(ttl time.Duration, max uint) *Cache

New creates a new cache instance, taking a ttl and max number for the containing items.

ttl = Null | never expires [~100 years]
max = 0    | fallback to default maxItems [64]

func (*Cache) Count

func (c *Cache) Count() uint

Count returns the number of items in the cache.

func (*Cache) Debug

func (c *Cache) Debug() bool

Debug toggles (enables/disables) debug output through Logger and returns either true if enabled or respectively false if disabled.

func (*Cache) Delete

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

Delete removes the item called by the name of key from the cache.

func (*Cache) Get

func (c *Cache) Get(key string) (interface{}, bool)

Get lookup items by key name and returns the data if found.

func (*Cache) Info

func (c *Cache) Info() []Info

Info returns metadata of the items in the cache.

func (*Cache) Max

func (c *Cache) Max() uint

Max returns the given max value from your New() call.

func (*Cache) Once

func (c *Cache) Once(key string, data interface{}) error

Once adds new item to the cache that expires after the first cycle and gets no update on a Get call.

func (*Cache) Persist

func (c *Cache) Persist(key string, data interface{}) error

Persist adds new item to the cache that never expires.

func (*Cache) Set

func (c *Cache) Set(key string, data interface{}) error

Set adds new item to the cache.

type Info

type Info struct {
	Date time.Time
	Name string
	Keep bool
	Once bool
}

Info holds metadata about the cache content.

Jump to

Keyboard shortcuts

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