cache

package
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

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 is a simple in memory cache that stores data in a map in memory. The cache is not persistent, so it will be lost when the application is restarted.

For the sake of speed and simplicity, try to store only necessary data in the cache to reduce the memory footprint and improve performance.

func New

func New(reset ...time.Duration) *Cache

Use this function to create a new cache

You can opt out of specifying the reset time and by default it will be set to 1 second Reset time is the time between each check for expired data

func (*Cache) Clear

func (c *Cache) Clear()

clears all the data in the cache

func (*Cache) Delete

func (c *Cache) Delete(key string)

deletes the data from the cache using the key

func (*Cache) Exists

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

Exists reports whether a non-expired entry for key is present in the cache. It performs the same expiry check as Get, so the two are always consistent.

func (*Cache) Get

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

Gets the data from the cache using the key. If the data is not found, it returns nil

func (*Cache) GetOrSet added in v1.1.4

func (c *Cache) GetOrSet(key string, fn func() (interface{}, time.Duration)) interface{}

GetOrSet atomically gets an existing unexpired entry or creates a new one. fn is called only when the key is absent or expired; its return value and TTL are stored under a single write lock — no TOCTOU window. If fn returns a zero TTL the cache's default reset interval is used.

func (*Cache) Set

func (c *Cache) Set(key string, data interface{}, ttl ...time.Duration)

Sets a new item to the cache specifying the key and data to store

You can opt out of specifying the time to live (ttl) and by default the cache will use the value specified when creating the cache using the New function

This will also start the cache if there was no items in the cache before.

Jump to

Keyboard shortcuts

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