Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,28 @@ added key: 1
added key: 2
```

## Interval
### Expire check interval
```go
func main () {
gc := gcache.New(10).
LRU().
EvictedFunc(func(key, value interface{}) {
fmt.Printf("key: [%v] evicted\n", key)
}).
ExpireCheckInterval(300 * time.Millisecond).
Build()

_ = gc.SetWithExpire(1, 1, time.Second)

time.Sleep(time.Second * 2)
}
```

```
key: [1] evicted
```

# Author

**Jun Kimura**
Expand Down
25 changes: 25 additions & 0 deletions arc.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@ func (c *ARC) init() {
c.t2 = newARCList()
c.b1 = newARCList()
c.b2 = newARCList()

if c.expireCheckInterval != nil && *c.expireCheckInterval != 0 {
go func() {
for range time.Tick(*c.expireCheckInterval) {
for key := range c.items {
now := time.Now()
c.checkAndDeleteExpire(key, &now)
}
}
}()
}
}

func (c *ARC) checkAndDeleteExpire(key interface{}, now *time.Time) {
c.mu.Lock()
defer c.mu.Unlock()

item, ok := c.items[key]
if !ok {
return
}

if item.IsExpired(now) {
c.remove(key)
}
}

func (c *ARC) replace(key interface{}) {
Expand Down
50 changes: 29 additions & 21 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ type Cache interface {
}

type baseCache struct {
clock Clock
size int
loaderExpireFunc LoaderExpireFunc
evictedFunc EvictedFunc
purgeVisitorFunc PurgeVisitorFunc
addedFunc AddedFunc
deserializeFunc DeserializeFunc
serializeFunc SerializeFunc
expiration *time.Duration
mu sync.RWMutex
loadGroup Group
clock Clock
size int
loaderExpireFunc LoaderExpireFunc
evictedFunc EvictedFunc
purgeVisitorFunc PurgeVisitorFunc
addedFunc AddedFunc
deserializeFunc DeserializeFunc
serializeFunc SerializeFunc
expiration *time.Duration
expireCheckInterval *time.Duration
mu sync.RWMutex
loadGroup Group
*stats
}

Expand All @@ -58,16 +59,17 @@ type (
)

type CacheBuilder struct {
clock Clock
tp string
size int
loaderExpireFunc LoaderExpireFunc
evictedFunc EvictedFunc
purgeVisitorFunc PurgeVisitorFunc
addedFunc AddedFunc
expiration *time.Duration
deserializeFunc DeserializeFunc
serializeFunc SerializeFunc
clock Clock
tp string
size int
loaderExpireFunc LoaderExpireFunc
evictedFunc EvictedFunc
purgeVisitorFunc PurgeVisitorFunc
addedFunc AddedFunc
expiration *time.Duration
expireCheckInterval *time.Duration
deserializeFunc DeserializeFunc
serializeFunc SerializeFunc
}

func New(size int) *CacheBuilder {
Expand Down Expand Up @@ -152,6 +154,11 @@ func (cb *CacheBuilder) Expiration(expiration time.Duration) *CacheBuilder {
return cb
}

func (cb *CacheBuilder) ExpireCheckInterval(expireCheckInterval time.Duration) *CacheBuilder {
cb.expireCheckInterval = &expireCheckInterval
return cb
}

func (cb *CacheBuilder) Build() Cache {
if cb.size <= 0 && cb.tp != TYPE_SIMPLE {
panic("gcache: Cache size <= 0")
Expand Down Expand Up @@ -185,6 +192,7 @@ func buildCache(c *baseCache, cb *CacheBuilder) {
c.serializeFunc = cb.serializeFunc
c.evictedFunc = cb.evictedFunc
c.purgeVisitorFunc = cb.purgeVisitorFunc
c.expireCheckInterval = cb.expireCheckInterval
c.stats = &stats{}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/autoloading/autoloading_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ func main() {
gc := gcache.New(10).
LFU().
LoaderFunc(func(key interface{}) (interface{}, error) {
return fmt.Sprintf("%v-value", key), nil
}).
return fmt.Sprintf("%v-value", key), nil
}).
Build()

v, err := gc.Get("key")
Expand Down
21 changes: 21 additions & 0 deletions examples/expireinterval/expire_interval.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
"github.com/bluele/gcache"
"time"
)

func main() {
gc := gcache.New(10).
LRU().
EvictedFunc(func(key, value interface{}) {
fmt.Printf("key: [%v] evicted\n", key)
}).
ExpireCheckInterval(300 * time.Millisecond).
Build()

_ = gc.SetWithExpire(1, 1, time.Second)

time.Sleep(time.Second * 2)
}
25 changes: 25 additions & 0 deletions lfu.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,31 @@ func (c *LFUCache) init() {
freq: 0,
items: make(map[*lfuItem]struct{}),
})

if c.expireCheckInterval != nil && *c.expireCheckInterval != 0 {
go func() {
for range time.Tick(*c.expireCheckInterval) {
for key := range c.items {
now := time.Now()
c.checkAndDeleteExpire(key, &now)
}
}
}()
}
}

func (c *LFUCache) checkAndDeleteExpire(key interface{}, now *time.Time) {
c.mu.Lock()
defer c.mu.Unlock()

item, ok := c.items[key]
if !ok {
return
}

if item.IsExpired(now) {
c.remove(key)
}
}

// Set a new key-value pair
Expand Down
25 changes: 25 additions & 0 deletions lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@ func newLRUCache(cb *CacheBuilder) *LRUCache {
func (c *LRUCache) init() {
c.evictList = list.New()
c.items = make(map[interface{}]*list.Element, c.size+1)

if c.expireCheckInterval != nil && *c.expireCheckInterval != 0 {
go func() {
for range time.Tick(*c.expireCheckInterval) {
for key := range c.items {
now := time.Now()
c.checkAndDeleteExpire(key, &now)
}
}
}()
}
}

func (c *LRUCache) checkAndDeleteExpire(key interface{}, now *time.Time) {
c.mu.Lock()
defer c.mu.Unlock()

item, ok := c.items[key]
if !ok {
return
}

if item.Value.(*lruItem).IsExpired(now) {
c.remove(key)
}
}

func (c *LRUCache) set(key, value interface{}) (interface{}, error) {
Expand Down
25 changes: 25 additions & 0 deletions simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ func (c *SimpleCache) init() {
} else {
c.items = make(map[interface{}]*simpleItem, c.size)
}

if c.expireCheckInterval != nil && *c.expireCheckInterval != 0 {
go func() {
for range time.Tick(*c.expireCheckInterval) {
for key := range c.items {
now := time.Now()
c.checkAndDeleteExpire(key, &now)
}
}
}()
}
}

func (c *SimpleCache) checkAndDeleteExpire(key interface{}, now *time.Time) {
c.mu.Lock()
defer c.mu.Unlock()

item, ok := c.items[key]
if !ok {
return
}

if item.IsExpired(now) {
c.remove(key)
}
}

// Set a new key-value pair
Expand Down