Documentation ¶
Index ¶
- Variables
- func CreateConvertorToString[Val any]() func(Val) string
- type Cache
- type CacheBase
- func (c *CacheBase[Data, Key]) Close()
- func (c *CacheBase[Data, Key]) Get(key Key, periodStart, periodEnd time.Time, extra interface{}) ([]Data, error)
- func (c *CacheBase[Data, Key]) GetCached(key Key, periodStart, periodEnd time.Time) ([]Data, bool, error)
- func (c *CacheBase[Data, Key]) GetCachedAll(key Key, ...) (CacheStateSegment[Data], bool, error)
- func (c *CacheBase[Data, Key]) GetCachedPeriodClosestFromEnd(key Key, point time.Time, nonEmpty bool) (*TimePeriodBounds[Data], error)
- func (c *CacheBase[Data, Key]) GetCachedPeriodClosestFromStart(key Key, point time.Time, nonEmpty bool) (*TimePeriodBounds[Data], error)
- type CacheBaseOptions
- type CacheData
- type CacheSource
- type CacheState
- type CacheStateSegment
- type CacheStorage
- type FileCache
- type FileCacheOptions
- type LoadCaheFunc
- type MemoryAndSqliteCache
- type MemoryAndSqliteCacheOptions
- type MemoryCache
- type MemoryCacheOptions
- type MemoryCacheState
- type PeriodBounds
- type SaveCacheFunc
- type SqliteCache
- type SqliteCacheOptions
- type TimePeriodBounds
Constants ¶
This section is empty.
Variables ¶
var IdxCmp = time.Time.Compare
Functions ¶
func CreateConvertorToString ¶
Types ¶
type Cache ¶
type Cache[Data any, Key any] interface { Get(key Key, periodStart, periodEnd time.Time, extra interface{}) ([]Data, error) GetCached(key Key, periodStart, periodEnd time.Time) ([]Data, bool, error) GetCachedAll(key Key, requiredPeriodStart, requiredPeriodEnd, minPeriodStart, maxPeriodEnd time.Time) (CacheStateSegment[Data], bool, error) GetCachedPeriodClosestFromStart(key Key, point time.Time, nonEmpty bool) (*TimePeriodBounds[Data], error) GetCachedPeriodClosestFromEnd(key Key, point time.Time, nonEmpty bool) (*TimePeriodBounds[Data], error) Close() }
type CacheBase ¶
func NewCacheBase ¶
func NewCacheBase[Data any, Key any](opts CacheBaseOptions[Data, Key]) *CacheBase[Data, Key]
func (*CacheBase[Data, Key]) Get ¶
func (c *CacheBase[Data, Key]) Get(key Key, periodStart, periodEnd time.Time, extra interface{}) ([]Data, error)
Retrieve data for specified period. If data is not found in cache, it will be loaded from source. Parameter extra is passed to GetFromSource function.
func (*CacheBase[Data, Key]) GetCached ¶
func (c *CacheBase[Data, Key]) GetCached(key Key, periodStart, periodEnd time.Time) ([]Data, bool, error)
Retrieve data from cache for specified period without loading it from source.
The error may be returned due to failure of loading data from persistent storage. If Load function is not configured, then error is never returned. Error is never returned due to loading from source, because it never happens.
func (*CacheBase[Data, Key]) GetCachedAll ¶
func (c *CacheBase[Data, Key]) GetCachedAll(key Key, requiredPeriodStart, requiredPeriodEnd, minPeriodStart, maxPeriodEnd time.Time) (CacheStateSegment[Data], bool, error)
Retrieve data on the longest possible period, containing requested period. Period [requiredPeriodStart, requiredPeriodEnd] is required to be in the result. Period [minPeriodStart, maxPeriodEnd] is desired to be in the result, but not required. No entries will be returned outside of this period.
The error may be returned due to failure of loading data from persistent storage. If Load function is not configured, then error is never returned. Error is never returned due to loading from source, because it never happens.
func (*CacheBase[Data, Key]) GetCachedPeriodClosestFromEnd ¶
func (c *CacheBase[Data, Key]) GetCachedPeriodClosestFromEnd(key Key, point time.Time, nonEmpty bool) (*TimePeriodBounds[Data], error)
Retrieves period boundaries (and edge entries) of a cached period, closest to the specified point from the end. If nonEmpty is true, then only non-empty periods are considered. If point is inside of some period, that period is returned. If no period is found, nil is returned.
func (*CacheBase[Data, Key]) GetCachedPeriodClosestFromStart ¶
func (c *CacheBase[Data, Key]) GetCachedPeriodClosestFromStart(key Key, point time.Time, nonEmpty bool) (*TimePeriodBounds[Data], error)
Retrieves period boundaries (and edge entries) of a cached period, closest to the specified point from the start. If nonEmpty is true, then only non-empty periods are considered. If point is inside of some period, that period is returned. If no period is found, nil is returned.
type CacheBaseOptions ¶
type CacheBaseOptions[Data any, Key any] struct { KeyToStr func(Key) string GetTimestamp func(d *Data) time.Time GetFromSource CacheSource[Data, Key] Storage CacheStorage[Data, Key] SkipDataVerification bool }
type CacheSource ¶
type CacheState ¶
type CacheStateSegment ¶
type CacheStorage ¶
type CacheStorage[Data any, Key any] interface { Load(key Key) (*CacheState[Data], error) Save(key Key, state *CacheState[Data], updated []*CacheStateSegment[Data]) error Add(key Key, periodStart, periodEnd time.Time, data []Data) (CacheData[Data], error) }
type FileCache ¶
type FileCache[Data any, Key any] struct { MemoryCache[Data, Key] // contains filtered or unexported fields }
func NewFileCache ¶
func NewFileCache[Data any, Key any](opts FileCacheOptions[Data, Key]) (*FileCache[Data, Key], error)
type FileCacheOptions ¶
type LoadCaheFunc ¶
type LoadCaheFunc[Data any, Key any] func(key Key) (*MemoryCacheState[Data], error)
Load cache from persistent storage
type MemoryAndSqliteCache ¶
type MemoryAndSqliteCache[Data any, Key any, ID comparable] struct { MemoryCache[Data, Key] // contains filtered or unexported fields }
func NewMemoryAndSqliteCache ¶
func NewMemoryAndSqliteCache[Data any, Key any, ID comparable](opts MemoryAndSqliteCacheOptions[Data, Key, ID]) (*MemoryAndSqliteCache[Data, Key, ID], error)
type MemoryAndSqliteCacheOptions ¶
type MemoryAndSqliteCacheOptions[Data any, Key any, ID comparable] struct { SqliteCacheOptions[Data, Key, ID] MinFirstLayerFetchPeriod time.Duration }
type MemoryCache ¶
func NewMemoryCache ¶
func NewMemoryCache[Data any, Key any](opts MemoryCacheOptions[Data, Key]) *MemoryCache[Data, Key]
type MemoryCacheOptions ¶
type MemoryCacheOptions[Data any, Key any] struct { GetTimestamp func(d *Data) time.Time // (Required) GetFromSource CacheSource[Data, Key] // (Required) Fetch data from source KeyToStr func(Key) string // (Optional) Save SaveCacheFunc[Data, Key] // (Optional) Save to persistent storage Load LoadCaheFunc[Data, Key] // (Optional) Load from persistent storage SkipDataVerification bool // (Optional) Set this to true if you are sure your data is well-ordered }
type MemoryCacheState ¶
type MemoryCacheState[Data any] struct { Segments []*CacheStateSegment[Data] }
type PeriodBounds ¶
type SaveCacheFunc ¶
type SaveCacheFunc[Data any, Key any] func(key Key, state *CacheState[Data], updated []*CacheStateSegment[Data]) error
Save cache to persistent storage
type SqliteCache ¶
type SqliteCache[Data any, Key any, ID comparable] struct { CacheBase[Data, Key] }
func NewSqliteCache ¶
func NewSqliteCache[Data any, Key any, ID comparable](opts SqliteCacheOptions[Data, Key, ID]) (*SqliteCache[Data, Key, ID], error)
type SqliteCacheOptions ¶
type SqliteCacheOptions[Data any, Key any, ID comparable] struct { CacheDir string // (Required) Directory to store cache files GetTimestamp func(d *Data) time.Time // (Required) Get timestamp of data entry GetID func(d *Data) ID // (Required) Get ID of data entry. Needed to properly order data in DB if timestamp is not unique. GetFromSource CacheSource[Data, Key] // (Required) Fetch data from source KeyToStr func(Key) string // (Optional) Convert key to string SkipDataVerification bool }