cache

package
v0.0.0-...-67cf1b1 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package cache provides a type-safe in-memory cache implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[T any] struct {
	Options
	// contains filtered or unexported fields
}

Cache implements a type-safe in-memory cache

func New

func New[T any](options ...CacheOption) *Cache[T]

New creates a new cache instance with the provided options.

func (*Cache[T]) Delete

func (c *Cache[T]) Delete(key string)

Delete removes the data associated with a key

func (*Cache[T]) DeleteMatchingEntries

func (c *Cache[T]) DeleteMatchingEntries(fn func(T) bool)

DeleteMatchingEntries deletes cache entries that match the given filter function.

func (*Cache[T]) Filter

func (c *Cache[T]) Filter(fn func(T) bool) []T

Filter applies a filter function to the cache entries and returns a slice of filtered values.

func (*Cache[T]) Get

func (c *Cache[T]) Get(key string) (T, bool)

Get retrieves the value associated with the given key from the cache. If the key is not found or if the entry has expired, it returns the zero value of type T and false. Otherwise, it returns the value and true.

func (*Cache[T]) Prune

func (c *Cache[T]) Prune(key string)

Prune removes all cache's items

func (*Cache[T]) Set

func (c *Cache[T]) Set(key string, data T, exp time.Duration)

Set adds or updates a cache entry with the given key, data, and expiration duration.

func (*Cache[T]) Size

func (c *Cache[T]) Size() uint64

type CacheEntry

type CacheEntry[T any] struct {
	// contains filtered or unexported fields
}

CacheEntry holds the cached data and its expiration time

type CacheOption

type CacheOption interface {
	// contains filtered or unexported methods
}

func WithCapacity

func WithCapacity(o uint64) CacheOption

type Options

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

Jump to

Keyboard shortcuts

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