From 7e7bd69e43e4e578aaad69318c6d464a8db87149 Mon Sep 17 00:00:00 2001 From: lxzan Date: Mon, 25 Dec 2023 10:26:07 +0800 Subject: [PATCH] update docs --- README.md | 12 +++++++++--- README_CN.md | 14 ++++++++++---- cache.go | 15 ++++++++++----- options.go | 2 ++ 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 382c9d0..b5e64cf 100644 --- a/README.md +++ b/README.md @@ -70,9 +70,15 @@ import ( func main() { mc := memorycache.New[string, any]( - memorycache.WithBucketNum(128), // Bucket number, recommended to be a prime number. - memorycache.WithBucketSize(1000, 10000), // Bucket size, initial size and maximum capacity. - memorycache.WithInterval(5*time.Second, 30*time.Second), // Active cycle cleanup interval and expiration time. + // Set the number of storage buckets, y=pow(2,x) + memorycache.WithBucketNum(128), + + // Set bucket size, initial size and maximum capacity (single bucket) + memorycache.WithBucketSize(1000, 10000), + + // Set the expiration time check period. + // If the number of expired elements is small, take the maximum value, otherwise take the minimum value. + memorycache.WithInterval(5*time.Second, 30*time.Second), ) mc.SetWithCallback("xxx", 1, time.Second, func(element *memorycache.Element[string, any], reason memorycache.Reason) { diff --git a/README_CN.md b/README_CN.md index f42dd1a..46d1ea8 100644 --- a/README_CN.md +++ b/README_CN.md @@ -56,15 +56,21 @@ package main import ( "fmt" - "github.com/lxzan/memorycache" "time" + + "github.com/lxzan/memorycache" ) func main() { mc := memorycache.New[string, any]( - memorycache.WithBucketNum(128), // Bucket number, recommended to be a prime number. - memorycache.WithBucketSize(1000, 10000), // Bucket size, initial size and maximum capacity. - memorycache.WithInterval(5*time.Second, 30*time.Second), // Active cycle cleanup interval and expiration time. + // 设置存储桶数量, y=pow(2,x) + memorycache.WithBucketNum(128), + + // 设置单个存储桶的初始化容量和最大容量 + memorycache.WithBucketSize(1000, 10000), + + // 设置过期时间检查周期. 如果过期元素较少, 取最大值, 反之取最小值. + memorycache.WithInterval(5*time.Second, 30*time.Second), ) mc.SetWithCallback("xxx", 1, time.Second, func(element *memorycache.Element[string, any], reason memorycache.Reason) { diff --git a/cache.go b/cache.go index a0a231f..5c8ead5 100644 --- a/cache.go +++ b/cache.go @@ -66,9 +66,12 @@ func New[K comparable, V any](options ...Option) *MemoryCache[K, V] { } // 删除数量超过阈值, 缩小时间间隔 - if d1 := utils.SelectValue(sum > conf.BucketNum*conf.DeleteLimits*7/10, conf.MinInterval, conf.MaxInterval); d1 != d0 { - d0 = d1 - ticker.Reset(d0) + if sum > 0 { + d1 := utils.SelectValue(sum > conf.BucketNum*conf.DeleteLimits*7/10, conf.MinInterval, conf.MaxInterval) + if d1 != d0 { + d0 = d1 + ticker.Reset(d0) + } } } } @@ -260,8 +263,10 @@ func (c *MemoryCache[K, V]) Delete(key K) (exist bool) { return false } -// Range 遍历缓存. 注意: 不要在回调函数里面操作 MemoryCache[K, V] 实例, 可能会造成死锁. -// Traverse the cache. Note: Do not manipulate MemoryCache[K, V] instances inside callback functions, as this may cause deadlocks. +// Range 遍历缓存 +// 注意: 不要在回调函数里面操作 MemoryCache[K, V] 实例, 可能会造成死锁. +// Traverse the cache. +// Note: Do not manipulate MemoryCache[K, V] instances inside callback functions, as this may cause deadlocks. func (c *MemoryCache[K, V]) Range(f func(K, V) bool) { var now = time.Now().UnixMilli() for _, b := range c.storage { diff --git a/options.go b/options.go index ce4bb28..cacedee 100644 --- a/options.go +++ b/options.go @@ -43,7 +43,9 @@ func WithBucketSize(size, cap int) Option { } // 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 WithInterval(min, max time.Duration) Option { return func(c *config) { c.MinInterval = min