Documentation
¶
Index ¶
Constants ¶
const ( // NoExpiration mark cached item never expire. NoExpiration = -2 * time.Second // DefaultExpiration use the default expiration time set when the cache was created. // Equivalent to passing in the same e duration as was given to NewCache() or NewCacheDefault(). DefaultExpiration = -1 * time.Second // DefaultCleanupInterval the default time interval for automatically cleaning up expired key-value pairs DefaultCleanupInterval = 10 * time.Second )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
// Set add item to the cache, replacing any existing items.
// (DefaultExpiration), the item uses a cached default expiration time.
// (NoExpiration), the item never expires.
// All values less than or equal to 0 are the same except DefaultExpiration, which means never expires.
Set(k string, v interface{}, d time.Duration)
// SetDefault add item to the cache with the default expiration time, replacing any existing items.
SetDefault(k string, v interface{})
// SetForever add item to cache and set to never expire, replacing any existing items.
SetForever(k string, v interface{})
// Get an item from the cache.
// Returns the item or nil,
// and a boolean indicating whether the key was found.
Get(k string) (interface{}, bool)
// GetWithExpiration get an item from the cache.
// Returns the item or nil,
// along with the expiration time, and a boolean indicating whether the key was found.
GetWithExpiration(k string) (interface{}, time.Time, bool)
// GetWithTTL get an item from the cache.
// Returns the item or nil,
// with the remaining lifetime and a boolean indicating whether the key was found.
GetWithTTL(k string) (interface{}, time.Duration, bool)
// GetOrSet returns the existing value for the key if present.
// Otherwise, it stores and returns the given value.
// The loaded result is true if the value was loaded, false if stored.
GetOrSet(k string, v interface{}, d time.Duration) (interface{}, bool)
// GetAndSet returns the existing value for the key if present,
// while setting the new value for the key.
// Otherwise, it stores and returns the given value.
// The loaded result is true if the value was loaded, false otherwise.
GetAndSet(k string, v interface{}, d time.Duration) (interface{}, bool)
// GetAndRefresh Get an item from the cache, and refresh the item's expiration time.
// Returns the item or nil,
// and a boolean indicating whether the key was found.
// Allows getting keys that have expired but not been evicted.
// Not atomic synchronization.
GetAndRefresh(k string, d time.Duration) (interface{}, bool)
// GetAndDelete Get an item from the cache, and delete the key.
// Returns the item or nil,
// and a boolean indicating whether the key was found.
GetAndDelete(k string) (interface{}, bool)
// Delete an item from the cache.
// Does nothing if the key is not in the cache.
Delete(k string)
// DeleteExpired delete all expired items from the cache.
DeleteExpired()
// Range calls f sequentially for each key and value present in the map.
// If f returns false, range stops the iteration.
Range(f func(k string, v interface{}) bool)
// Items return the items in the cache.
// This is a snapshot, which may include items that are about to expire.
Items() map[string]interface{}
// Count returns the number of items in the cache.
// This may include items that have expired but have not been cleaned up.
Count() int
}
func NewDefault ¶
func NewDefault( defaultExpiration, cleanupInterval time.Duration, evictedCallback ...EvictedCallback, ) Cache
func NewXsyncMap ¶
NewXsyncMap create a new cache, optionally specifying configuration items.
func NewXsyncMapDefault ¶
func NewXsyncMapDefault(defaultExpiration, cleanupInterval time.Duration, evictedCallback ...EvictedCallback) Cache
NewXsyncMapDefault creates a new cache with the given default expiration duration and cleanup interval. If the cleanup interval is less than 1, the cleanup needs to be performed manually, calling c.DeleteExpired()
type CacheOf ¶
type CacheOf[V any] interface { // Set add item to the cache, replacing any existing items. // (DefaultExpiration), the item uses a cached default expiration time. // (NoExpiration), the item never expires. // All values less than or equal to 0 are the same except DefaultExpiration, which means never expires. Set(k string, v V, d time.Duration) // SetDefault add item to the cache with the default expiration time, replacing any existing items. SetDefault(k string, v V) // SetForever add item to cache and set to never expire, replacing any existing items. SetForever(k string, v V) // Get an item from the cache. // Returns the item or nil, // and a boolean indicating whether the key was found. Get(k string) (V, bool) // GetWithExpiration get an item from the cache. // Returns the item or nil, // along with the expiration time, and a boolean indicating whether the key was found. GetWithExpiration(k string) (V, time.Time, bool) // GetWithTTL get an item from the cache. // Returns the item or nil, // with the remaining lifetime and a boolean indicating whether the key was found. GetWithTTL(k string) (V, time.Duration, bool) // GetOrSet returns the existing value for the key if present. // Otherwise, it stores and returns the given value. // The loaded result is true if the value was loaded, false if stored. GetOrSet(k string, v V, d time.Duration) (V, bool) // GetAndSet returns the existing value for the key if present, // while setting the new value for the key. // Otherwise, it stores and returns the given value. // The loaded result is true if the value was loaded, false otherwise. GetAndSet(k string, v V, d time.Duration) (V, bool) // GetAndRefresh Get an item from the cache, and refresh the item's expiration time. // Returns the item or nil, // and a boolean indicating whether the key was found. // Allows getting keys that have expired but not been evicted. // Not atomic synchronization. GetAndRefresh(k string, d time.Duration) (V, bool) // GetAndDelete Get an item from the cache, and delete the key. // Returns the item or nil, // and a boolean indicating whether the key was found. GetAndDelete(k string) (V, bool) // Delete an item from the cache. // Does nothing if the key is not in the cache. Delete(k string) // DeleteExpired delete all expired items from the cache. DeleteExpired() // Range calls f sequentially for each key and value present in the map. // If f returns false, range stops the iteration. Range(f func(k string, v V) bool) // Items return the items in the cache. // This is a snapshot, which may include items that are about to expire. Items() map[string]V // Count returns the number of items in the cache. // This may include items that have expired but have not been cleaned up. Count() int }
func NewOfDefault ¶
func NewOfDefault[V any]( defaultExpiration, cleanupInterval time.Duration, evictedCallback ...EvictedCallbackOf[V], ) CacheOf[V]
func NewXsyncMapOf ¶
NewXsyncMapOf create a new cache, optionally specifying configuration items.
func NewXsyncMapOfDefault ¶
func NewXsyncMapOfDefault[V any]( defaultExpiration, cleanupInterval time.Duration, evictedCallback ...EvictedCallbackOf[V], ) CacheOf[V]
NewXsyncMapOfDefault creates a new cache with the given default expiration duration and cleanup interval. If the cleanup interval is less than 1, the cleanup needs to be performed manually, calling c.DeleteExpired()
type Config ¶
type Config struct {
// DefaultExpiration default expiration time for key-value pairs.
DefaultExpiration time.Duration
// CleanupInterval the interval at which expired key-value pairs are automatically cleaned up.
CleanupInterval time.Duration
// EvictedCallback executed when the key-value pair expires.
EvictedCallback EvictedCallback
}
func DefaultConfig ¶
func DefaultConfig() Config
type ConfigOf ¶
type ConfigOf[V any] struct { // DefaultExpiration default expiration time for key-value pairs. DefaultExpiration time.Duration // CleanupInterval the interval at which expired key-value pairs are automatically cleaned up. CleanupInterval time.Duration // EvictedCallback executed when the key-value pair expires. EvictedCallback EvictedCallbackOf[V] }
func DefaultConfigOf ¶
type EvictedCallback ¶
type EvictedCallback func(k string, v interface{})
EvictedCallback callback function to execute when the key-value pair expires and is evicted. Warning: cannot block, it is recommended to use goroutine.
type EvictedCallbackOf ¶
EvictedCallbackOf callback function to execute when the key-value pair expires and is evicted. Warning: cannot block, it is recommended to use goroutine.
type Option ¶
type Option func(config *Config)
func WithCleanupInterval ¶
func WithDefaultExpiration ¶
func WithEvictedCallback ¶
func WithEvictedCallback(ec EvictedCallback) Option
type OptionOf ¶
func WithDefaultExpirationOf ¶
func WithEvictedCallbackOf ¶
func WithEvictedCallbackOf[V any](ec EvictedCallbackOf[V]) OptionOf[V]
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
generic
command
|
|
|
generic/with-options
command
|
|
|
normal
command
|
|
|
normal/with-options
command
|
|
|
internal
|
|