mirror of https://github.com/rjbasitali/go-cache
parent
d0a7fc26e1
commit
9b5a155a44
|
|
@ -8,6 +8,9 @@ func (cache *myCache) deleteExpired() {
|
|||
|
||||
now := time.Now().UnixNano()
|
||||
for k, v := range cache.items {
|
||||
if cache.decisionFunc != nil && !cache.decisionFunc(v) {
|
||||
continue
|
||||
}
|
||||
if v.expiration > 0 && now > v.expiration {
|
||||
delete(cache.items, k)
|
||||
}
|
||||
|
|
|
|||
14
my_cache.go
14
my_cache.go
|
|
@ -7,9 +7,10 @@ import (
|
|||
)
|
||||
|
||||
type myCache struct {
|
||||
mutex sync.Mutex
|
||||
items map[string]*value
|
||||
expireAfter int64
|
||||
mutex sync.Mutex
|
||||
items map[string]*value
|
||||
expireAfter int64
|
||||
decisionFunc func(v interface{}) bool
|
||||
}
|
||||
|
||||
func NewCache() Cache {
|
||||
|
|
@ -18,10 +19,11 @@ func NewCache() Cache {
|
|||
}
|
||||
}
|
||||
|
||||
func NewCacheWithSweeper(interval, expireAfter time.Duration) Cache {
|
||||
func NewCacheWithSweeper(interval, expireAfter time.Duration, decisionFunc func(v interface{}) bool) Cache {
|
||||
c := &myCache{
|
||||
items: make(map[string]*value),
|
||||
expireAfter: expireAfter.Nanoseconds(),
|
||||
items: make(map[string]*value),
|
||||
expireAfter: expireAfter.Nanoseconds(),
|
||||
decisionFunc: decisionFunc,
|
||||
}
|
||||
|
||||
if interval > 0 && expireAfter > 0 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue