cache

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: MIT Imports: 8 Imported by: 0

README

LoadableCache

能够定时自动刷新内存中的数据,常用于 常量、枚举、变更频率较小的数据。

场景

  • 常量/枚举
  • 变更频率较小的数据
  • 白山App信息
  • 白山服务类型信息
  • etc..

从数据库加载并缓存到内存中,避免每次都从数据库中查询

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LoadableCache

type LoadableCache[K comparable, V any] interface {
	// Get a value pair to the cache data by key.
	Get(ctx context.Context, k K) (V, error)
	// GetALL returns all key-value pairs in the cache data.
	GetALL(context.Context) map[K]V
	// Values returns all values in the cache data.
	Values(context.Context) []V
	// Set a value pair to the cache data by key.
	Set(ctx context.Context, k K, v V) error
	// Purge clears all cache data.
	Purge(context.Context)
	// TryPurgeAndReload try to refresh cache data, if refresh result is nil, return false.
	TryPurgeAndReload(context.Context) bool
}

func New

func New[K comparable, V any](opts ...Option[K, V]) LoadableCache[K, V]

type Option

type Option[K comparable, V any] func(*loadableCache[K, V])

func WithBlock

func WithBlock[K comparable, V any]() Option[K, V]

WithBlock block call first RefreshAfterWrite.

func WithExpiration

func WithExpiration[K comparable, V any](exp time.Duration) Option[K, V]

WithExpiration cache expiration, Automatically reload if timeout

func WithRefreshAfterWrite

func WithRefreshAfterWrite[K comparable, V any](f func() map[K]V) Option[K, V]

WithRefreshAfterWrite refresh data provider

func WithSize

func WithSize[K comparable, V any](size int) Option[K, V]

WithSize cache size limit.

func WithTracing

func WithTracing[K comparable, V any](provider oteltrace.TracerProvider) Option[K, V]

WithTracing enable otel tracing

Jump to

Keyboard shortcuts

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