cache

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 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

checks if the data exists in the cache using the key

If you call this method and immediately afterwards call the Get method within the window of time that the data in the cache is set to expire, sometimes exists will return true but Get will return nil

To avoid this, you can call the Get method directly and check if the value returned is nil or not

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