cache

package module
v0.0.0-...-0ccbb05 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2024 License: BSD-3-Clause Imports: 6 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 cycled on each Get() call, which permits the item to stay longer in the cache. There is a soft cap by default on the cache items that get rotated out based on the expiry date.

Index

Constants

View Source
const (

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

Variables

This section is empty.

Functions

func Debug

func Debug() bool

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

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) Delete

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

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

func (*Cache) EnableHardCap

func (c *Cache) EnableHardCap()

EnableHardCap disables the ability to rotate the oldest item out of cache to be replaced by a new item.

func (*Cache) Expire

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

Expire adds a new item to the cache that expires after the TTL is reached.

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