Merge pull request #22 from alexejk/gcOnDelete

Calling OnDelete from gc()
This commit is contained in:
Karl Seguin 2018-11-26 20:29:43 +07:00 committed by GitHub
commit 142396791e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 6 deletions

8
Gopkg.lock generated
View file

@ -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

View file

@ -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

View file

@ -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() {

View file

@ -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{}