cachekit

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: MIT Imports: 2 Imported by: 0

README

go-cache-kit

Generic in-memory LRU cache with TTL, tags, and thread safety for Go.

Installation

go get github.com/philiprehberger/go-cache-kit

Usage

Basic Cache
import "github.com/philiprehberger/go-cache-kit"

cache := cachekit.New[string](1000, 5*time.Minute)

cache.Set("key", "value")
val, ok := cache.Get("key") // "value", true
Custom TTL per Entry
cache.Set("session", "abc123", cachekit.WithTTL(30*time.Minute))
cache.Set("temp", "data", cachekit.WithTTL(5*time.Second))
Tag-Based Invalidation
cache.Set("user:1", userData, cachekit.WithTags("users", "team-a"))
cache.Set("user:2", userData, cachekit.WithTags("users", "team-b"))
cache.Set("post:1", postData, cachekit.WithTags("posts", "team-a"))

removed := cache.InvalidateByTag("team-a") // 2
LRU Eviction
cache := cachekit.New[string](100, 0) // no default TTL

// When full, least recently used entries are evicted
// Expired entries are preferred for eviction
Other Operations
cache.Has("key")      // check existence
cache.Delete("key")   // delete single entry
cache.Keys()          // list all non-expired keys
cache.Size()          // current entry count
cache.Clear()         // remove everything

License

MIT

Documentation

Overview

Package cachekit provides a generic in-memory LRU cache with TTL and tag-based invalidation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is a thread-safe in-memory LRU cache with TTL and tag support.

func New

func New[V any](maxSize int, defaultTTL time.Duration) *Cache[V]

New creates a new Cache with the given max size and default TTL. A zero TTL means entries don't expire by default.

func (*Cache[V]) Clear

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

Clear removes all entries.

func (*Cache[V]) Delete

func (c *Cache[V]) Delete(key string) bool

Delete removes an entry by key. Returns true if the key was found.

func (*Cache[V]) Get

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

Get retrieves a value from the cache. Returns the value and true if found and not expired.

func (*Cache[V]) Has

func (c *Cache[V]) Has(key string) bool

Has checks if a key exists and is not expired.

func (*Cache[V]) InvalidateByTag

func (c *Cache[V]) InvalidateByTag(tag string) int

InvalidateByTag removes all entries with the given tag. Returns the count removed.

func (*Cache[V]) Keys

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

Keys returns all non-expired keys.

func (*Cache[V]) Set

func (c *Cache[V]) Set(key string, value V, opts ...SetOption)

Set adds or updates an entry in the cache.

func (*Cache[V]) Size

func (c *Cache[V]) Size() int

Size returns the number of entries in the cache.

type SetOption

type SetOption func(*setConfig)

SetOption configures a Set call.

func WithTTL

func WithTTL(d time.Duration) SetOption

WithTTL overrides the default TTL for this entry.

func WithTags

func WithTags(tags ...string) SetOption

WithTags associates tags with the entry.

Jump to

Keyboard shortcuts

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