Documentation
¶
Overview ¶
Package bigcache encapsulates a low garbage collection (GC) consuming local cache library named BigCache.
Index ¶
- Constants
- func InitDefaultConfig(cfg Config) bigcache.Config
- func ToInterface(s string, to interface{}) (interface{}, error)
- func ToString(i interface{}) (string, error)
- type BigCache
- func (c *BigCache) Capacity() int
- func (c *BigCache) Close() error
- func (c *BigCache) Delete(key string) error
- func (c *BigCache) Get(key string, val interface{}, serializationType int) (err error)
- func (c *BigCache) GetBytes(key string) ([]byte, error)
- func (c *BigCache) GetBytesWithInfo(key string) ([]byte, bigcache.Response, error)
- func (c *BigCache) GetWithInfo(key string, val interface{}, serializationType int) (bigcache.Response, error)
- func (c *BigCache) Iterator() *bigcache.EntryInfoIterator
- func (c *BigCache) KeyMetadata(key string) bigcache.Metadata
- func (c *BigCache) Len() int
- func (c *BigCache) Reset() error
- func (c *BigCache) Set(key string, val interface{}, serializationType ...int) error
- func (c *BigCache) Stats() bigcache.Stats
- type Config
- type Option
- func WithCleanWindow(t time.Duration) Option
- func WithHardMaxCacheSize(m int) Option
- func WithHasher(h bigcache.Hasher) Option
- func WithLifeWindow(t time.Duration) Option
- func WithLogger(l bigcache.Logger) Option
- func WithMaxEntriesInWindow(i int) Option
- func WithMaxEntrySize(i int) Option
- func WithOnRemoveCallback(f func(key string, entry []byte)) Option
- func WithOnRemoveWithMetadataCallback(f func(key string, entry []byte, keyMetadata bigcache.Metadata)) Option
- func WithOnRemoveWithReasonCallback(f func(key string, entry []byte, reason bigcache.RemoveReason)) Option
- func WithShards(i int) Option
- func WithStatsEnabled(s bool) Option
- func WithVerbose(v bool) Option
Constants ¶
const (
// ErrEntryNotFound entry not found
ErrEntryNotFound = -1
)
Variables ¶
This section is empty.
Functions ¶
func InitDefaultConfig ¶
InitDefaultConfig initializes the config used by the third-party library BigCache. You can directly modify the bigcache.Config if necessary.
func ToInterface ¶
ToInterface converts a string type to the corresponding interface types such as bool, float, int, uint, error, etc.
Types ¶
type BigCache ¶
type BigCache struct {
// contains filtered or unexported fields
}
BigCache is the local cache struct, encapsulating the corresponding struct of third-party library.
func New ¶
New initializes a BigCache with default parameters. If the initialization fails, it returns an error, which is then handled by the business layer.
func (*BigCache) Close ¶
Close sends a shutdown signal, exits the clean coroutine, and ensures that it can be garbage collected.
func (*BigCache) Get ¶
Get gets the value by the given key. If the key does not exist, it returns ErrEntryNotFound. The value is automatically parsed using the specified serialization type.
func (*BigCache) GetBytes ¶
GetBytes gets the raw bytes by the given key. If the key does not exist, it returns ErrEntryNotFound.
func (*BigCache) GetBytesWithInfo ¶
GetBytesWithInfo gets the raw bytes by the given key. If the key does not exist, it returns ErrEntryNotFound, along with additional information indicating whether the entry has expired.
func (*BigCache) GetWithInfo ¶
func (c *BigCache) GetWithInfo(key string, val interface{}, serializationType int) (bigcache.Response, error)
GetWithInfo gets the value by the key. If the entry has been removed or expired, it returns ErrEntryNotFound, along with additional information indicating whether the entry has expired. The value is automatically parsed using the specified serialization type.
func (*BigCache) Iterator ¶
func (c *BigCache) Iterator() *bigcache.EntryInfoIterator
Iterator returns an iterator that can traverse the entire cache.
func (*BigCache) KeyMetadata ¶
KeyMetadata returns the hit count for the key.
type Config ¶
type Config struct { // Shards is the number of shards, must be a power of 2, such as 2, 4, 8, 16, 32, etc. Shards int // LifeWindow is the life window of data in the cache. LifeWindow time.Duration // CleanWindow is the window between each cleanup of expired data, set to a value less than 0 to disable cleanup. // Default value is 1 second. CleanWindow time.Duration // MaxEntriesInWindow is the maximum number of data entries, only used when initializing cache shards. MaxEntriesInWindow int // MaxEntrySize is the maximum size of a value in bytes, only used when initializing cache shards. MaxEntrySize int // StatsEnabled enables tracking of hit count for individual keys when set to true. StatsEnabled bool // Verbose enables output of memory allocation information when set to true. Verbose bool // HardMaxCacheSize is the maximum cache size in MB. // It's set to prevent excessive memory allocation and avoid OOM errors, // and it only takes effect when set to a value greater than 0. HardMaxCacheSize int Logger bigcache.Logger // OnRemove is a callback fired when the oldest entry is removed because of its expiration time or no space left // for the new entry, or because delete was called. // Default value is nil which means no callback and it prevents from unwrapping the oldest entry. // ignored if OnRemoveWithMetadata is specified. OnRemove func(key string, entry []byte) // OnRemoveWithMetadata is a callback fired when the oldest entry is removed because of its expiration time // or no space left for the new entry, or because delete was called. // A structure representing details about that specific entry. // Default value is nil which means no callback and it prevents from unwrapping the oldest entry. OnRemoveWithMetadata func(key string, entry []byte, keyMetadata bigcache.Metadata) // OnRemoveWithReason is a callback fired when the oldest entry is removed because of its expiration time // or no space left for the new entry, or because delete was called. // A constant representing the reason will be passed through. // Default value is nil which means no callback and it prevents from unwrapping the oldest entry. // Ignored if OnRemove is specified. OnRemoveWithReason func(key string, entry []byte, reason bigcache.RemoveReason) // Hasher used to map between string keys and unsigned 64bit integers, by default fnv64 hashing is used. Hasher bigcache.Hasher }
Config is a configuration parameter that can be used to initialize BigCache.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig initializes the default values for the main config parameters. The default life window is set to 7 days, and the default cleaning window is set to 1 minute.
type Option ¶
type Option func(*Config)
Option declares the option functions of the cache
func WithCleanWindow ¶
WithCleanWindow sets the clean window
func WithHardMaxCacheSize ¶
WithHardMaxCacheSize sets the maximum cache size in MB. It's set to prevent excessive memory allocation and avoid OOM errors, and it only takes effect when set to a value greater than 0.
func WithLifeWindow ¶
WithLifeWindow sets the life window
func WithMaxEntriesInWindow ¶
WithMaxEntriesInWindow sets the maximum number of data entries, used only during initialization, and affects the minimum memory usage.
func WithMaxEntrySize ¶
WithMaxEntrySize sets the maximum size of a value in bytes, used only during initialization, and affects the minimum memory usage.
func WithOnRemoveCallback ¶
WithOnRemoveCallback sets OnRemove callback function
func WithOnRemoveWithMetadataCallback ¶
func WithOnRemoveWithMetadataCallback(f func(key string, entry []byte, keyMetadata bigcache.Metadata)) Option
WithOnRemoveWithMetadataCallback sets OnRemoveWithMetadata callback function
func WithOnRemoveWithReasonCallback ¶
func WithOnRemoveWithReasonCallback(f func(key string, entry []byte, reason bigcache.RemoveReason)) Option
WithOnRemoveWithReasonCallback sets OnRemoveWithReason callback function
func WithStatsEnabled ¶
WithStatsEnabled sets whether to enable tracking of hit count for individual keys.
func WithVerbose ¶
WithVerbose sets whether to enable output of memory allocation information.