cache

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package cache provides a simple in-memory concurrency-safe key-value store for strings, with optional expiration. Think of it as a basic and local version of Redis or https://github.com/patrickmn/go-cache.

Index

Constants

View Source
const (
	DefaultCleanupInterval time.Duration = 61 * time.Minute // Prime number & slightly over an hour.
	DefaultExpiration      time.Duration = 0
	NoCleanup              time.Duration = 0 // For use in [New] only.
	NoExpiration           time.Duration = -1
	KeepTTL                time.Duration = -2 // For use in [Replace] only.
)

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 an in-memory concurrency-safe key-value store for strings, with optional expiration. Think of it as a basic and local version of Redis or https://github.com/patrickmn/go-cache.

func New

func New[T any](defaultExpiration, cleanupInterval time.Duration) *Cache[T]

New creates a new Cache instance.

func (*Cache[T]) Add

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

Add adds a value to the cache only if the key does not already exist or is expired. It returns true if the item was added, false otherwise. Note that any negative TTL value is treated as NoExpiration.

func (*Cache[T]) Clear

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

Clear removes all items from the cache.

func (*Cache[T]) Del

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

Del removes a specified item from the cache. If the item does not exist, this is a no-op.

func (*Cache[T]) Get

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

Get retrieves a value from the cache, and also returns a boolean indicating if it was found and not expired. It also handles lazy expiration (deleting expired items).

func (*Cache[T]) Item

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

Item retrieves a copy of an Item from the cache, and also returns a boolean indicating if it was found and not expired. It also handles lazy expiration (deleting expired items).

func (*Cache[T]) ItemCount

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

ItemCount returns the number of unexpired items in the cache. This is different from [Len], which returns the total number of items, including expired-but-still-present ones.

func (*Cache[T]) Items

func (c *Cache[T]) Items() map[string]Item[T]

Items returns a copy of all the unexpired [Item]s in the cache.

func (*Cache[T]) Keys

func (c *Cache[T]) Keys() []string

Keys returns a slice of all unexpired keys in the cache. The order of keys is not guaranteed.

func (*Cache[T]) Len

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

Len returns the total number of items in the cache, including expired-but-still-present ones.

func (*Cache[T]) Replace

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

Replace updates a value in the cache only if the key already exists and is not expired. It returns true if the item was replaced, false otherwise. See also KeepTTL, and note that any negative TTL value is treated as NoExpiration.

func (*Cache[T]) Set

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

Set adds a value to the cache with an optional Time-To-Live duration until it expires (see also DefaultExpiration and NoExpiration). Note that any negative TTL value is treated as NoExpiration.

type Item

type Item[T any] struct {
	Value      T
	Expiration time.Time
}

Item represents a single cache item, with its value and expiration time. An Expiration of zero means the item never expires.

func (*Item[T]) Expired

func (i *Item[T]) Expired() bool

Expired checks if the item is expired.

Jump to

Keyboard shortcuts

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