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 ¶
- func DefaultDir(appName string) string
- func GenerateKey(prefix string, params map[string]interface{}) string
- type Cache
- type FileCache
- type MemoryCache
- func (c *MemoryCache) Clear() error
- func (c *MemoryCache) Delete(key string) error
- func (c *MemoryCache) Get(key string) (interface{}, bool)
- func (c *MemoryCache) Keys() []string
- func (c *MemoryCache) Set(key string, value interface{}, ttl time.Duration) error
- func (c *MemoryCache) Stats() MemoryCacheStats
- type MemoryCacheStats
- type MemoryOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultDir ¶
DefaultDir returns the default cache directory for an application Uses XDG Base Directory specification: ~/.cache/<appname>
func GenerateKey ¶
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 ¶
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
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 ¶
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