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 loadData add different item #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
38 changes: 38 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,33 @@ func TestFlush(t *testing.T) {
}
}

func TestDeleteAll(t *testing.T) {
table := Cache("testDeleteAll")
called := false

table.SetAboutToDeleteItemCallback(func(item *CacheItem) {
called = true
})

table.Add(k, 10*time.Second, v)
// flush the entire table
table.DeleteAll()

if !called {
t.Fail()
}

// try to retrieve the item
p, err := table.Value(k)
if err == nil || p != nil {
t.Error("Error flushing table")
}
// make sure there's really nothing else left in the cache
if table.Count() != 0 {
t.Error("Error verifying count of flushed table")
}
}

func TestCount(t *testing.T) {
// add a huge amount of items to the cache
table := Cache("testCount")
Expand All @@ -233,13 +260,18 @@ func TestCount(t *testing.T) {
}

func TestDataLoader(t *testing.T) {
called := 0

// setup a cache with a configured data-loader
table := Cache("testDataLoader")
table.SetDataLoader(func(key interface{}, args ...interface{}) *CacheItem {
var item *CacheItem
if key.(string) != "nil" {
val := k + key.(string)
i := NewCacheItem(key, 500*time.Millisecond, val)
i.SetAboutToExpireCallback(func(key interface{}) {
called++
})
item = i
}

Expand All @@ -260,6 +292,12 @@ func TestDataLoader(t *testing.T) {
if err != nil || p == nil || p.Data().(string) != vp {
t.Error("Error validating data loader")
}

table.Delete(key)
}

if called != 10 {
t.Fail()
}
}

Expand Down
31 changes: 29 additions & 2 deletions cachetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ func (table *CacheTable) Add(key interface{}, lifeSpan time.Duration, data inter
return item
}

func (table *CacheTable) addItem(item *CacheItem) *CacheItem {
table.Lock()
table.addInternal(item)

return item
}

func (table *CacheTable) deleteInternal(key interface{}) (*CacheItem, error) {
r, ok := table.items[key]
if !ok {
Expand Down Expand Up @@ -253,7 +260,7 @@ func (table *CacheTable) Value(key interface{}, args ...interface{}) (*CacheItem
if loadData != nil {
item := loadData(key, args...)
if item != nil {
table.Add(key, item.lifeSpan, item.data)
table.addItem(item)
return item, nil
}

Expand All @@ -277,6 +284,26 @@ func (table *CacheTable) Flush() {
}
}

// DeleteAll deletes all items from this cache table and call aboutToDeleteItem,aboutToExpire
func (table *CacheTable) DeleteAll() {
table.Foreach(func(key interface{}, item *CacheItem) {
// Trigger callbacks before deleting an item from cache.
if table.aboutToDeleteItem != nil {
table.aboutToDeleteItem(item)
}

item.RLock()
defer item.RUnlock()
if item.aboutToExpire != nil {
item.aboutToExpire(key)
}
})

table.log("Remove all table", table.name)

table.Flush()
}

// CacheItemPair maps key to access counter
type CacheItemPair struct {
Key interface{}
Expand Down Expand Up @@ -327,5 +354,5 @@ func (table *CacheTable) log(v ...interface{}) {
return
}

table.logger.Println(v)
table.logger.Println(v...)
}