cache

package
v0.0.0-...-9edf4cd Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheMiss = errors.New("cache miss")

ErrCacheMiss is returned by Cache.Get if item is not found in cache.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is a simple rotating cache that maintains byte slices in memory up to the defined storage limit, both in memory size and entry count.

The cache is safe for concurrent access.

func NewCache

func NewCache(memLimit uint32, itemLimit uint32) *Cache

NewCache returns a new cache with the given memory usage limit in bytes and maximum entry count.

Arguments:

  • memLimit: The maximum memory usage of the cache in bytes.
  • itemLimit: The maximum number of items that can be stored in the cache.

Returns:

  • A pointer to a new Cache instance.

Example:

cache := NewCache(1024*1024, 100) // 1MB limit, 100 items max

func (*Cache) Delete

func (self *Cache) Delete(key string) (exists bool)

Delete deletes entry under key from cache if it exists and returns true if it was found and deleted, false otherwise.

Arguments:

  • key: The key of the item to delete.

Returns:

  • exists: True if the item was found and deleted, false otherwise.

Example:

deleted := cache.Delete("my_item")
if deleted {
	fmt.Println("Item deleted from cache")
} else {
	fmt.Println("Item not found in cache")
}

func (*Cache) Exists

func (self *Cache) Exists(key string) (exists bool)

Exists returns true if an entry under key exists in cache, false otherwise.

Arguments:

  • key: The key of the item to check for.

Returns:

  • exists: True if the item exists in the cache, false otherwise.

Example:

exists := cache.Exists("my_item")
if exists {
	fmt.Println("Item exists in cache")
} else {
	fmt.Println("Item does not exist in cache")
}

func (*Cache) Get

func (self *Cache) Get(key string) (out []byte, err error)

Get retrieves an item from cache by key.

Arguments:

  • key: The key of the item to retrieve.

Returns:

  • out: The byte slice stored under the given key, if found.
  • err: An error. ErrCacheMiss if the item was not found.

Example:

data, err := cache.Get("my_item")
if err != nil {
	if errors.Is(err, ErrCacheMiss) {
		fmt.Println("Item not found in cache")
	} else {
		fmt.Println("Error getting item:", err)
	}
	return
}
fmt.Println("Item found:", string(data))

func (*Cache) Put

func (self *Cache) Put(key string, data []byte)

Put stores data into cache under key and rotates the cache if storage limit has been reached. If an item with the same key already exists, it will be overwritten.

Arguments:

  • key: The key under which to store the data.
  • data: The byte slice to store in the cache.

Example:

data := []byte("some data to cache")
cache.Put("my_item", data)

func (*Cache) Usage

func (self *Cache) Usage() (used uint32)

Usage returns current memory usage in bytes.

Returns:

  • used: The current memory usage of the cache in bytes.

Example:

usage := cache.Usage()
fmt.Println("Cache usage:", usage, "bytes")

Jump to

Keyboard shortcuts

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