cache

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package cache provides caching functionality for templates.

Package cache provides caching functionality for templates.

Package cache provides caching functionality for templates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheEntry

type CacheEntry struct {
	// Template is the cached template
	Template *format.Template
	// CreatedAt is the time the entry was created
	CreatedAt time.Time
	// ExpiresAt is the time the entry expires
	ExpiresAt time.Time
	// LastAccessed is the time the entry was last accessed
	LastAccessed time.Time
	// AccessCount is the number of times the entry has been accessed
	AccessCount int
}

CacheEntry represents a cached template

type CacheLevel

type CacheLevel int

CacheLevel represents the level of caching

const (
	// LevelFragment represents fragment-level caching (template parts)
	LevelFragment CacheLevel = iota
	// LevelTemplate represents template-level caching (full templates)
	LevelTemplate
	// LevelQuery represents query-level caching (data queries)
	LevelQuery
	// LevelResult represents result-level caching (execution results)
	LevelResult
)

type CacheOptions

type CacheOptions struct {
	// EnableFragmentCache enables fragment-level caching
	EnableFragmentCache bool
	// EnableTemplateCache enables template-level caching
	EnableTemplateCache bool
	// EnableQueryCache enables query-level caching
	EnableQueryCache bool
	// EnableResultCache enables result-level caching
	EnableResultCache bool
	// FragmentTTL is the TTL for fragment cache entries
	FragmentTTL time.Duration
	// TemplateTTL is the TTL for template cache entries
	TemplateTTL time.Duration
	// QueryTTL is the TTL for query cache entries
	QueryTTL time.Duration
	// ResultTTL is the TTL for result cache entries
	ResultTTL time.Duration
	// FragmentCacheSize is the maximum size of the fragment cache
	FragmentCacheSize int
	// TemplateCacheSize is the maximum size of the template cache
	TemplateCacheSize int
	// QueryCacheSize is the maximum size of the query cache
	QueryCacheSize int
	// ResultCacheSize is the maximum size of the result cache
	ResultCacheSize int
	// EnableCompression enables compression of cached items
	EnableCompression bool
	// CompressionLevel is the compression level (1-9)
	CompressionLevel int
	// EnableSharding enables sharding of the cache
	EnableSharding bool
	// ShardCount is the number of shards
	ShardCount int
	// EnablePrefetching enables prefetching of related items
	EnablePrefetching bool
	// PrefetchCount is the number of related items to prefetch
	PrefetchCount int
	// EnableAdaptiveTTL enables adaptive TTL based on access patterns
	EnableAdaptiveTTL bool
	// MinAdaptiveTTL is the minimum TTL for adaptive TTL
	MinAdaptiveTTL time.Duration
	// MaxAdaptiveTTL is the maximum TTL for adaptive TTL
	MaxAdaptiveTTL time.Duration
}

CacheOptions represents options for the multi-level cache

func DefaultCacheOptions

func DefaultCacheOptions() *CacheOptions

DefaultCacheOptions returns default cache options

type CacheStats

type CacheStats struct {
	// Hits is the number of cache hits
	Hits int64
	// Misses is the number of cache misses
	Misses int64
	// Evictions is the number of cache evictions
	Evictions int64
	// Expirations is the number of expired entries
	Expirations int64
	// TotalLookups is the total number of lookups
	TotalLookups int64
}

CacheStats tracks cache statistics

type EvictionPolicy

type EvictionPolicy string

EvictionPolicy represents the policy for evicting cache entries

const (
	// LRU (Least Recently Used) evicts the least recently used entries first
	LRU EvictionPolicy = "lru"
	// LFU (Least Frequently Used) evicts the least frequently used entries first
	LFU EvictionPolicy = "lfu"
	// FIFO (First In, First Out) evicts the oldest entries first
	FIFO EvictionPolicy = "fifo"
)

type MultiLevelCache

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

MultiLevelCache is a hierarchical caching system with multiple levels

func NewMultiLevelCache

func NewMultiLevelCache(options *CacheOptions) *MultiLevelCache

