cache

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2025 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package cache provides high-performance in-memory caching with TTL support. It offers both a simple interface and optimized implementations using the otter cache library for production workloads requiring fast access times and automatic expiration.

Example usage:

// Create a new cache instance
ctx := context.Background()
cache, err := cache.NewCache(ctx)
if err != nil {
	log.Fatal(err)
}

// Store a value
cache.Set("user:123", userObject)

// Retrieve a value
if value, exists := cache.Get("user:123"); exists {
	user := value.(User)
	fmt.Printf("Found user: %+v\n", user)
}

// Remove a value
cache.Delete("user:123")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get retrieves a value by key from the cache.
	// Returns the value and true if the key exists, or nil and false if not found.
	Get(key string) (any, bool)

	// Set stores a value with the specified key in the cache.
	// Returns true if the operation was successful, false otherwise.
	Set(key string, value any) bool

	// Delete removes a key-value pair from the cache.
	// No error is returned if the key doesn't exist.
	Delete(key string)
}

Cache defines the interface for cache implementations. It provides basic cache operations for storing, retrieving, and removing key-value pairs. All implementations should be safe for concurrent use.

func NewCache

func NewCache(ctx context.Context) (Cache, error)

NewCache creates a new high-performance cache instance using the otter library. The cache is configured with a maximum capacity of 1,000 entries, 1-minute TTL, and statistics collection enabled. All entries have equal cost (1) for eviction purposes. The context parameter is reserved for future use and cancellation support. Returns an error if cache initialization fails, though this is unlikely with current configuration.

type InMemoryCache

type InMemoryCache struct {
	// contains filtered or unexported fields
}

InMemoryCache is a simple thread-safe in-memory cache implementation. It uses a map with read-write mutex for concurrent access protection. This implementation does not support TTL or automatic eviction.

Jump to

Keyboard shortcuts

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