Documentation
¶
Index ¶
- Constants
- type CallbackFunc
- type Element
- type MemoryCache
- func (c *MemoryCache[K, V]) Clear()
- func (c *MemoryCache[K, V]) Delete(key K) (exist bool)
- func (c *MemoryCache[K, V]) Get(key K) (v V, exist bool)
- func (c *MemoryCache[K, V]) GetOrCreate(key K, value V, exp time.Duration) (v V, exist bool)
- func (c *MemoryCache[K, V]) GetOrCreateWithCallback(key K, value V, exp time.Duration, cb CallbackFunc[*Element[K, V]]) (v V, exist bool)
- func (c *MemoryCache[K, V]) GetTTL(key K) (time.Time, bool)
- func (c *MemoryCache[K, V]) GetWithTTL(key K, exp time.Duration) (v V, exist bool)
- func (c *MemoryCache[K, V]) Len() int
- func (c *MemoryCache[K, V]) Range(f func(K, V) bool)
- func (c *MemoryCache[K, V]) Set(key K, value V, exp time.Duration) (exist bool)
- func (c *MemoryCache[K, V]) SetWithCallback(key K, value V, exp time.Duration, cb CallbackFunc[*Element[K, V]]) (exist bool)
- func (c *MemoryCache[K, V]) Stop()
- func (c *MemoryCache[K, V]) UpdateTTL(key K, d time.Duration) bool
- type Option
- type Reason
Constants ¶
const ( ReasonExpired = Reason(0) // 过期 ReasonEvicted = Reason(1) // 被驱逐 ReasonDeleted = Reason(2) // 被删除 ReasonCleared = Reason(3) // 被清空 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CallbackFunc ¶ added in v1.1.3
type Element ¶ added in v1.1.3
type Element[K comparable, V any] struct { // 键 Key K // 值 Value V // 过期时间, 毫秒 ExpireAt int64 // contains filtered or unexported fields }
type MemoryCache ¶
type MemoryCache[K comparable, V any] struct { // contains filtered or unexported fields }
func New ¶
func New[K comparable, V any](options ...Option) *MemoryCache[K, V]
New 创建缓存数据库实例 Creating a Cached Database Instance
func (*MemoryCache[K, V]) Clear ¶ added in v1.1.2
func (c *MemoryCache[K, V]) Clear()
Clear 清空缓存. 对存活非过期元素触发 ReasonCleared 回调. Clear caches. Triggers ReasonCleared callback for alive non-expired elements.
func (*MemoryCache[K, V]) Delete ¶
func (c *MemoryCache[K, V]) Delete(key K) (exist bool)
Delete 删除缓存 delete cache
func (*MemoryCache[K, V]) Get ¶
func (c *MemoryCache[K, V]) Get(key K) (v V, exist bool)
Get 查询缓存 query cache
func (*MemoryCache[K, V]) GetOrCreate ¶ added in v1.1.5
func (c *MemoryCache[K, V]) GetOrCreate(key K, value V, exp time.Duration) (v V, exist bool)
GetOrCreate 如果存在, 返回已有值(忽略 value 参数)并刷新过期时间. 如果不存在, 创建一个新的. Get or create a value. If it exists, returns the existing value (ignoring the value parameter) and refreshes the expiration time. If it does not exist, creates a new one.
func (*MemoryCache[K, V]) GetOrCreateWithCallback ¶ added in v1.1.5
func (c *MemoryCache[K, V]) GetOrCreateWithCallback(key K, value V, exp time.Duration, cb CallbackFunc[*Element[K, V]]) (v V, exist bool)
GetOrCreateWithCallback 如果存在, 返回已有值(忽略 value 和 cb 参数)并刷新过期时间. 如果不存在, 创建一个新的. 回调默认可通过 WithAsyncCallback 配置; 异步模式下 Element 为快照. Get or create a value with CallbackFunc. If it exists, returns the existing value (ignoring the value and cb parameters) and refreshes the expiration time. If it does not exist, creates a new one. Callback dispatch is configurable via WithAsyncCallback; in async mode the Element is a snapshot.
func (*MemoryCache[K, V]) GetTTL ¶ added in v1.2.3
func (c *MemoryCache[K, V]) GetTTL(key K) (time.Time, bool)
GetTTL 获取 key 的过期时间 Get the expiration time of the key.
func (*MemoryCache[K, V]) GetWithTTL ¶ added in v1.1.3
func (c *MemoryCache[K, V]) GetWithTTL(key K, exp time.Duration) (v V, exist bool)
GetWithTTL 获取. 如果存在, 刷新过期时间. Get a value. If it exists, refreshes the expiration time.
func (*MemoryCache[K, V]) Len ¶
func (c *MemoryCache[K, V]) Len() int
Len 快速获取当前缓存元素数量, 不做过期检查. 注意: 返回值可能包含已过期但未清除的元素. Quickly gets the current number of cached elements, without checking for expiration. Note: the returned count may include expired elements that have not been cleaned up yet.
func (*MemoryCache[K, V]) Range ¶ added in v1.2.0
func (c *MemoryCache[K, V]) Range(f func(K, V) bool)
Range 遍历缓存 注意: 不要在回调函数里面操作 MemoryCache[K, V] 实例, 可能会造成死锁. Traverse the cache. Note: Do not manipulate MemoryCache[K, V] instances inside callback functions, as this may cause deadlocks.
func (*MemoryCache[K, V]) Set ¶
func (c *MemoryCache[K, V]) Set(key K, value V, exp time.Duration) (exist bool)
Set 设置键值和过期时间. exp<=0表示永不过期. Set the key value and expiration time. exp<=0 means never expire.
func (*MemoryCache[K, V]) SetWithCallback ¶ added in v1.1.3
func (c *MemoryCache[K, V]) SetWithCallback(key K, value V, exp time.Duration, cb CallbackFunc[*Element[K, V]]) (exist bool)
SetWithCallback 设置键值, 过期时间和回调函数. 容量溢出和过期都会触发回调. 默认异步执行; 同步模式下回调在桶锁内执行, 收到的 Element 为活跃引用, 勿在回调中操作 MemoryCache 以免死锁. Set the key value, expiration time and callback function. Callbacks are triggered on capacity overflow or expiration. Async by default; in sync mode the callback runs under the bucket lock with a live Element reference — do not call back into MemoryCache to avoid deadlocks.
func (*MemoryCache[K, V]) Stop ¶ added in v1.1.5
func (c *MemoryCache[K, V]) Stop()
Stop 停止后台协程并排空待执行回调. Stop 之后不应再使用该实例; 若仍发生删除/清空等操作, 异步回调会降级为同步执行, 避免静默丢失. Stop background goroutines and drain pending callbacks. Do not use the instance after Stop; if mutations still occur, async callbacks fall back to synchronous execution to avoid silent loss.
type Option ¶ added in v1.0.1
type Option func(c *config)
func WithAsyncCallback ¶ added in v1.2.3
WithAsyncCallback 是否异步执行回调, 默认为true Whether to dispatch callbacks asynchronously, true by default.
func WithBucketNum ¶ added in v1.1.0
WithBucketNum 设置存储桶数量 Setting the number of storage buckets
func WithBucketSize ¶ added in v1.1.0
WithBucketSize 设置初始化大小和最大容量. 超过最大容量会被清除. (单个存储桶) Set the initial size and maximum capacity. Exceeding the maximum capacity will be erased. (Single bucket)
func WithCachedTime ¶ added in v1.2.0
WithCachedTime 是否开启时间缓存 Whether to turn on time caching
func WithDeleteLimits ¶ added in v1.2.0
WithDeleteLimits 设置每次TTL检查最大删除key数量. (单个存储桶) Set the maximum number of keys to be deleted per TTL check (single bucket)
func WithInterval ¶ added in v1.1.0
WithInterval 设置TTL检查周期 设置过期时间检查周期. 如果过期元素较少, 取最大值, 反之取最小值. Setting the TTL check period If the number of expired elements is small, take the maximum value, otherwise take the minimum value.
func WithSwissTable ¶ added in v1.2.0
WithSwissTable 使用swiss table替代runtime map Using swiss table instead of runtime map