cache

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2026 License: Apache-2.0 Imports: 3 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 thread-safe in-memory cache with TTL support

func New

func New(maxSize int, defaultTTL time.Duration) *Cache

New creates a new cache with the specified max size and default TTL

func (*Cache) Clear

func (c *Cache) Clear()

Clear removes all items from the cache

func (*Cache) Delete

func (c *Cache) Delete(key string)

Delete removes an item from the cache

func (*Cache) Exists

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

Exists checks if a key exists in the cache

func (*Cache) Get

func (c *Cache) Get(key string) (any, bool)

Get retrieves a value from the cache

func (*Cache) GetBytes

func (c *Cache) GetBytes(key string) ([]byte, bool)

GetBytes retrieves a byte slice from the cache

func (*Cache) GetString

func (c *Cache) GetString(key string) (string, bool)

GetString retrieves a string value from the cache

func (*Cache) Set

func (c *Cache) Set(key string, value any, ttl time.Duration)

Set adds or updates a value in the cache

func (*Cache) SetBytes

func (c *Cache) SetBytes(key string, value []byte, ttl time.Duration)

SetBytes adds a byte slice to the cache

func (*Cache) SetString

func (c *Cache) SetString(key string, value string, ttl time.Duration)

SetString adds a string value to the cache

func (*Cache) Size

func (c *Cache) Size() int

Size returns the number of items in the cache

func (*Cache) Stats

func (c *Cache) Stats() CacheStats

Stats returns cache statistics

type CacheStats

type CacheStats struct {
	Size    int
	MaxSize int
	HitRate float64
}

CacheStats holds cache statistics

type CacheWithLoader

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

CacheWithLoader is a cache that can load missing values

func NewWithLoader

func NewWithLoader(maxSize int, defaultTTL time.Duration, loader func(key string) (any, error)) *CacheWithLoader

NewWithLoader creates a cache with a loader function

func (*CacheWithLoader) GetOrLoad

func (c *CacheWithLoader) GetOrLoad(key string) (any, error)

GetOrLoad retrieves a value, loading it if not cached

type TypedCache

type TypedCache[T any] struct {
	*Cache
}

TypedCache is a type-safe wrapper around Cache

func NewTyped

func NewTyped[T any](maxSize int, defaultTTL time.Duration) *TypedCache[T]

NewTyped creates a new typed cache

func (*TypedCache[T]) Get

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

Get retrieves a typed value from the cache

func (*TypedCache[T]) Set

func (c *TypedCache[T]) Set(key string, value T, ttl time.Duration)

Set adds or updates a typed value in the cache

Jump to

Keyboard shortcuts

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