Documentation ¶
Overview ¶
Package localcache is a stand-alone local K-V cache component that allows concurrent access by multiple goroutines. The cache eliminates elements based on LRU and expiration time strategies.
Index ¶
- Variables
- func Clear()
- func Del(key string)
- func Get(key string) (interface{}, bool)
- func GetWithLoad(ctx context.Context, key string, load LoadFunc, ttl int64) (interface{}, error)
- func Set(key string, value interface{}, ttl int64) bool
- type Cache
- type CachedStatus
- type Item
- type ItemCallBackFunc
- type ItemFlag
- type LoadFunc
- type MLoadFunc
- type Option
- func WithCapacity(capacity int) Option
- func WithDelay(duration int64) Option
- func WithExpiration(ttl int64) Option
- func WithLoad(f LoadFunc) Option
- func WithMLoad(f MLoadFunc) Option
- func WithOnDel(delCallBack ItemCallBackFunc) Option
- func WithOnExpire(expireCallback ItemCallBackFunc) Option
- func WithSettingTimeout(t time.Duration) Option
- func WithSyncDelFlag(flag bool) Option
Constants ¶
This section is empty.
Variables ¶
var ErrCacheExpire = errors.New("cache exist, but expired")
ErrCacheExpire the cache exists but has expired
Functions ¶
func Get ¶
Get returns the value corresponding to key, bool returns true. If the key does not exist or expires, bool returns false
func GetWithLoad ¶
GetWithLoad returns the value corresponding to the key. If the key does not exist, use the load function to load the return and cache it for ttl seconds.
Types ¶
type Cache ¶
type Cache interface { // Get returns the value corresponding to key, bool returns true. // If the key does not exist or expires, bool returns false Get(key string) (interface{}, bool) // GetWithStatus returns the value corresponding to the key and returns the cache status GetWithStatus(key string) (interface{}, CachedStatus) // GetWithLoad returns the value corresponding to the key. // If the key does not exist, use the user-defined loading function to obtain the data and cache it. GetWithLoad(ctx context.Context, key string) (interface{}, error) // MGetWithLoad returns values corresponding to multiple keys. // When some keys do not exist, use a custom batch loading function to obtain data and cache it // For a key that does not exist in the cache and does not exist in the calling result of the mLoad function, // the return result of MGetWithLoad includes the key and the corresponding value is nil. MGetWithLoad(ctx context.Context, keys []string) (map[string]interface{}, error) // GetWithCustomLoad returns the value corresponding to the key. // If the key does not exist, the passed in load function is used to load and cache the ttl time. // If the load function does not exist, err will be returned. // If you do not need to pass in the load function every time you get it, please use the option method // to set the load in the new cache, and use the GetWithLoad method to obtain the cache value. GetWithCustomLoad(ctx context.Context, key string, customLoad LoadFunc, ttl int64) (interface{}, error) // MGetWithCustomLoad returns values corresponding to multiple keys. // When some keys do not exist, the passed in load function is used to load and cache the ttl time. // For a key that does not exist in the cache and does not exist in the calling result of the mLoad function, // the return result of MGetWithLoad includes the key and the corresponding value is nil. // If the load function does not exist, err will be returned. // If you do not need to pass in the load function every time you get it, please use the option method to set // the load in the new cache, and use the MGetWithLoad method to obtain the cache value. MGetWithCustomLoad(ctx context.Context, keys []string, customLoad MLoadFunc, ttl int64) (map[string]interface{}, error) // Set key and value Set(key string, value interface{}) bool // SetWithExpire sets key, value, and sets different ttl (expiration time in seconds) for different keys SetWithExpire(key string, value interface{}, ttl int64) bool // Del delete key Del(key string) // Len key quantity Len() int // Clear clears all queues and caches Clear() // Close Close cache Close() }
Cache is a local K-V memory store that supports expiration time
type CachedStatus ¶
type CachedStatus int
CachedStatus cache status
const ( // CacheNotExist cache data does not exist CacheNotExist CachedStatus = 1 // CacheExist cache data exists CacheExist CachedStatus = 2 // CacheExpire cache data exists but has expired. You can choose whether to use it CacheExpire CachedStatus = 3 )
type ItemCallBackFunc ¶
type ItemCallBackFunc func(*Item)
ItemCallBackFunc callback function triggered when the element expires/deletes
type LoadFunc ¶
LoadFunc loads the value data corresponding to the key and is used to fill the cache
type Option ¶
type Option func(*cache)
Option parameter tool function
func WithCapacity ¶
WithCapacity sets the maximum number of keys
func WithExpiration ¶
WithExpiration sets the expiration time of the element (in seconds). If the expiration time is not set, the default is 60 seconds.
func WithOnDel ¶
func WithOnDel(delCallBack ItemCallBackFunc) Option
WithOnDel sets the callback function when the element is deleted
func WithOnExpire ¶
func WithOnExpire(expireCallback ItemCallBackFunc) Option
WithOnExpire sets the callback function triggered when the element expires
func WithSettingTimeout ¶
WithSettingTimeout causes elements to be written to the store synchronously, and a timeout needs to be set.
func WithSyncDelFlag ¶
WithSyncDelFlag sets the synchronous method to delete elements in the store when deleting elements.
Directories ¶
Path | Synopsis |
---|---|
examples
|
|
Package mocklocalcache is a generated GoMock package.
|
Package mocklocalcache is a generated GoMock package. |