Documentation
¶
Index ¶
- Constants
- Variables
- func GenUUID() string
- type Cache
- func (c *Cache) BackupCacheDumpFolder(backupFolderPath string, zip bool) (err error)
- func (c *Cache) Clear()
- func (c *Cache) DumpToFile() (err error)
- func (c *Cache) Get(itmID string) (value any, ok bool)
- func (c *Cache) GetCacheStats() (cs *CacheStats)
- func (c *Cache) GetGroupItemIDs(grpID string) (itmIDs []string)
- func (c *Cache) GetGroupItems(grpID string) (itms []any)
- func (c *Cache) GetItemExpiryTime(itmID string) (exp time.Time, ok bool)
- func (c *Cache) GetItemIDs(prfx string) (itmIDs []string)
- func (c *Cache) GroupLength(grpID string) int
- func (c *Cache) HasGroup(grpID string) (has bool)
- func (c *Cache) HasItem(itmID string) (has bool)
- func (c *Cache) Len() int
- func (c *Cache) Remove(itmID string)
- func (c *Cache) RemoveGroup(grpID string)
- func (c *Cache) RewriteDumpFiles() error
- func (c *Cache) Set(itmID string, value any, grpIDs []string)
- func (c *Cache) Shutdown() (err error)
- type CacheCloner
- type CacheConfig
- type CacheStats
- type CollectionEntity
- type OfflineCacheEntity
- type OfflineCollector
- type TransCache
- func (tc *TransCache) BackupDumpFolder(backupFolderPath string, zip bool) (err error)
- func (tc *TransCache) BeginTransaction() (transID string)
- func (tc *TransCache) Clear(chIDs []string)
- func (tc *TransCache) CommitTransaction(transID string)
- func (tc *TransCache) DumpAll() (err error)
- func (tc *TransCache) Get(chID, itmID string) (interface{}, bool)
- func (tc *TransCache) GetCacheStats(chIDs []string) (cs map[string]*CacheStats)
- func (tc *TransCache) GetGroupItemIDs(chID, grpID string) (itmIDs []string)
- func (tc *TransCache) GetGroupItems(chID, grpID string) (itms []interface{})
- func (tc *TransCache) GetItemExpiryTime(chID, itmID string) (exp time.Time, ok bool)
- func (tc *TransCache) GetItemIDs(chID, prfx string) (itmIDs []string)
- func (tc *TransCache) HasGroup(chID, grpID string) (has bool)
- func (tc *TransCache) HasItem(chID, itmID string) (has bool)
- func (tc *TransCache) Remove(chID, itmID string, commit bool, transID string)
- func (tc *TransCache) RemoveGroup(chID, grpID string, commit bool, transID string)
- func (tc *TransCache) RewriteAll() (err error)
- func (tc *TransCache) RollbackTransaction(transID string)
- func (tc *TransCache) Set(chID, itmID string, value interface{}, groupIDs []string, commit bool, ...)
- func (tc *TransCache) Shutdown()
- type TransCacheOpts
Constants ¶
const ( UnlimitedCaching = -1 DisabledCaching = 0 )
const ( AddItem = "AddItem" RemoveItem = "RemoveItem" RemoveGroup = "RemoveGroup" DefaultCacheInstance = "*default" )
Variables ¶
var ( ErrNotFound = errors.New("not found") ErrNotClonable = errors.New("not clonable") )
var ErrDumpIntervalDisabled = errors.New("dumpInterval is disabled")
Functions ¶
Types ¶
type Cache ¶
Cache is an LRU/TTL cache. It is safe for concurrent access.
func NewCache ¶
func NewCache(maxEntries int, ttl time.Duration, staticTTL, clone bool, onEvicted []func(itmID string, value any)) (c *Cache)
NewCache initializes a new cache.
func NewCacheFromFolder ¶
func NewCacheFromFolder(offColl *OfflineCollector, maxEntries int, ttl time.Duration, staticTTL, clone bool, onEvicted []func(itmID string, value any)) (cache *Cache, err error)
NewCacheFromFolder construct a new Cache from reading dump files
func (*Cache) BackupCacheDumpFolder ¶
BackupCacheDumpFolder will backup the dump folder of the cache in backupFolderPath. zip true will compress the Cache dump folder and puts it in backupFolderPath (thread safe)
func (*Cache) DumpToFile ¶
DumpToFile dumps to file all of collected cache. (is thread safe)
func (*Cache) GetCacheStats ¶
func (c *Cache) GetCacheStats() (cs *CacheStats)
GetStats will return the CacheStats for this instance
func (*Cache) GetGroupItemIDs ¶
func (*Cache) GetGroupItems ¶
func (*Cache) GetItemExpiryTime ¶
func (*Cache) GetItemIDs ¶
GetItemIDs returns a list of items matching prefix
func (*Cache) GroupLength ¶
GroupLength returns the length of a group
func (*Cache) RemoveGroup ¶
func (*Cache) RewriteDumpFiles ¶
RewriteDumpFiles rewrites dump files of specified c Cache
type CacheCloner ¶
type CacheCloner interface {
CacheClone() any
}
CacheCloner is an interface for objects to clone themselves into interface
type CacheConfig ¶
type CacheStats ¶
type CollectionEntity ¶
type CollectionEntity struct {
IsSet bool // Controls if the item that is collected is a SET or a REMOVE of the item from cache
ItemID string // Holds the cache ItemID
}
CollectionEntity is used to temporarily collect cache keys of the items to be dumped to file
type OfflineCacheEntity ¶
type OfflineCacheEntity struct {
IsSet bool // Controls if the item that is written is a SET or a REMOVE of the item
ItemID string // Holds the cache ItemID to be stored in file
Value any // Value of cache item to be stored in file
GroupIDs []string // GroupIDs of cache item to be stored in file
ExpiryTime time.Time // ExpiryTime of cache item to be stored in file
}
OfflineCacheEntity is used as the structure to be encoded/decoded per cache item to be dumped to file
type OfflineCollector ¶
type OfflineCollector struct {
// contains filtered or unexported fields
}
OfflineCollector used dump cache to files
func NewOfflineCollector ¶
func NewOfflineCollector(cacheName string, opts *TransCacheOpts, logger logger) *OfflineCollector
NewOfflineCollector construct a new OfflineCollector
type TransCache ¶
type TransCache struct {
// contains filtered or unexported fields
}
TransCache is a bigger cache with transactions and multiple Cache instances support
func NewTransCache ¶
func NewTransCache(cfg map[string]*CacheConfig) (tc *TransCache)
NewTransCache instantiates a new TransCache
func NewTransCacheWithOfflineCollector ¶
func NewTransCacheWithOfflineCollector(opts *TransCacheOpts, cfg map[string]*CacheConfig, l logger) (tc *TransCache, err error)
NewTransCacheWithOfflineCollector constructs a new TransCache with OfflineCollector if opts are provided. If not it runs NewTransCache constructor. Cache configuration is taken from cfg and logs will be sent to l logger.
func (*TransCache) BackupDumpFolder ¶
func (tc *TransCache) BackupDumpFolder(backupFolderPath string, zip bool) (err error)
BackupDumpFolder will momentarely stop any dumping and rewriting per Cache until their dump folder is backed up in folder path backupFolderPath, making zip true will create a zip file for each Cache dump in the backupFolderPath instead.
func (*TransCache) BeginTransaction ¶
func (tc *TransCache) BeginTransaction() (transID string)
BeginTransaction initializes a new transaction into transactions buffer
func (*TransCache) Clear ¶
func (tc *TransCache) Clear(chIDs []string)
Remove all items in one or more cache instances
func (*TransCache) CommitTransaction ¶
func (tc *TransCache) CommitTransaction(transID string)
CommitTransaction executes the actions in a transaction buffer
func (*TransCache) DumpAll ¶
func (tc *TransCache) DumpAll() (err error)
DumpAll collected cache in files
func (*TransCache) Get ¶
func (tc *TransCache) Get(chID, itmID string) (interface{}, bool)
Get returns the value of an Item
func (*TransCache) GetCacheStats ¶
func (tc *TransCache) GetCacheStats(chIDs []string) (cs map[string]*CacheStats)
GetCacheStats returns on overview of full cache
func (*TransCache) GetGroupItemIDs ¶
func (tc *TransCache) GetGroupItemIDs(chID, grpID string) (itmIDs []string)
GetGroupItems returns all items in a group. Nil if group does not exist
func (*TransCache) GetGroupItems ¶
func (tc *TransCache) GetGroupItems(chID, grpID string) (itms []interface{})
GetGroupItems returns all items in a group. Nil if group does not exist
func (*TransCache) GetItemExpiryTime ¶
func (tc *TransCache) GetItemExpiryTime(chID, itmID string) (exp time.Time, ok bool)
GetItemExpiryTime returns the expiry time of an item, ok is false if not found
func (*TransCache) GetItemIDs ¶
func (tc *TransCache) GetItemIDs(chID, prfx string) (itmIDs []string)
GetItemIDs returns a list of item IDs matching prefix
func (*TransCache) HasGroup ¶
func (tc *TransCache) HasGroup(chID, grpID string) (has bool)
func (*TransCache) HasItem ¶
func (tc *TransCache) HasItem(chID, itmID string) (has bool)
HasItem verifies if Item is in the cache
func (*TransCache) Remove ¶
func (tc *TransCache) Remove(chID, itmID string, commit bool, transID string)
RempveItem removes an item from the cache
func (*TransCache) RemoveGroup ¶
func (tc *TransCache) RemoveGroup(chID, grpID string, commit bool, transID string)
RemoveGroup removes a group of items out of cache
func (*TransCache) RewriteAll ¶
func (tc *TransCache) RewriteAll() (err error)
RewriteAll will gather all sets and removes from dump files and rewrite a new streamlined file
func (*TransCache) RollbackTransaction ¶
func (tc *TransCache) RollbackTransaction(transID string)
RollbackTransaction destroys a transaction from transactions buffer
func (*TransCache) Set ¶
func (tc *TransCache) Set(chID, itmID string, value interface{}, groupIDs []string, commit bool, transID string)
Set will add/edit an item to the cache
func (*TransCache) Shutdown ¶
func (tc *TransCache) Shutdown()
Shutdown depending on dump and rewrite intervals, will dump all thats left in cache collector to file and/or rewrite files, and close all files
type TransCacheOpts ¶
type TransCacheOpts struct {
DumpPath string // path where TransCache will be dumped
BackupPath string // path where dump files will backup
StartTimeout time.Duration // if time to start TransCache passes this duration, it will stop and return error
DumpInterval time.Duration // dump frequency interval at which cache will be dumped to file (-1 dumps cache as soon as a set/remove is done; 0 disables it)
RewriteInterval time.Duration // rewrite the dump files to streamline them, using RewriteInterval. (-2 rewrites on shutdown, -1 rewrites before start of dumping, 0 disables it).
FileSizeLimit int64 // File size limit in bytes. When limit is passed, it creates a new file where cache will be dumped. (only bigger than 0 allowed)
}
TransCacheOpts holds the options needed to create a TransCache with OfflineCollector