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]) 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()
- type Option
- type Reason
Constants ¶
const ( ReasonExpired = Reason(0) // 过期 ReasonEvicted = Reason(1) // 被驱逐 ReasonDeleted = Reason(2) // 被删除 )
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 清空缓存 clear caches
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 如果存在, 刷新过期时间. 如果不存在, 创建一个新的. Get or create a value. If it exists, 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 如果存在, 刷新过期时间. 如果不存在, 创建一个新的. Get or create a value with CallbackFunc. If it exists, refreshes the expiration time. If it does not exist, creates a new one.
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.
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 设置键值, 过期时间和回调函数. 容量溢出和过期都会触发回调. Set the key value, expiration time and callback function. The callback is triggered by both capacity overflow and expiration.
func (*MemoryCache[K, V]) Stop ¶ added in v1.1.5
func (c *MemoryCache[K, V]) Stop()
type Option ¶ added in v1.0.1
type Option func(c *config)
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