Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lxzan committed Dec 25, 2023
1 parent f752915 commit 7e7bd69
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 10 additions & 4 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 10 additions & 5 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7e7bd69

Please sign in to comment.