tinycache

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2026 License: MIT Imports: 3 Imported by: 0

README

tinycache

tinycache is a tiny, no-frills, in-memory cache for a single type. Safe for concurrent usage. Lazy expiration by default but a reaping option can be enabled if needed.

Usage

Grab the module:

go get -u github.com/Xiol/tinycache

Then use it:

package main

import (
	"fmt"
	"time"

	"github.com/Xiol/tinycache"
)

func main() {
	cache := tinycache.New[string](
		// WithTTL sets the default TTL for all keys. TTLs for
		// individual keys can be set with SetTTL(), and this
		// overrides the default. If omitted or zero, keys will 
		// not expire.
		tinycache.WithTTL(1*time.Second),

		// When WithReapInterval is provided, a reaper goroutine
		// is started which removes expired keys at the specified
		// interval. If omitted, keys will not be cleaned up
		// automatically. Call Reap() yourself, or Delete() keys
		// manually.
		tinycache.WithReapInterval(2*time.Second),
	)
	// Closing the cache will stop the reaper goroutine and remove
	// all cache entries
	defer cache.Close()

	// Set key/value pairs in the cache
	cache.Set("key1", "value1")
	cache.SetTTL("key2", "value2", 3*time.Second)

	// Retrieve values from the cache
	v, ok := cache.Get("key1")
	if ok {
		fmt.Println(v)
	}

	v2, ok := cache.Get("key2")
	if ok {
		fmt.Println(v2)
	}

	// Wait for TTL
	time.Sleep(2 * time.Second)

	_, ok = cache.Get("key1")
	if ok {
		panic("key1 should be expired")
	} else {
		fmt.Println("key1 expired")
	}

	vc2, ok := cache.Get("key2")
	if ok {
		fmt.Printf("key2 still alive: %s\n", vc2)
	} else {
		panic("key2 should still be alive")
	}

	// Delete a key from the cache
	cache.Delete("key2")
}

Documentation

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
}

func New

func New[T any](opts ...Option) *Cache[T]

func (*Cache[T]) Clear added in v0.3.0

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

func (*Cache[T]) Close

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

func (*Cache[T]) Delete

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

func (*Cache[T]) Get

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

func (*Cache[T]) Len added in v0.3.0

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

func (*Cache[T]) Reap

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

func (*Cache[T]) Set

func (c *Cache[T]) Set(key string, value T)

func (*Cache[T]) SetPermanent added in v0.2.0

func (c *Cache[T]) SetPermanent(key string, value T)

func (*Cache[T]) SetTTL

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

type Option

type Option func(*cacheOptions)

func WithReapInterval

func WithReapInterval(interval time.Duration) Option

func WithTTL

func WithTTL(ttl time.Duration) Option

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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