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
|
package gocache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type myCache struct {
|
type myCache struct {
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
items map[string]*value
|
items map[string]*value
|
||||||
|
expireAfter int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCache() Cache {
|
func NewCache() Cache {
|
||||||
|
|
@ -14,3 +17,18 @@ func NewCache() Cache {
|
||||||
items: make(map[string]*value),
|
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()
|
cache.mutex.Lock()
|
||||||
defer cache.mutex.Unlock()
|
defer cache.mutex.Unlock()
|
||||||
|
|
||||||
value := newValue(key, data)
|
value := newValue(key, data, cache.expireAfter)
|
||||||
cache.items[key] = value
|
cache.items[key] = value
|
||||||
|
|
||||||
return nil
|
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
|
package gocache
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
type value struct {
|
type value struct {
|
||||||
key string
|
key string
|
||||||
data interface{}
|
data interface{}
|
||||||
|
expiration int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func newValue(key string, data interface{}) *value {
|
func newValue(key string, data interface{}, expiration int64) *value {
|
||||||
return &value{
|
return &value{
|
||||||
key: key,
|
key: key,
|
||||||
data: data,
|
data: data,
|
||||||
|
expiration: time.Now().UnixNano() + expiration,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue