cache

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2025 License: MIT Imports: 16 Imported by: 0

README

Porter Cache Package

The cache package provides an in-memory caching system specifically designed for Apache Arrow record batches. It implements a thread-safe, size-bounded cache with LRU (Least Recently Used) eviction policy and comprehensive statistics tracking.

Design Overview

Core Components
  1. Cache Interface

    type Cache interface {
        Get(ctx context.Context, key string) (arrow.Record, error)
        Put(ctx context.Context, key string, record arrow.Record) error
        Delete(ctx context.Context, key string) error
        Clear(ctx context.Context) error
        Close() error
    }
    
  2. MemoryCache Implementation

    • Thread-safe in-memory storage using sync.RWMutex
    • Size-bounded with configurable maximum size
    • LRU eviction policy
    • Arrow memory allocator integration
  3. CacheEntry Structure

    type CacheEntry struct {
        Record    arrow.Record
        CreatedAt time.Time
        LastUsed  time.Time
        Size      int64
    }
    
  4. Configuration System

    type Config struct {
        MaxSize    int64
        TTL        time.Duration
        Allocator  memory.Allocator
        EnableStats bool
    }
    
  5. Statistics Collection

    type Stats struct {
        Hits        uint64
        Misses      uint64
        Evictions   uint64
        Size        int64
        LastUpdated time.Time
    }
    

Data Flow

1. Cache Initialization
config := cache.DefaultConfig().
    WithMaxSize(100 * 1024 * 1024).  // 100MB
    WithTTL(5 * time.Minute).
    WithStats(true)

cache := cache.NewMemoryCache(config.MaxSize, config.Allocator)
2. Cache Operations
Storing Data (Put)
  1. Calculate record size (including schema overhead)
  2. Check if record fits in cache
  3. Evict entries if necessary (LRU policy)
  4. Store record with metadata
  5. Update cache size
  6. Record statistics
Retrieving Data (Get)
  1. Look up entry by key
  2. Update last used timestamp
  3. Retain record (increment reference count)
  4. Record hit/miss statistics
  5. Return record
Eviction Process
  1. Identify least recently used entry
  2. Release record (decrement reference count)
  3. Remove entry from cache
  4. Update cache size
  5. Record eviction statistics

Memory Management

Arrow Integration
  • Uses Apache Arrow's memory allocator
  • Properly manages record reference counts
  • Handles Arrow buffer lifecycle
Size Calculation
  • Includes all Arrow buffers
  • Accounts for schema overhead
  • Tracks child data structures

Thread Safety

  • Read-Write mutex for concurrent access
  • Atomic operations for statistics
  • Safe record reference counting

Performance Considerations

  1. Memory Efficiency

    • Configurable maximum size
    • LRU eviction policy
    • Proper Arrow memory management
  2. Concurrency

    • Read-Write lock for better concurrent access
    • Atomic statistics updates
    • Thread-safe operations
  3. Statistics

    • Hit/miss tracking
    • Eviction monitoring
    • Size tracking
    • Hit rate calculation

Usage Example

// Create cache with default configuration
config := cache.DefaultConfig()
cache := cache.NewMemoryCache(config.MaxSize, config.Allocator)

// Store a record
err := cache.Put(ctx, "key1", record)
if err != nil {
    log.Printf("Failed to cache record: %v", err)
}

// Retrieve a record
cachedRecord, err := cache.Get(ctx, "key1")
if err != nil {
    log.Printf("Failed to retrieve record: %v", err)
}

// Get cache statistics
stats := cache.GetStats()
hitRate := cache.HitRate()

Best Practices

  1. Configuration

    • Set appropriate maximum size
    • Configure TTL based on data freshness requirements
    • Enable statistics for monitoring
  2. Memory Management

    • Monitor cache size
    • Watch eviction rates
    • Adjust maximum size based on usage patterns
  3. Error Handling

    • Handle cache misses gracefully
    • Monitor error rates
    • Implement fallback strategies
  4. Monitoring

    • Track hit rates
    • Monitor eviction patterns
    • Watch memory usage

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Escape

func Escape(s string) string

escape ensures separator characters don't leak into the output.

func RenderValue

func RenderValue(v any) string

renderValue converts an arbitrary parameter value into a canonical string.

Types

type Cache

