cache

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package cache provides standardized caching for CLI applications. Supports both file-based and in-memory caching with consistent interfaces.

Example usage:

// File-based cache (default)
c := cache.New(cache.DefaultDir("myapp"), 24*time.Hour)
c.Set("key", data, 1*time.Hour)

// Memory cache for hot data
mc := cache.NewMemory(cache.MemoryOptions{MaxSize: 100})
mc.Set("key", data, 5*time.Minute)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultDir

func DefaultDir(appName string) string

DefaultDir returns the default cache directory for an application Uses XDG Base Directory specification: ~/.cache/<appname>

func GenerateKey

func GenerateKey(prefix string, params map[string]interface{}) string

GenerateKey creates a cache key from a prefix and parameters Useful for API response caching

Types

type Cache

type Cache interface {
	// Get retrieves an item from the cache
	// Returns the cached value and true if found and not expired
	Get(key string) (interface{}, bool)

	// Set stores an item in the cache with the specified TTL
	Set(key string, value interface{}, ttl time.Duration) error

	// Delete removes an item from the cache
	Delete(key string) error

	// Clear removes all items from the cache
	Clear() error

	// Keys returns all keys in the cache
	Keys() []string
}

Cache defines the interface for caching operations

func New

func New(dir string, defaultTTL time.Duration) Cache

New creates a new file-based cache dir: cache directory path defaultTTL: default time-to-live for cached items

func NewMemory

func NewMemory(opts MemoryOptions) Cache

NewMemory creates a new memory-based cache

type FileCache

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

FileCache implements file-based caching

func (*FileCache) Clear

func (c *FileCache) Clear() error

Clear removes all items from the cache

func (*FileCache) Delete

func (c *FileCache) Delete(key string) error

Delete removes an item from the cache

func (*FileCache) Get

func (c *FileCache) Get(key string) (interface{}, bool)

Get retrieves an item from the cache

func (*FileCache) Keys

func (c *FileCache) Keys() []string

Keys returns all keys in the cache

func (*FileCache) Set

func (c *FileCache) Set(key string, value interface{}, ttl time.Duration) error

Set stores an item in the cache

type MemoryCache

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

MemoryCache implements in-memory caching with LRU eviction

func (*MemoryCache) Clear

func (c *MemoryCache) Clear() error

Clear removes all items from the memory cache

func (*MemoryCache) Delete

func (c *MemoryCache) Delete(key string) error

Delete removes an item from the memory cache

func (*MemoryCache) Get

func (c *MemoryCache) Get(key string) (interface{}, bool)

Get retrieves an item from the memory cache

func (*MemoryCache) Keys

func (c *MemoryCache) Keys() []string

Keys returns all keys in the memory cache

func (*MemoryCache) Set

func (c *MemoryCache) Set(key string, value interface{}, ttl time.Duration) error

Set stores an item in the memory cache

func (*MemoryCache) Stats

func (c *MemoryCache) Stats() MemoryCacheStats

Stats returns cache statistics

type MemoryCacheStats

type MemoryCacheStats struct {
	Size    int
	MaxSize int
}

MemoryCacheStats represents cache statistics

func (MemoryCacheStats) String

func (s MemoryCacheStats) String() string

String returns a string representation of the stats

type MemoryOptions

type MemoryOptions struct {
	// MaxSize is the maximum number of items to store (0 = unlimited)
	MaxSize int

	// DefaultTTL is the default time-to-live for cached items
	DefaultTTL time.Duration
}

MemoryOptions configures memory cache behavior

Jump to

Keyboard shortcuts

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