Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #15 #16

Merged
merged 1 commit into from
Feb 25, 2019
Merged
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
9 changes: 4 additions & 5 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Cache struct {
priorityQueue *priorityQueue
expirationNotification chan bool
expirationTime time.Time
skipTtlExtension bool
skipTtlExtension bool
}

func (cache *Cache) getItem(key string) (*item, bool) {
Expand Down Expand Up @@ -56,7 +56,7 @@ func (cache *Cache) startExpirationProcessing() {
cache.mutex.Lock()
if cache.priorityQueue.Len() > 0 {
sleepTime = cache.priorityQueue.items[0].expireAt.Sub(time.Now())
if sleepTime < 0 && cache.priorityQueue.items[0].expireAt.IsZero(){
if sleepTime < 0 && cache.priorityQueue.items[0].expireAt.IsZero() {
sleepTime = time.Hour
} else if sleepTime < 0 {
sleepTime = time.Microsecond
Expand Down Expand Up @@ -88,9 +88,7 @@ func (cache *Cache) startExpirationProcessing() {

if cache.checkExpireCallback != nil {
if !cache.checkExpireCallback(item.key, item.data) {
if !cache.skipTtlExtension {
item.touch()
}
item.touch()
cache.priorityQueue.update(item)
i++
if i == cache.priorityQueue.Len() {
Expand Down Expand Up @@ -220,6 +218,7 @@ func (cache *Cache) SetCheckExpirationCallback(callback checkExpireCallback) {
func (cache *Cache) SetNewItemCallback(callback expireCallback) {
cache.newItemCallback = callback
}

// SkipTtlExtensionOnHit allows the user to change the cache behaviour. When this flag is set to true it will
// no longer extend TTL of items when they are retrieved using Get, or when their expiration condition is evaluated
// using SetCheckExpirationCallback.
Expand Down
6 changes: 4 additions & 2 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"time"

"fmt"
"github.com/stretchr/testify/assert"
"log"
"sync"

"github.com/stretchr/testify/assert"
)

// test for Feature request in issue #12
Expand All @@ -29,7 +30,7 @@ func TestCache_SkipTtlExtensionOnHit(t *testing.T) {
cache.SkipTtlExtensionOnHit(true)
cache.Set("expireTest", "!")
// will loop if item does not expire
for _, found := cache.Get("expireTest"); found; _, found = cache.Get("expireTest"){
for _, found := cache.Get("expireTest"); found; _, found = cache.Get("expireTest") {
}
}

Expand Down Expand Up @@ -232,6 +233,7 @@ func TestCacheCheckExpirationCallbackFunction(t *testing.T) {
var lock sync.Mutex

cache := NewCache()
cache.SkipTtlExtensionOnHit(true)
cache.SetTTL(time.Duration(50 * time.Millisecond))
cache.SetCheckExpirationCallback(func(key string, value interface{}) bool {
if key == "key2" || key == "key4" {
Expand Down