type Cache interface {
	// Get retrieves a record batch from the cache
	Get(ctx context.Context, key string) (arrow.Record, error)
	// Put stores a record batch in the cache
	Put(ctx context.Context, key string, record arrow.Record) error
	// Delete removes a record batch from the cache
	Delete(ctx context.Context, key string) error
	// Clear removes all entries from the cache
	Clear(ctx context.Context) error
	// Close releases any resources held by the cache
	Close() error
}

Cache defines the interface for caching Arrow record batches

type CacheEntry

type CacheEntry struct {
	Record    arrow.Record
	CreatedAt time.Time
	LastUsed  time.Time
	Size      int64
}

CacheEntry represents a single cache entry with metadata

type CacheKeyGenerator

type CacheKeyGenerator interface {
	GenerateKey(query string, params map[string]interface{}) string
}

CacheKeyGenerator defines the interface for generating cache keys

type Config

type Config struct {
	// MaxSize is the maximum size of the cache in bytes
	MaxSize int64
	// TTL is the time-to-live for cache entries
	TTL time.Duration
	// Allocator is the memory allocator to use
	Allocator memory.Allocator
	// EnableStats enables cache statistics collection
	EnableStats bool
}

Config holds the configuration for the cache

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns a default cache configuration

func (*Config) WithAllocator

func (c *Config) WithAllocator(alloc memory.Allocator) *Config

WithAllocator sets the memory allocator

func (*Config) WithMaxSize

func (c *Config) WithMaxSize(size int64) *Config

WithMaxSize sets the maximum size of the cache

func (*Config) WithStats

func (c *Config) WithStats(enable bool) *Config

WithStats enables or disables cache statistics

func (*Config) WithTTL

func (c *Config) WithTTL(ttl time.Duration) *Config

WithTTL sets the time-to-live for cache entries

type DefaultCacheKeyGenerator

type DefaultCacheKeyGenerator struct{}

DefaultCacheKeyGenerator implements CacheKeyGenerator

func (*DefaultCacheKeyGenerator) GenerateKey

func (g *DefaultCacheKeyGenerator) GenerateKey(query string, params map[string]interface{}) string

TODO:GenerateKey creates a deterministic cache‑key based on the SQL text (with whitespace normalised) and a stable, ordered serialisation of the parameter map.

The resulting key is a hex‑encoded SHA‑256 digest, keeping it short and collision‑resistant while hiding potentially sensitive values.

type MemoryCache

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

MemoryCache implements Cache interface using in-memory storage

func NewMemoryCache

func NewMemoryCache(maxSize int64, alloc memory.Allocator) *MemoryCache

NewMemoryCache creates a new memory cache with the specified maximum size

func (*MemoryCache) Clear

func (c *MemoryCache) Clear(ctx context.Context) error

Clear removes all entries from the cache

func (*MemoryCache) Close

func (c *MemoryCache) Close() error

Close releases any resources held by the cache

func (*MemoryCache) Delete

func (c *MemoryCache) Delete(ctx context.Context, key string) error

Delete removes a record batch from the cache

func (*MemoryCache) Get

func (c *MemoryCache) Get(ctx context.Context, key string) (arrow.Record, error)

Get retrieves a record batch from the cache

func (*MemoryCache) Put

func (c *MemoryCache) Put(ctx context.Context, key string, record arrow.Record) error

Put stores a record batch in the cache

type Stats

type Stats struct {
	Hits        uint64
	Misses      uint64
	Evictions   uint64
	Size        int64
	LastUpdated time.Time
}

Stats holds cache statistics

type StatsCollector

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

StatsCollector collects and reports cache statistics

func NewStatsCollector

func NewStatsCollector() *StatsCollector

NewStatsCollector creates a new statistics collector

func (*StatsCollector) GetStats

func (c *StatsCollector) GetStats() Stats

GetStats returns the current cache statistics

func (*StatsCollector) HitRate

func (c *StatsCollector) HitRate() float64

HitRate returns the cache hit rate

func (*StatsCollector) RecordEviction

func (c *StatsCollector) RecordEviction()

RecordEviction records a cache eviction

func (*StatsCollector) RecordHit

func (c *StatsCollector) RecordHit()

RecordHit records a cache hit

func (*StatsCollector) RecordMiss

func (c *StatsCollector) RecordMiss()

RecordMiss records a cache miss

func (*StatsCollector) UpdateSize

func (c *StatsCollector) UpdateSize(size int64)

UpdateSize updates the current cache size

Jump to

Keyboard shortcuts

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