NewMultiLevelCache creates a new multi-level cache

func (*MultiLevelCache) Clear

func (c *MultiLevelCache) Clear()

Clear clears all caches

func (*MultiLevelCache) ClearLevel

func (c *MultiLevelCache) ClearLevel(level CacheLevel)

ClearLevel clears a specific cache level

func (*MultiLevelCache) GetExecutionResult

func (c *MultiLevelCache) GetExecutionResult(templateID string, options string) (*interfaces.TemplateResult, bool)

GetExecutionResult gets a template execution result from the cache

func (*MultiLevelCache) GetFragment

func (c *MultiLevelCache) GetFragment(id string) (*format.Template, bool)

GetFragment gets a template fragment from the cache

func (*MultiLevelCache) GetQueryResult

func (c *MultiLevelCache) GetQueryResult(query string) (interface{}, bool)

GetQueryResult gets a query result from the cache

func (*MultiLevelCache) GetStats

func (c *MultiLevelCache) GetStats() map[string]interface{}

GetStats returns statistics about the cache

func (*MultiLevelCache) GetTemplate

func (c *MultiLevelCache) GetTemplate(id string) (*format.Template, bool)

GetTemplate gets a template from the cache

func (*MultiLevelCache) PreloadTemplates

func (c *MultiLevelCache) PreloadTemplates(templates map[string]*format.Template)

PreloadTemplates preloads templates into the cache

func (*MultiLevelCache) Prune

func (c *MultiLevelCache) Prune(ctx context.Context) int

Prune removes old entries from all caches

func (*MultiLevelCache) PruneLevel

func (c *MultiLevelCache) PruneLevel(level CacheLevel, maxAge time.Duration) int

PruneLevel prunes a specific cache level

func (*MultiLevelCache) SetExecutionResult

func (c *MultiLevelCache) SetExecutionResult(templateID string, options string, result *interfaces.TemplateResult)

SetExecutionResult sets a template execution result in the cache

func (*MultiLevelCache) SetFragment

func (c *MultiLevelCache) SetFragment(id string, fragment *format.Template)

SetFragment sets a template fragment in the cache

func (*MultiLevelCache) SetQueryResult

func (c *MultiLevelCache) SetQueryResult(query string, result interface{})

SetQueryResult sets a query result in the cache

func (*MultiLevelCache) SetTemplate

func (c *MultiLevelCache) SetTemplate(id string, template *format.Template)

SetTemplate sets a template in the cache

type MultiLevelCacheStats

type MultiLevelCacheStats struct {
	// FragmentStats tracks fragment cache statistics
	FragmentStats CacheStats
	// TemplateStats tracks template cache statistics
	TemplateStats CacheStats
	// QueryStats tracks query cache statistics
	QueryStats CacheStats
	// ResultStats tracks result cache statistics
	ResultStats CacheStats
	// TotalHits is the total number of cache hits across all levels
	TotalHits int64
	// TotalMisses is the total number of cache misses across all levels
	TotalMisses int64
	// TotalLookups is the total number of lookups across all levels
	TotalLookups int64
	// HitRatio is the overall hit ratio
	HitRatio float64
}

MultiLevelCacheStats tracks statistics for the multi-level cache

type OptimizedCacheEntry

type OptimizedCacheEntry struct {
	// Template is the cached template
	Template *format.Template
	// CreatedAt is the time the entry was created
	CreatedAt time.Time
	// ExpiresAt is the time the entry expires
	ExpiresAt time.Time
	// Size is an estimate of the template's memory size
	Size int
}

OptimizedCacheEntry represents a cached template with additional metadata

type OptimizedTemplateCache

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

OptimizedTemplateCache is an enhanced template cache with LRU eviction and TTL support

func NewOptimizedTemplateCache

func NewOptimizedTemplateCache(defaultTTL time.Duration, maxSize int) *OptimizedTemplateCache

NewOptimizedTemplateCache creates a new optimized template cache

func (*OptimizedTemplateCache) Clear

func (c *OptimizedTemplateCache) Clear()

