mirror of https://github.com/rjbasitali/go-cache
cache eviction using sweeper
parent
de2865c97f
commit
8142b23bcb
|
|
@ -0,0 +1,15 @@
|
|||
package gocache
|
||||
|
||||
import "time"
|
||||
|
||||
func (cache *myCache) deleteExpired() {
|
||||
cache.mutex.Lock()
|
||||
defer cache.mutex.Unlock()
|
||||
|
||||
now := time.Now().UnixNano()
|
||||
for k, v := range cache.items {
|
||||
if v.expiration > 0 && now > v.expiration {
|
||||
delete(cache.items, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
package gocache
|
||||
|
||||
func (cache *myCache) Flush() {
|
||||
cache.mutex.Lock()
|
||||
defer cache.mutex.Unlock()
|
||||
|
||||
cache.items = make(map[string]*value)
|
||||
}
|
||||
18
my_cache.go
18
my_cache.go
|
|
@ -1,12 +1,15 @@
|
|||
package gocache
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type myCache struct {
|
||||
mutex sync.Mutex
|
||||
items map[string]*value
|
||||
expireAfter int64
|
||||
}
|
||||
|
||||
func NewCache() Cache {
|
||||
|
|
@ -14,3 +17,18 @@ func NewCache() Cache {
|
|||
items: make(map[string]*value),
|
||||
}
|
||||
}
|
||||
|
||||
func NewCacheWithSweeper(interval, expireAfter time.Duration) Cache {
|
||||
c := &myCache{
|
||||
items: make(map[string]*value),
|
||||
expireAfter: expireAfter.Nanoseconds(),
|
||||
}
|
||||
|
||||
if interval > 0 && expireAfter > 0 {
|
||||
s := newSweeper(c, interval)
|
||||
runtime.SetFinalizer(c, s.stopSweeper)
|
||||
go s.run(c)
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
|
|
|||
2
set.go
2
set.go
|
|
@ -4,7 +4,7 @@ func (cache *myCache) Set(key string, data interface{}) error {
|
|||
cache.mutex.Lock()
|
||||
defer cache.mutex.Unlock()
|
||||
|
||||
value := newValue(key, data)
|
||||
value := newValue(key, data, cache.expireAfter)
|
||||
cache.items[key] = value
|
||||
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
package gocache
|
||||
|
||||
import "time"
|
||||
|
||||
type sweeper struct {
|
||||
Interval time.Duration
|
||||
stop chan bool
|
||||
}
|
||||
|
||||
func (s *sweeper) run(c *myCache) {
|
||||
ticker := time.NewTicker(s.Interval)
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
c.deleteExpired()
|
||||
case <-s.stop:
|
||||
ticker.Stop()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newSweeper(c *myCache, i time.Duration) *sweeper {
|
||||
return &sweeper{
|
||||
Interval: i,
|
||||
stop: make(chan bool),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *sweeper) stopSweeper() {
|
||||
s.stop <- true
|
||||
}
|
||||
6
value.go
6
value.go
|
|
@ -1,13 +1,17 @@
|
|||
package gocache
|
||||
|
||||
import "time"
|
||||
|
||||
type value struct {
|
||||
key string
|
||||
data interface{}
|
||||
expiration int64
|
||||
}
|
||||
|
||||
func newValue(key string, data interface{}) *value {
|
||||
func newValue(key string, data interface{}, expiration int64) *value {
|
||||
return &value{
|
||||
key: key,
|
||||
data: data,
|
||||
expiration: time.Now().UnixNano() + expiration,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue