cache

package
v0.3.9 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package cache provides caching functionality for data sources.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get retrieves data from the cache
	// Returns (data, found, stale) where stale indicates data is stale but usable
	Get(key string) ([]map[string]interface{}, bool, bool)

	// Set stores data in the cache with the given TTL
	Set(key string, data []map[string]interface{}, ttl time.Duration)

	// SetWithStale stores data with separate stale and expire times
	// staleAfter: duration until data becomes stale
	// expireAfter: duration until data expires completely
	SetWithStale(key string, data []map[string]interface{}, staleAfter, expireAfter time.Duration)

	// Invalidate removes an entry from the cache
	Invalidate(key string)

	// InvalidateAll removes all entries from the cache
	InvalidateAll()
}

Cache defines the interface for source caching

type CacheInfo

type CacheInfo struct {
	Cached     bool   `json:"cached"`     // Whether data came from cache
	Age        string `json:"age"`        // How old the cached data is (e.g., "2m30s")
	ExpiresIn  string `json:"expires_in"` // Time until expiry (e.g., "4m15s")
	Stale      bool   `json:"stale"`      // Whether data is stale (but still usable)
	Refreshing bool   `json:"refreshing"` // Whether background refresh is in progress
}

CacheInfo provides cache metadata for UI display

type Entry

type Entry struct {
	Data      []map[string]interface{}
	ExpiresAt time.Time
	StaleAt   time.Time // For stale-while-revalidate: when data becomes stale (but still usable)
}

Entry represents a cached data entry

func (*Entry) IsExpired

func (e *Entry) IsExpired() bool

IsExpired returns true if the entry has expired

func (*Entry) IsStale

func (e *Entry) IsStale() bool

IsStale returns true if the entry is stale but not expired Used for stale-while-revalidate strategy

type MemoryCache

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

MemoryCache is an in-memory cache implementation with TTL support

func NewMemoryCache

func NewMemoryCache() *MemoryCache

NewMemoryCache creates a new in-memory cache

func (*MemoryCache) Get

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

Get retrieves data from the cache Returns (data, found, stale) where stale indicates data is stale but usable

func (*MemoryCache) Invalidate

func (c *MemoryCache) Invalidate(key string)

Invalidate removes an entry from the cache

func (*MemoryCache) InvalidateAll

func (c *MemoryCache) InvalidateAll()

InvalidateAll removes all entries from the cache

func (*MemoryCache) Len

func (c *MemoryCache) Len() int

Len returns the number of entries in the cache (for testing)

func (*MemoryCache) Set

func (c *MemoryCache) Set(key string, data []map[string]interface{}, ttl time.Duration)

Set stores data in the cache with the given TTL

func (*MemoryCache) SetWithStale

func (c *MemoryCache) SetWithStale(key string, data []map[string]interface{}, staleAfter, expireAfter time.Duration)

SetWithStale stores data with separate stale and expire times

func (*MemoryCache) Stop

func (c *MemoryCache) Stop()

Stop stops the background cleanup goroutine Safe to call multiple times

Jump to

Keyboard shortcuts

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