Clear clears the cache

func (*OptimizedTemplateCache) Delete

func (c *OptimizedTemplateCache) Delete(id string)

Delete deletes a template from the cache

func (*OptimizedTemplateCache) Get

Get gets a template from the cache

func (*OptimizedTemplateCache) GetKeys

func (c *OptimizedTemplateCache) GetKeys() []string

GetKeys returns the keys of all templates in the cache

func (*OptimizedTemplateCache) GetStats

func (c *OptimizedTemplateCache) GetStats() map[string]interface{}

GetStats returns statistics about the cache

func (*OptimizedTemplateCache) PreloadTemplates

func (c *OptimizedTemplateCache) PreloadTemplates(templates map[string]*format.Template)

PreloadTemplates preloads templates into the cache

func (*OptimizedTemplateCache) Prune

func (c *OptimizedTemplateCache) Prune(maxAge time.Duration) int

Prune removes entries from the cache that are older than the specified duration

func (*OptimizedTemplateCache) Refresh

func (c *OptimizedTemplateCache) Refresh(id string) bool

Refresh refreshes the expiration time of a cache entry

func (*OptimizedTemplateCache) RefreshWithTTL

func (c *OptimizedTemplateCache) RefreshWithTTL(id string, ttl time.Duration) bool

RefreshWithTTL refreshes the expiration time of a cache entry with a specific TTL

func (*OptimizedTemplateCache) Set

func (c *OptimizedTemplateCache) Set(id string, template *format.Template)

Set sets a template in the cache

func (*OptimizedTemplateCache) SetDefaultTTL

func (c *OptimizedTemplateCache) SetDefaultTTL(ttl time.Duration)

SetDefaultTTL sets the default TTL for cache entries

func (*OptimizedTemplateCache) SetMaxSize

func (c *OptimizedTemplateCache) SetMaxSize(maxSize int)

SetMaxSize sets the maximum size of the cache

func (*OptimizedTemplateCache) SetWithTTL

func (c *OptimizedTemplateCache) SetWithTTL(id string, template *format.Template, ttl time.Duration)

SetWithTTL sets a template in the cache with a specific TTL

func (*OptimizedTemplateCache) Size

func (c *OptimizedTemplateCache) Size() int

Size returns the number of templates in the cache

type QueryCache

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

QueryCache is a cache for database query results

func NewQueryCache

func NewQueryCache(defaultTTL time.Duration, maxSize int, enableCompression bool) *QueryCache

NewQueryCache creates a new query cache

func (*QueryCache) Clear

func (c *QueryCache) Clear()

Clear clears the cache

func (*QueryCache) Delete

func (c *QueryCache) Delete(query string)

Delete deletes a query result from the cache

func (*QueryCache) Get

func (c *QueryCache) Get(query string) (interface{}, bool)

Get gets a query result from the cache

func (*QueryCache) GetStats

func (c *QueryCache) GetStats() map[string]interface{}

GetStats returns statistics about the cache

func (*QueryCache) Prune

func (c *QueryCache) Prune(maxAge time.Duration) int

Prune removes entries from the cache that are older than the specified duration

func (*QueryCache) Set

func (c *QueryCache) Set(query string, value interface{})

Set sets a query result in the cache

func (*QueryCache) SetAdaptiveTTL

func (c *QueryCache) SetAdaptiveTTL(enabled bool)

SetAdaptiveTTL sets whether adaptive TTL is enabled

func (*QueryCache) SetAdaptiveTTLRange

func (c *QueryCache) SetAdaptiveTTLRange(min, max time.Duration)

SetAdaptiveTTLRange sets the range for adaptive TTL

func (*QueryCache) SetCompressionEnabled

func (c *QueryCache) SetCompressionEnabled(enabled bool)

SetCompressionEnabled sets whether compression is enabled

func (*QueryCache) SetCompressionLevel

func (c *QueryCache) SetCompressionLevel(level int)

SetCompressionLevel sets the compression level (1-9)

func (*QueryCache) SetDefaultTTL

