Documentation
¶
Index ¶
Constants ¶
const (
NeverExpire = time.Hour * 24 * 365 * 100
)
NeverExpire represents a duration of 100 years, effectively used to denote a value that should never expire.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EchoCache ¶
type EchoCache[T any] struct { // contains filtered or unexported fields }
EchoCache is a generic caching mechanism that integrates singleflight to prevent redundant computations. EchoCache uses a Cacher interface for data storage and retrieval, supporting custom refresh functions for cache misses. EchoCache ensures only one computation per key occurs simultaneously to optimize concurrent operations.
func NewEchoCache ¶ added in v1.2.0
NewEchoCache creates a new EchoCache instance to enable caching with optional singleflight for concurrent requests.
func (*EchoCache[T]) FetchWithCache ¶ added in v1.2.0
func (ec *EchoCache[T]) FetchWithCache(ctx context.Context, key string, refreshFn store.RefreshFunc[T]) (T, bool, error)
FetchWithCache retrieves a cached value by key or computes it using a given refresh function, caching the result for future use. Returns the value, a boolean indicating if it was found or computed, and an error if computation or retrieval fails.
type EchoCacheLazy ¶ added in v1.2.0
type EchoCacheLazy[T any] struct { // contains filtered or unexported fields }
EchoCacheLazy is a lazy-refresh cache designed to handle stale-while-revalidate caching with generic type support. It queues refresh tasks to be processed later instead of blocking calls for cache updates. It leverages singleflight to prevent duplicate execution of refresh tasks for the same key. Refresh operations are managed with timeout and cancellation support for efficient processing. This type is suitable for scenarios where background cache updates improve application performance.
func NewLazyEchoCache ¶ added in v1.2.0
func NewLazyEchoCache[T any](cacher store.StaleWhileRevalidateCache[T], refreshTimeout time.Duration) *EchoCacheLazy[T]
NewLazyEchoCache initializes a lazy echo cache with a specified stale-while-revalidate cacher and refresh timeout. It starts a background goroutine to handle refresh tasks and returns a pointer to the configured EchoCacheLazy instance.
func (*EchoCacheLazy[T]) FetchWithLazyRefresh ¶ added in v1.2.0
func (ec *EchoCacheLazy[T]) FetchWithLazyRefresh(ctx context.Context, key string, refreshFn store.RefreshFunc[T], lazyRefreshInterval time.Duration) (T, bool, error)
FetchWithLazyRefresh retrieves a cached value or computes a new value if missing, scheduling a lazy refresh if needed. It uses a key to fetch a value from the cache and utilizes a provided function to refresh the value when necessary. If the cached value exists but is older than the lazy refresh interval, a refresh task is sent to the queue. If the value is missing or an error occurs during retrieval, a new value is computed immediately. Returns the cached or computed value, a boolean indicating cache hit, and an error if any.
func (*EchoCacheLazy[T]) ShutdownLazyRefresh ¶ added in v1.2.0
func (ec *EchoCacheLazy[T]) ShutdownLazyRefresh()
ShutdownLazyRefresh gracefully shuts down the refresh process by canceling the context and closing the task queue.