datacache

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MilliToNanoSecond millisecond to nanosecond ratio
	MilliToNanoSecond = int64(time.Millisecond / time.Nanosecond)

	// DefaultRetryInterval default retry interval
	DefaultRetryInterval int64 = 1000000000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheInitOption

type CacheInitOption[T any] struct {
	WithInitData    bool
	InitData        T
	LoadData        LoadDataCallback[T]
	Expiration      int64 // data expiration, in milliseconds
	ExpirationOnErr int64 // data expiration when err occur, will not cache if not set, in milliseconds
	RetryInterval   int64 // retry interval for load data if failed, in milliseconds. If 0, use 1 second
	WaitTimeout     int64 // waiting timeout for first data, in milliseconds
}

CacheInitOption initiation params of DataCache

type CacheItem

type CacheItem[V any] struct {
	// contains filtered or unexported fields
}

CacheItem is basic unit of cache

type CacheMap

type CacheMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

CacheMap is a collection for CacheItems

func NewCacheMap

func NewCacheMap[K comparable, V any](opt *CacheMapOption[K, V]) (c *CacheMap[K, V])

NewCacheMap creates new data cache by DataCacheMapOption The data updating is async, the Get method will return old data while updating in progress Only one goroutine will try to load the data during one RetryInterval First Get method call will trigger data loading, all Get requests will wait for first data until WaitTimeout The data will be loaded again if exceed Expiration since last load, and will be removed from cache if exceed EvictTimeout since last access.

func (*CacheMap[K, V]) Get

func (c *CacheMap[K, V]) Get(key K) V

Get gets data by key from CacheMap Return zero value if no valid data Note the returned data is reference, any modification in returned data will affect the subsequent returned data

func (*CacheMap[K, V]) GetCopy

func (c *CacheMap[K, V]) GetCopy(key K, v V) error

GetCopy gets deep copy of data by key from CacheMap

func (*CacheMap[K, V]) GetMany

func (c *CacheMap[K, V]) GetMany(keys []K) map[K]V

GetMany gets data by key from CacheMap Return zero value if no valid data Note the returned data is reference, any modification in returned data will affect the subsequent returned data

type CacheMapOption

type CacheMapOption[K comparable, V any] struct {
	LoadData        LoadDataByKeyCallback[K, V]
	LoadDataBatch   LoadDataBatchByKeysCallback[K, V] // optional, if not provided, fallback to LoadData
	Expiration      int64                             // data update interval, in milliseconds
	ExpirationOnErr int64                             // data update interval when err occur, will not cache if not set
	EvictTimeout    int64                             // timeout for evict data from cache, in milliseconds. If 0, use Expiration * 2
	RetryInterval   int64                             // retry interval for load data if failed, in milliseconds. If 0, use 1 second
	WaitTimeout     int64                             // waiting timeout for first data, in milliseconds
}

CacheMapOption has all options of a CacheMap

type DataCache

type DataCache[T any] struct {
	// contains filtered or unexported fields
}

DataCache a thread/goroutine safe in memory cache storage.

func NewDataCache

func NewDataCache[T any](opt *CacheInitOption[T]) (c *DataCache[T])

NewDataCache create new DataCache with a CacheInitOption The data updating is synchronized, the Get method will return old data while updating in progress Only one go routine will try to load the data during one RetryInterval If didn't provide init data, first Get method call will trigger data loading, and all Get requests will wait for first data until WaitTimeout

func (*DataCache[T]) Get

func (c *DataCache[T]) Get() T

Get data from DataCache Return zero value if no valid data Note the returned data is reference, any modification in returned data will affect the subsequent returned data

func (*DataCache[T]) GetCopy

func (c *DataCache[T]) GetCopy(v T) error

GetCopy get deep copy of data from DataCache

type LoadDataBatchByKeysCallback

type LoadDataBatchByKeysCallback[K comparable, V any] func(keys []K) (map[K]V, error)

LoadDataBatchByKeysCallback is a call back to load batch data from source

type LoadDataByKeyCallback

type LoadDataByKeyCallback[K comparable, V any] func(key K) (V, error)

LoadDataByKeyCallback is a call back to load data from source

type LoadDataCallback

type LoadDataCallback[T any] func() (T, error)

LoadDataCallback a call back function used to load data from remote.

Jump to

Keyboard shortcuts

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