multilevel

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package multilevel provides a multi-level cache (L1 memory + L2 Redis) with cache warming, stampede protection, and promotion between levels.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheWarmer

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

CacheWarmer pre-populates the cache with data at startup.

func NewCacheWarmer

func NewCacheWarmer(cache *MultiLevelCache) *CacheWarmer

NewCacheWarmer creates a new cache warmer.

func (*CacheWarmer) Warmup

func (cw *CacheWarmer) Warmup(ctx context.Context, data map[string]any) error

Warmup pre-populates the cache with the given data. Uses default TTL of 1 hour.

func (*CacheWarmer) WarmupByKeys

func (cw *CacheWarmer) WarmupByKeys(ctx context.Context, keys []string, valueFn func(string) any) error

WarmupByKeys pre-populates cache with keys from a slice. Useful for warming up specific frequently-accessed keys.

func (*CacheWarmer) WarmupWithTTL

func (cw *CacheWarmer) WarmupWithTTL(ctx context.Context, data map[string]any, ttl time.Duration) error

WarmupWithTTL pre-populates the cache with the given data and custom TTL.

type MultiLevelCache

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

MultiLevelCache implements port.CachePort using L1 + L2 caching strategy

func NewMultiLevelCache

func NewMultiLevelCache(l1, l2 port.CachePort) *MultiLevelCache

NewMultiLevelCache creates a coordinated L1+L2 cache

func NewMultiLevelCacheWithTTL

func NewMultiLevelCacheWithTTL(l1, l2 port.CachePort, l1TTL, l2TTL, promotionTTL time.Duration) *MultiLevelCache

NewMultiLevelCacheWithTTL creates a coordinated L1+L2 cache with custom TTLs

func (*MultiLevelCache) Clear

func (mlc *MultiLevelCache) Clear(ctx context.Context) error

Clear removes from both L1 and L2

func (*MultiLevelCache) Delete

func (mlc *MultiLevelCache) Delete(ctx context.Context, key string) error

Delete removes from both L1 and L2

func (*MultiLevelCache) Get

func (mlc *MultiLevelCache) Get(ctx context.Context, key string) (any, error)

Get retrieves a value with L1->L2 fallback and promotion

func (*MultiLevelCache) GetPromotionCount

func (mlc *MultiLevelCache) GetPromotionCount() int64

GetPromotionCount returns number of L2->L1 promotions

func (*MultiLevelCache) GetPromotionTTL

func (mlc *MultiLevelCache) GetPromotionTTL() time.Duration

GetPromotionTTL returns the current promotion TTL

func (*MultiLevelCache) Set

func (mlc *MultiLevelCache) Set(ctx context.Context, key string, value any, ttl time.Duration) error

Set stores a value in both L1 and L2

func (*MultiLevelCache) SetPromotionTTL

func (mlc *MultiLevelCache) SetPromotionTTL(ttl time.Duration)

SetPromotionTTL updates the TTL for promoted items

func (*MultiLevelCache) Stats

func (mlc *MultiLevelCache) Stats() *port.CacheStats

Stats aggregates statistics from both levels

type StampedeProtection

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

StampedeProtection implements single-flight pattern to prevent cache stampede. Multiple concurrent requests for the same key will result in only one fetch operation, with all other requesters waiting for and sharing the result.

func NewStampedeProtection

func NewStampedeProtection() *StampedeProtection

NewStampedeProtection creates a new stampede protection instance.

func (*StampedeProtection) Do

func (sp *StampedeProtection) Do(key string, fn func() (any, error)) (any, error)

Do executes the function fn for the given key, ensuring that only one execution is in-flight for any given key at a time. Concurrent calls with the same key will wait for the first call to complete and share its result.

This implements the single-flight pattern to prevent cache stampede.

type TTLManager

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

TTLManager manages time-to-live for cache entries.

func NewTTLManager

func NewTTLManager() *TTLManager

NewTTLManager creates a new TTL manager.

func (*TTLManager) CleanupExpired

func (tm *TTLManager) CleanupExpired() int

CleanupExpired removes all expired entries from the TTL manager. Returns the number of entries cleaned up.

func (*TTLManager) Delete

func (tm *TTLManager) Delete(key string)

Delete removes a key from TTL tracking.

func (*TTLManager) GetRemaining

func (tm *TTLManager) GetRemaining(key string) time.Duration

GetRemaining returns the remaining TTL for a key. Returns 0 if the key has expired or doesn't exist.

func (*TTLManager) IsExpired

func (tm *TTLManager) IsExpired(key string) bool

IsExpired checks if a key has expired. Returns true if the key has expired or doesn't exist.

func (*TTLManager) Set

func (tm *TTLManager) Set(key string, ttl time.Duration)

Set sets the TTL for a key. The key will expire after the specified duration.

Jump to

Keyboard shortcuts

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