-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package ttl | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
// Cache is a synchronised map of items that auto-expire once stale | ||
type Cache struct { | ||
sync.RWMutex | ||
ttl time.Duration | ||
items map[string]*Item | ||
} | ||
|
||
// NewCache 创建指定生命周期的 Cache | ||
func NewCache(ttl time.Duration) *Cache { | ||
cache := &Cache{ | ||
ttl: ttl, | ||
items: map[string]*Item{}, | ||
} | ||
go cache.gc() // async gc | ||
return cache | ||
} | ||
|
||
func (c *Cache) gc() { | ||
ticker := time.Tick(time.Minute) | ||
for { | ||
<-ticker | ||
c.Lock() | ||
for key, item := range c.items { | ||
if item.expired() { | ||
delete(c.items, key) | ||
} | ||
} | ||
c.Unlock() | ||
} | ||
} | ||
|
||
// Get 通过 key 获取指定的元素 | ||
func (c *Cache) Get(key string) interface{} { | ||
c.RLock() | ||
item, ok := c.items[key] | ||
c.RUnlock() | ||
if ok && item.expired() { | ||
c.Delete(key) | ||
return nil | ||
} | ||
if item == nil { | ||
return nil | ||
} | ||
return item.value | ||
} | ||
|
||
// Set 设置指定 key 的值 | ||
func (c *Cache) Set(key string, val interface{}) { | ||
c.Lock() | ||
defer c.Unlock() | ||
item := &Item{ | ||
exp: time.Now().Add(c.ttl), | ||
value: val, | ||
} | ||
c.items[key] = item | ||
} | ||
|
||
// Delete 删除指定key | ||
func (c *Cache) Delete(key string) { | ||
c.Lock() | ||
defer c.Unlock() | ||
delete(c.items, key) | ||
} | ||
|
||
// Touch 为指定key添加一定生命周期 | ||
func (c *Cache) Touch(key string, ttl time.Duration) { | ||
c.Lock() | ||
defer c.Unlock() | ||
if c.items[key] != nil { | ||
c.items[key].exp = c.items[key].exp.Add(ttl) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package ttl | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGet(t *testing.T) { | ||
cache := NewCache(time.Second) | ||
|
||
data := cache.Get("hello") | ||
assert.Nil(t, data) | ||
|
||
cache.Set("hello", "world") | ||
data = cache.Get("hello") | ||
assert.Equal(t, "world", data) | ||
} | ||
|
||
func TestExpiration(t *testing.T) { | ||
cache := NewCache(time.Second) | ||
|
||
cache.Set("x", "1") | ||
cache.Set("y", "z") | ||
cache.Set("z", "3") | ||
|
||
<-time.After(500 * time.Millisecond) | ||
val := cache.Get("x") | ||
assert.Equal(t, "1", val) | ||
|
||
<-time.After(time.Second) | ||
val = cache.Get("x") | ||
assert.Equal(t, nil, val) | ||
val = cache.Get("y") | ||
assert.Equal(t, nil, val) | ||
val = cache.Get("z") | ||
assert.Equal(t, nil, val) | ||
assert.Equal(t, 0, len(cache.items)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Package ttl provides a synchronised map that auto-expire | ||
// | ||
// This package is simply to provide a storage for session. | ||
package ttl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package ttl | ||
|
||
import "time" | ||
|
||
// Item represents a record in the cache map | ||
type Item struct { | ||
exp time.Time // expired time | ||
value interface{} // value of the item | ||
} | ||
|
||
func (it *Item) expired() bool { | ||
return it.exp.Before(time.Now()) | ||
} |