Skip to content

Commit f476a73

Browse files
author
lixizan
committed
update doc
1 parent 546ae4d commit f476a73

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
func main() {
1616
mc := memorycache.New(
1717
memorycache.WithSegment(16),
18-
memorycache.WithTTLCheckInterval(100*time.Millisecond),
18+
memorycache.WithTTLCheckInterval(30*time.Second),
1919
)
2020
mc.Set("xxx", 1, 500*time.Millisecond)
2121
time.Sleep(time.Second)

config.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,28 @@ const (
99

1010
type (
1111
Config struct {
12-
TTLCheckInterval time.Duration // 过期检查周期
13-
Segment uint32 // 分片数, segment=2^n, eg: 4, 8, 16...
12+
// 过期时间检查周期
13+
// expiration time inspection cycle
14+
TTLCheckInterval time.Duration //
15+
16+
// 分片数, segment=2^n, eg: 4, 8, 16...
17+
// number of hashmap pieces, eg: 4, 8, 16...
18+
Segment uint32
1419
}
1520

1621
Option func(c *Config)
1722
)
1823

24+
// WithSegment
25+
// Set Segment
1926
func WithSegment(segment uint32) Option {
2027
return func(c *Config) {
2128
c.Segment = segment
2229
}
2330
}
2431

32+
// WithTTLCheckInterval
33+
// Set TTLCheckInterval
2534
func WithTTLCheckInterval(TTLCheckInterval time.Duration) Option {
2635
return func(c *Config) {
2736
c.TTLCheckInterval = TTLCheckInterval

index.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ func (c *MemoryCache) getExpireTimestamp(expiration time.Duration) int64 {
3434
return time.Now().Add(expiration).UnixNano() / 1000000
3535
}
3636

37+
// Set
3738
// 设置键值和过期时间
3839
// expiration: <=0表示永不过期
40+
// set the key value and expiration time
41+
// expiration: <=0 means never expire
3942
func (c *MemoryCache) Set(key string, value interface{}, expiration time.Duration) {
4043
var ele = types.Element{
4144
Value: value,
@@ -54,7 +57,9 @@ func (c *MemoryCache) Set(key string, value interface{}, expiration time.Duratio
5457
bucket.Unlock()
5558
}
5659

60+
// Get
5761
// 根据键获取值
62+
// get value by key
5863
func (c *MemoryCache) Get(key string) (interface{}, bool) {
5964
var bucket = c.storage.GetBucket(key)
6065
bucket.RLock()
@@ -66,7 +71,9 @@ func (c *MemoryCache) Get(key string) (interface{}, bool) {
6671
return result.Value, true
6772
}
6873

74+
// Delete
6975
// 删除一个键
76+
// delete value by key
7077
func (c *MemoryCache) Delete(key string) {
7178
var bucket = c.storage.GetBucket(key)
7279
bucket.Lock()
@@ -91,7 +98,9 @@ func (c *MemoryCache) Expire(key string, expiration time.Duration) {
9198
bucket.Unlock()
9299
}
93100

94-
// 获取所有键
101+
// Keys
102+
// 获取所有有效的键
103+
// list valid keys
95104
func (c *MemoryCache) Keys() []string {
96105
var arr = make([]string, 0)
97106
var now = utils.Timestamp()
@@ -107,7 +116,9 @@ func (c *MemoryCache) Keys() []string {
107116
return arr
108117
}
109118

119+
// Len
110120
// 获取有效元素个数
121+
// get the number of valid keys
111122
func (c *MemoryCache) Len() int {
112123
var num = 0
113124
var now = utils.Timestamp()

index_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func TestNew(t *testing.T) {
1010
var as = assert.New(t)
1111

1212
t.Run("", func(t *testing.T) {
13-
var db = New(WithTTLCheckInterval(100 * time.Millisecond))
13+
var db = New()
1414
db.Set("a", 1, time.Second)
1515
db.Set("b", 1, 3*time.Second)
1616
db.Set("c", 1, 5*time.Second)
@@ -23,7 +23,7 @@ func TestNew(t *testing.T) {
2323
})
2424

2525
t.Run("", func(t *testing.T) {
26-
var db = New(WithTTLCheckInterval(100 * time.Millisecond))
26+
var db = New()
2727
db.Set("a", 1, time.Second)
2828
db.Set("b", 1, 2*time.Second)
2929
db.Set("c", 1, 5*time.Second)
@@ -36,7 +36,7 @@ func TestNew(t *testing.T) {
3636
})
3737

3838
t.Run("", func(t *testing.T) {
39-
var db = New(WithTTLCheckInterval(100 * time.Millisecond))
39+
var db = New()
4040
db.Set("a", 1, time.Second)
4141
db.Set("b", 1, 2*time.Second)
4242
db.Set("c", 1, 4*time.Second)

0 commit comments

Comments
 (0)