func (c *QueryCache) SetDefaultTTL(ttl time.Duration)

SetDefaultTTL sets the default TTL for cache entries

func (*QueryCache) SetMaxSize

func (c *QueryCache) SetMaxSize(maxSize int)

SetMaxSize sets the maximum size of the cache

func (*QueryCache) SetWithTTL

func (c *QueryCache) SetWithTTL(query string, value interface{}, ttl time.Duration)

SetWithTTL sets a query result in the cache with a specific TTL

func (*QueryCache) Size

func (c *QueryCache) Size() int

Size returns the number of entries in the cache

type QueryCacheEntry

type QueryCacheEntry struct {
	// Value is the cached query result
	Value interface{}
	// CreatedAt is the time the entry was created
	CreatedAt time.Time
	// ExpiresAt is the time the entry expires
	ExpiresAt time.Time
	// Size is an estimate of the entry's memory size
	Size int
	// AccessCount is the number of times the entry has been accessed
	AccessCount int
	// LastAccessed is the time the entry was last accessed
	LastAccessed time.Time
	// Compressed indicates if the value is compressed
	Compressed bool
}

QueryCacheEntry represents a cached query result with metadata

type ResultCache

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

ResultCache is a cache for template execution results

func NewResultCache

func NewResultCache(defaultTTL time.Duration, maxSize int, enableCompression bool) *ResultCache

NewResultCache creates a new result cache

func (*ResultCache) Clear

func (c *ResultCache) Clear()

Clear clears the cache

func (*ResultCache) Delete

func (c *ResultCache) Delete(key string)

Delete deletes a template execution result from the cache

func (*ResultCache) Get

Get gets a template execution result from the cache

func (*ResultCache) GetStats

func (c *ResultCache) GetStats() map[string]interface{}

GetStats returns statistics about the cache

func (*ResultCache) Prune

func (c *ResultCache) Prune(maxAge time.Duration) int

Prune removes entries from the cache that are older than the specified duration

func (*ResultCache) Set

func (c *ResultCache) Set(key string, result *interfaces.TemplateResult)

Set sets a template execution result in the cache

func (*ResultCache) SetAdaptiveTTL

func (c *ResultCache) SetAdaptiveTTL(enabled bool)

SetAdaptiveTTL sets whether adaptive TTL is enabled

func (*ResultCache) SetAdaptiveTTLRange

func (c *ResultCache) SetAdaptiveTTLRange(min, max time.Duration)

SetAdaptiveTTLRange sets the range for adaptive TTL

func (*ResultCache) SetCompressionEnabled

func (c *ResultCache) SetCompressionEnabled(enabled bool)

SetCompressionEnabled sets whether compression is enabled

func (*ResultCache) SetCompressionLevel

func (c *ResultCache) SetCompressionLevel(level int)

SetCompressionLevel sets the compression level (1-9)

func (*ResultCache) SetDefaultTTL

func (c *ResultCache) SetDefaultTTL(ttl time.Duration)

SetDefaultTTL sets the default TTL for cache entries

func (*ResultCache) SetMaxSize

func (c *ResultCache) SetMaxSize(maxSize int)

SetMaxSize sets the maximum size of the cache

func (*ResultCache) SetWithTTL

func (c *ResultCache) SetWithTTL(key string, result *interfaces.TemplateResult, ttl time.Duration)

SetWithTTL sets a template execution result in the cache with a specific TTL

func (*ResultCache) Size

func (c *ResultCache) Size() int

Size returns the number of entries in the cache

type ResultCacheEntry

type ResultCacheEntry struct {
	// Result is the cached template execution result
	Result *interfaces.TemplateResult
	// CreatedAt is the time the entry was created
	CreatedAt time.Time
	// ExpiresAt is the time the entry expires
	ExpiresAt time.Time
	// Size is an estimate of the entry's memory size
	Size int
	// AccessCount is the number of times the entry has been accessed
	AccessCount int
	// LastAccessed is the time the entry was last accessed
	LastAccessed time.Time
	// Compressed indicates if the result is compressed
	Compressed bool
}

