cache

package
v1.0.16 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitGlobalCache

func InitGlobalCache(cacheDir string) error

InitGlobalCache initializes the global cache with the given directory.

Types

type BadgerCache

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

BadgerCache implements the Cache interface using Badger DB.

func GetBadgerCache

func GetBadgerCache() (*BadgerCache, bool)

GetBadgerCache returns the global cache as a BadgerCache if applicable.

func NewBadgerCache

func NewBadgerCache(dir string) (*BadgerCache, error)

NewBadgerCache creates a new Badger-based cache.

func (*BadgerCache) Clear

func (c *BadgerCache) Clear() error

Clear removes all items from the cache.

func (*BadgerCache) Close

func (c *BadgerCache) Close() error

Close closes the badger database and stops the background GC goroutine.

func (*BadgerCache) Delete

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

Delete removes an item from the cache.

func (*BadgerCache) Get

func (c *BadgerCache) Get(key string, dest interface{}) (bool, error)

Get retrieves data from the cache.

func (*BadgerCache) Set

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

Set stores data in the cache.

type Cache

type Cache interface {
	// Get retrieves data from the cache, returning whether it was found
	Get(key string, dest interface{}) (bool, error)

	// Set stores data in the cache with optional TTL
	Set(key string, data 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

	// Close closes the cache and releases any resources
	Close() error
}

Cache defines the interface for the caching system.

func GetGlobalCache

func GetGlobalCache() Cache

GetGlobalCache returns the global cache instance.

func GetNamespacedCache added in v1.0.7

func GetNamespacedCache(namespace string) Cache

GetNamespacedCache returns (and initializes if needed) a cache scoped to the provided namespace. Namespaced caches live alongside the global cache but operate on their own storage so they aren't affected by global cache invalidation.

type CacheItem

type CacheItem struct {
	Data      json.RawMessage `json:"data"` // Store as raw JSON to avoid double marshaling
	Timestamp int64           `json:"timestamp"`
	TTL       int64           `json:"ttl"` // TTL in seconds, 0 means no expiration
}

CacheItem represents an item in the cache with TTL.

type FileCache

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

FileCache implements a simple file-based cache with LRU eviction.

func NewFileCache

func NewFileCache(cacheDir string, persisted bool) (*FileCache, error)

NewFileCache creates a new file-based cache with optional size limit. maxSize of 0 means unlimited cache size.

func NewFileCacheWithSize added in v1.0.7

func NewFileCacheWithSize(cacheDir string, persisted bool, maxSize int) (*FileCache, error)

NewFileCacheWithSize creates a new file-based cache with a maximum size limit. When the cache exceeds maxSize items, least recently used items are evicted. maxSize of 0 means unlimited cache size.

func NewMemoryCache

func NewMemoryCache() *FileCache

NewMemoryCache creates an in-memory only cache (no persistence).

func NewMemoryCacheWithSize added in v1.0.7

func NewMemoryCacheWithSize(maxSize int) *FileCache

NewMemoryCacheWithSize creates an in-memory cache with a size limit.

func (*FileCache) Clear

func (c *FileCache) Clear() error

Clear removes all items from the cache.

func (*FileCache) Close

func (c *FileCache) Close() error

Close implements the Cache.Close method for FileCache This is a no-op for FileCache since it doesn't maintain any resources that need explicit closing.

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, dest interface{}) (bool, error)

Get retrieves data from the cache and updates LRU order.

func (*FileCache) Set

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

Set stores data in the cache.

Jump to

Keyboard shortcuts

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