Merge pull request #22 from alexejk/gcOnDelete
Calling OnDelete from gc()
This commit is contained in:
commit
142396791e
8
Gopkg.lock
generated
8
Gopkg.lock
generated
|
@ -2,24 +2,28 @@
|
||||||
|
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
|
digest = "1:65c6a2822d8653e4f780c259a86d1b444c0b1ce7601b500deb985387bfe6bdec"
|
||||||
name = "github.com/karlseguin/expect"
|
name = "github.com/karlseguin/expect"
|
||||||
packages = [
|
packages = [
|
||||||
".",
|
".",
|
||||||
"build",
|
"build",
|
||||||
"mock"
|
"mock",
|
||||||
]
|
]
|
||||||
|
pruneopts = "UT"
|
||||||
revision = "4fcda73748276dc72bcc09729bdb56242093c12c"
|
revision = "4fcda73748276dc72bcc09729bdb56242093c12c"
|
||||||
version = "v1.0.1"
|
version = "v1.0.1"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
branch = "master"
|
branch = "master"
|
||||||
|
digest = "1:d594bb9f2a18ba4da7ab1368f4debf59f6b77cc7046705553f966837c12059f1"
|
||||||
name = "github.com/wsxiaoys/terminal"
|
name = "github.com/wsxiaoys/terminal"
|
||||||
packages = ["color"]
|
packages = ["color"]
|
||||||
|
pruneopts = "UT"
|
||||||
revision = "0940f3fc43a0ed42d04916b1c04578462c650b09"
|
revision = "0940f3fc43a0ed42d04916b1c04578462c650b09"
|
||||||
|
|
||||||
[solve-meta]
|
[solve-meta]
|
||||||
analyzer-name = "dep"
|
analyzer-name = "dep"
|
||||||
analyzer-version = 1
|
analyzer-version = 1
|
||||||
inputs-digest = "a7bf38c5b0c419759438d9dca1af1ee50d70f07bc087f713c2ae35cbebdaf84b"
|
input-imports = ["github.com/karlseguin/expect"]
|
||||||
solver-name = "gps-cdcl"
|
solver-name = "gps-cdcl"
|
||||||
solver-version = 1
|
solver-version = 1
|
||||||
|
|
3
cache.go
3
cache.go
|
@ -223,6 +223,9 @@ func (c *Cache) gc() {
|
||||||
c.bucket(item.key).delete(item.key)
|
c.bucket(item.key).delete(item.key)
|
||||||
c.size -= item.size
|
c.size -= item.size
|
||||||
c.list.Remove(element)
|
c.list.Remove(element)
|
||||||
|
if c.onDelete != nil {
|
||||||
|
c.onDelete(item)
|
||||||
|
}
|
||||||
item.promotions = -2
|
item.promotions = -2
|
||||||
}
|
}
|
||||||
element = prev
|
element = prev
|
||||||
|
|
|
@ -98,7 +98,14 @@ func (_ CacheTests) TrackerDoesNotCleanupHeldInstance() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ CacheTests) RemovesOldestItemWhenFull() {
|
func (_ CacheTests) RemovesOldestItemWhenFull() {
|
||||||
cache := New(Configure().MaxSize(5).ItemsToPrune(1))
|
onDeleteFnCalled := false
|
||||||
|
onDeleteFn := func(item *Item) {
|
||||||
|
if item.key == "0" {
|
||||||
|
onDeleteFnCalled = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cache := New(Configure().MaxSize(5).ItemsToPrune(1).OnDelete(onDeleteFn))
|
||||||
for i := 0; i < 7; i++ {
|
for i := 0; i < 7; i++ {
|
||||||
cache.Set(strconv.Itoa(i), i, time.Minute)
|
cache.Set(strconv.Itoa(i), i, time.Minute)
|
||||||
}
|
}
|
||||||
|
@ -106,6 +113,7 @@ func (_ CacheTests) RemovesOldestItemWhenFull() {
|
||||||
Expect(cache.Get("0")).To.Equal(nil)
|
Expect(cache.Get("0")).To.Equal(nil)
|
||||||
Expect(cache.Get("1")).To.Equal(nil)
|
Expect(cache.Get("1")).To.Equal(nil)
|
||||||
Expect(cache.Get("2").Value()).To.Equal(2)
|
Expect(cache.Get("2").Value()).To.Equal(2)
|
||||||
|
Expect(onDeleteFnCalled).To.Equal(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ CacheTests) RemovesOldestItemWhenFullBySizer() {
|
func (_ CacheTests) RemovesOldestItemWhenFullBySizer() {
|
||||||
|
|
|
@ -2,9 +2,9 @@ package ccache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
. "github.com/karlseguin/expect"
|
. "github.com/karlseguin/expect"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type SecondaryCacheTests struct{}
|
type SecondaryCacheTests struct{}
|
||||||
|
@ -77,14 +77,14 @@ func (_ SecondaryCacheTests) FetchReturnsAnExistingValue() {
|
||||||
cache := newLayered()
|
cache := newLayered()
|
||||||
cache.Set("spice", "flow", "value-a", time.Minute)
|
cache.Set("spice", "flow", "value-a", time.Minute)
|
||||||
sCache := cache.GetOrCreateSecondaryCache("spice")
|
sCache := cache.GetOrCreateSecondaryCache("spice")
|
||||||
val, _ := sCache.Fetch("flow", time.Minute, func() (interface{}, error) {return "a fetched value", nil})
|
val, _ := sCache.Fetch("flow", time.Minute, func() (interface{}, error) { return "a fetched value", nil })
|
||||||
Expect(val.Value().(string)).To.Equal("value-a")
|
Expect(val.Value().(string)).To.Equal("value-a")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ SecondaryCacheTests) FetchReturnsANewValue() {
|
func (_ SecondaryCacheTests) FetchReturnsANewValue() {
|
||||||
cache := newLayered()
|
cache := newLayered()
|
||||||
sCache := cache.GetOrCreateSecondaryCache("spice")
|
sCache := cache.GetOrCreateSecondaryCache("spice")
|
||||||
val, _ := sCache.Fetch("flow", time.Minute, func() (interface{}, error) {return "a fetched value", nil})
|
val, _ := sCache.Fetch("flow", time.Minute, func() (interface{}, error) { return "a fetched value", nil })
|
||||||
Expect(val.Value().(string)).To.Equal("a fetched value")
|
Expect(val.Value().(string)).To.Equal("a fetched value")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue