Skip to content

Commit

Permalink
Merge 32afdb1 into 46a3a44
Browse files Browse the repository at this point in the history
  • Loading branch information
trapajim authored May 16, 2019
2 parents 46a3a44 + 32afdb1 commit aa3dc4f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
34 changes: 32 additions & 2 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ func TestCallbacks(t *testing.T) {
t.Error("AddedItem callback not working")
}
m.Unlock()

// verify the AboutToDelete handler works
time.Sleep(500 * time.Millisecond)
m.Lock()
Expand All @@ -339,8 +338,39 @@ func TestCallbacks(t *testing.T) {
t.Error("AboutToExpire callback not working")
}
m.Unlock()
}

// test removeing of the callbacks
table.RemoveAddedItemCallbacks()
table.RemoveAboutToDeleteItemCallback()
secondItemKey := "itemKey02"
expired = false
i = table.Add(secondItemKey, 500*time.Millisecond, v)
i.SetAboutToExpireCallback(func(key interface{}) {
m.Lock()
expired = true
m.Unlock()
})
i.RemoveAboutToExpireCallback()
//verify if the callbacks were removed
time.Sleep(250 * time.Millisecond)
m.Lock()
if addedKey == secondItemKey {
t.Error("AddedItemCallbacks were not removed")
}
m.Unlock()

// verify the AboutToDelete handler works
time.Sleep(500 * time.Millisecond)
m.Lock()
if removedKey == secondItemKey {
t.Error("AboutToDeleteItem not removed")
}
// verify the AboutToExpire handler works
if expired {
t.Error("AboutToExpire callback not removed")
}
m.Unlock()
}
func TestLogger(t *testing.T) {
// setup a logger
out := new(bytes.Buffer)
Expand Down
11 changes: 9 additions & 2 deletions cacheitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type CacheItem struct {
accessCount int64

// Callback method triggered right before removing the item from the cache
aboutToExpire func(key interface{})
aboutToExpire []func(key interface{})
}

// NewCacheItem returns a newly created CacheItem.
Expand Down Expand Up @@ -104,5 +104,12 @@ func (item *CacheItem) Data() interface{} {
func (item *CacheItem) SetAboutToExpireCallback(f func(interface{})) {
item.Lock()
defer item.Unlock()
item.aboutToExpire = f
item.aboutToExpire = append(item.aboutToExpire, f)
}

// RemoveAboutToExpireCallback empties the about to expire callback queue
func (item *CacheItem) RemoveAboutToExpireCallback() {
item.Lock()
defer item.Unlock()
item.aboutToExpire = nil
}
34 changes: 27 additions & 7 deletions cachetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ type CacheTable struct {
// Callback method triggered when trying to load a non-existing key.
loadData func(key interface{}, args ...interface{}) *CacheItem
// Callback method triggered when adding a new item to the cache.
addedItem func(item *CacheItem)
addedItem []func(item *CacheItem)
// Callback method triggered before deleting an item from the cache.
aboutToDeleteItem func(item *CacheItem)
aboutToDeleteItem []func(item *CacheItem)
}

// Count returns how many items are currently stored in the cache.
Expand Down Expand Up @@ -70,15 +70,29 @@ func (table *CacheTable) SetDataLoader(f func(interface{}, ...interface{}) *Cach
func (table *CacheTable) SetAddedItemCallback(f func(*CacheItem)) {
table.Lock()
defer table.Unlock()
table.addedItem = f
table.addedItem = append(table.addedItem, f)
}

// RemoveAddedItemCallbacks empties the added item callback queue
func (table *CacheTable) RemoveAddedItemCallbacks() {
table.Lock()
defer table.Unlock()
table.addedItem = nil
}

// SetAboutToDeleteItemCallback configures a callback, which will be called
// every time an item is about to be removed from the cache.
func (table *CacheTable) SetAboutToDeleteItemCallback(f func(*CacheItem)) {
table.Lock()
defer table.Unlock()
table.aboutToDeleteItem = f
table.aboutToDeleteItem = append(table.aboutToDeleteItem, f)
}

// RemoveAboutToDeleteItemCallback empties the about to delete item callback queue
func (table *CacheTable) RemoveAboutToDeleteItemCallback() {
table.Lock()
defer table.Unlock()
table.aboutToDeleteItem = nil
}

// SetLogger sets the logger to be used by this cache table.
Expand Down Expand Up @@ -148,7 +162,9 @@ func (table *CacheTable) addInternal(item *CacheItem) {

// Trigger callback after adding an item to cache.
if addedItem != nil {
addedItem(item)
for _, callback := range addedItem {
callback(item)
}
}

// If we haven't set up any expiration check timer or found a more imminent item.
Expand Down Expand Up @@ -184,13 +200,17 @@ func (table *CacheTable) deleteInternal(key interface{}) (*CacheItem, error) {

// Trigger callbacks before deleting an item from cache.
if aboutToDeleteItem != nil {
aboutToDeleteItem(r)
for _, callback := range aboutToDeleteItem {
callback(r)
}
}

r.RLock()
defer r.RUnlock()
if r.aboutToExpire != nil {
r.aboutToExpire(key)
for _, callback := range r.aboutToExpire {
callback(key)
}
}

table.Lock()
Expand Down
6 changes: 5 additions & 1 deletion examples/callbacks/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ func main() {
// This callback will be triggered every time a new item
// gets added to the cache.
cache.SetAddedItemCallback(func(entry *cache2go.CacheItem) {
fmt.Println("Added:", entry.Key(), entry.Data(), entry.CreatedOn())
fmt.Println("Added Callback 1:", entry.Key(), entry.Data(), entry.CreatedOn())
})
cache.SetAddedItemCallback(func(entry *cache2go.CacheItem) {
fmt.Println("Added Callback 2:", entry.Key(), entry.Data(), entry.CreatedOn())
})
// This callback will be triggered every time an item
// is about to be removed from the cache.
Expand All @@ -35,6 +38,7 @@ func main() {
// Deleting the item will execute the AboutToDeleteItem callback.
cache.Delete("someKey")

cache.RemoveAddedItemCallbacks()
// Caching a new item that expires in 3 seconds
res = cache.Add("anotherKey", 3*time.Second, "This is another test")

Expand Down

0 comments on commit aa3dc4f

Please sign in to comment.