cache

package
v0.0.0-...-acbe8ed Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package cache provides a simple in-memory cache with optional TTL and weak reference support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is a simple in-memory cache with optional TTL support and weak references.

func New

func New[T any]() *Cache[T]

New creates a new Cache instance.

func (*Cache[T]) Cleanup

func (c *Cache[T]) Cleanup() int

Cleanup removes all expired entries and entries with GC'd values. This can be called periodically to free memory.

func (*Cache[T]) Clear

func (c *Cache[T]) Clear()

Clear removes all entries from the cache.

func (*Cache[T]) Delete

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

Delete removes a value from the cache.

func (*Cache[T]) Get

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

Get retrieves a value from the cache. Returns the value and true if found, not expired, and not GC'd; nil and false otherwise.

func (*Cache[T]) Len

func (c *Cache[T]) Len() int

Len returns the number of entries in the cache.

func (*Cache[T]) Set

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

Set stores a value with an optional TTL using weak references. If ttl is 0, the entry never expires (but can still be GC'd due to weak reference).

func (*Cache[T]) StartCleanupTimer

func (c *Cache[T]) StartCleanupTimer(interval time.Duration) func()

StartCleanupTimer starts a periodic cleanup of expired and GC'd entries. Returns a function to stop the cleanup timer.

type Entry

type Entry[T any] struct {
	Value     weak.Pointer[T]
	ExpiresAt *time.Time
}

Entry represents a cached value with optional expiration using weak references.

Jump to

Keyboard shortcuts

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