ResultCacheEntry represents a cached template execution result with metadata

type TemplateCache

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

TemplateCache is responsible for caching templates

func NewTemplateCache

func NewTemplateCache(defaultTTL time.Duration, maxSize int, evictionPolicy EvictionPolicy) *TemplateCache

NewTemplateCache creates a new template cache

func (*TemplateCache) Clear

func (c *TemplateCache) Clear()

Clear clears the cache

func (*TemplateCache) Delete

func (c *TemplateCache) Delete(id string)

Delete deletes a template from the cache

func (*TemplateCache) Get

func (c *TemplateCache) Get(id string) (*format.Template, bool)

Get gets a template from the cache

func (*TemplateCache) GetKeys

func (c *TemplateCache) GetKeys() []string

GetKeys returns the keys of all templates in the cache

func (*TemplateCache) GetStats

func (c *TemplateCache) GetStats() map[string]interface{}

GetStats returns statistics about the cache

func (*TemplateCache) Prune

func (c *TemplateCache) Prune(maxAge time.Duration) int

Prune removes entries from the cache that are older than the specified duration

func (*TemplateCache) Refresh

func (c *TemplateCache) Refresh(id string) bool

Refresh refreshes the expiration time of a cache entry

func (*TemplateCache) RefreshWithTTL

func (c *TemplateCache) RefreshWithTTL(id string, ttl time.Duration) bool

RefreshWithTTL refreshes the expiration time of a cache entry with a specific TTL

func (*TemplateCache) Set

func (c *TemplateCache) Set(id string, template *format.Template)

Set sets a template in the cache

func (*TemplateCache) SetDefaultTTL

func (c *TemplateCache) SetDefaultTTL(ttl time.Duration)

SetDefaultTTL sets the default TTL for cache entries

func (*TemplateCache) SetEvictionPolicy

func (c *TemplateCache) SetEvictionPolicy(policy EvictionPolicy)

SetEvictionPolicy sets the eviction policy

func (*TemplateCache) SetMaxSize

func (c *TemplateCache) SetMaxSize(maxSize int)

SetMaxSize sets the maximum size of the cache

func (*TemplateCache) SetWithTTL

func (c *TemplateCache) SetWithTTL(id string, template *format.Template, ttl time.Duration)

SetWithTTL sets a template in the cache with a specific TTL

func (*TemplateCache) Size

func (c *TemplateCache) Size() int

Size returns the number of templates in the cache

type TemplateCacheAdapter

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

TemplateCacheAdapter adapts the TemplateCache to implement the interfaces.TemplateCache interface

func NewTemplateCacheAdapter

func NewTemplateCacheAdapter(cache *TemplateCache) *TemplateCacheAdapter

NewTemplateCacheAdapter creates a new template cache adapter

func (*TemplateCacheAdapter) Clear

func (a *TemplateCacheAdapter) Clear()

Clear clears the cache

func (*TemplateCacheAdapter) Delete

func (a *TemplateCacheAdapter) Delete(id string)

Delete deletes a template from the cache

func (*TemplateCacheAdapter) Get

Get gets a template from the cache

func (*TemplateCacheAdapter) GetStats

func (a *TemplateCacheAdapter) GetStats() map[string]interface{}

GetStats gets cache statistics

func (*TemplateCacheAdapter) Prune

func (a *TemplateCacheAdapter) Prune(maxAge time.Duration) int

Prune removes entries from the cache that are older than the specified duration

func (*TemplateCacheAdapter) Set

func (a *TemplateCacheAdapter) Set(id string, template *format.Template)

Set sets a template in the cache

func (*TemplateCacheAdapter) SetWithTTL

func (a *TemplateCacheAdapter) SetWithTTL(id string, template *format.Template, ttl time.Duration)

SetWithTTL sets a template in the cache with a specific TTL

func (*TemplateCacheAdapter) Size

func (a *TemplateCacheAdapter) Size() int

Size returns the number of templates in the cache

Jump to

Keyboard shortcuts

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