cache

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package cache provides a simple, thread-safe, in-memory key-value store. It features item expiration and an optional background process (janitor) that periodically removes expired items.

Index

Constants

View Source
const (
	// DefaultJanitorInterval is the default interval at which the janitor
	// runs to clean up expired cache items.
	DefaultJanitorInterval = 1 * time.Minute
	// DefaultExpiration is the default time-to-live for a cache item.
	// Note: This constant is defined but not used in the current implementation,
	// as expiration is set on a per-item basis.
	DefaultExpiration = 60
)

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 key-value store with self-cleaning capabilities.

func NewCache

func NewCache() *Cache

NewCache creates and returns a new Cache instance. The janitor for cleaning up expired items is not started by default. Use the WithJanitor method to start the cleanup process.

Example:

c := cache.NewCache()
c.Set("myKey", "myValue", 5*time.Minute)

func (*Cache) Get

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

Get retrieves an item from the cache by its key. It returns the item's value and a boolean. The boolean is true if the key was found and the item has not expired. Otherwise, it is false.

Example:

v, found := c.Get("myKey")
if found {
	fmt.Printf("Found value: %v\n", v)
} else {
	fmt.Println("Key not found or expired.")
}

func (*Cache) Set

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

Set adds an item to the cache, replacing any existing item with the same key.

The `ttl` (time-to-live) parameter specifies how long the item should remain in the cache. If `ttl` is positive, the item will expire after that duration. If `ttl` is zero or negative, the item will never expire.

Example:

// Add a key that expires in 5 minutes.
c.Set("sessionToken", "xyz123", 5*time.Minute)

// Add a key that never expires.
c.Set("appConfig", "configValue", 0)

func (*Cache) Stop

func (c *Cache) Stop()

Stop terminates the background janitor goroutine. It is safe to call Stop even if the janitor was never started or has already been stopped. This is useful for cleaning up resources.

func (*Cache) WithJanitor

func (c *Cache) WithJanitor(interval time.Duration) *Cache

WithJanitor starts a background goroutine (janitor) that periodically cleans up expired items from the cache. If a janitor is already running, it will be stopped and a new one will be started with the specified interval.

The interval parameter defines how often the janitor should run. If a non-positive interval is provided, it defaults to DefaultJanitorInterval (1 minute).

It returns a pointer to the Cache to allow for method chaining.

Example:

// Create a cache that cleans itself every 10 minutes.
c := cache.NewCache().WithJanitor(10 * time.Minute)
defer c.Stop() // It's important to stop the janitor when the cache is no longer needed.

type CacheItem

type CacheItem struct {
	Value      any   // The actual value being stored.
	Expiration int64 // The time when the item expires, as a Unix nano timestamp. 0 means no expiration.
}

CacheItem represents a value stored in the cache, along with its expiration time.

Jump to

Keyboard shortcuts

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