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

NOISSUE: Added cache.GetOrInsert method #6

Merged
merged 1 commit into from
Sep 4, 2024
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
7 changes: 7 additions & 0 deletions .envrc.devbox
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Automatically sets up your devbox environment whenever you cd into this
# directory via our direnv integration:

eval "$(devbox generate direnv --print-envrc)"

# check out https://www.jetpack.io/devbox/docs/ide_configuration/direnv/
# for more details
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ lint: ## CI lint
.PHONY: test
test:
GOARCH=$(GOARCHFORCED) $(GOTEST) ./... -count $(TEST_COUNT) -race $(TEST_ARGS)

.PHONY: init-devbox
init-devbox:
go mod tidy
41 changes: 40 additions & 1 deletion cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ func (c *cache[K, V]) Replace(k K, x V, d time.Duration) error {

// Get an item from the cache. Returns the item or nil, and a bool indicating
// whether the key was found.
// Method doesn't return or clean up expired item. You should explicitly call DeleteExpired() or Delete(k) to clean up
// expired items, or GetOrInsert() or use cache.janitor.
func (c *cache[K, V]) Get(k K) (V, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
Expand All @@ -150,6 +152,8 @@ func (c *cache[K, V]) Get(k K) (V, bool) {

// GetWithTouch gets an item from the cache and update its expiration time. Returns the item or nil, and a bool indicating
// whether the key was found.
// Method doesn't return or clean up expired item. You should explicitly call DeleteExpired() or Delete(k) to clean up
// expired items, or GetOrInsert() or use cache.janitor.
func (c *cache[K, V]) GetWithTouch(k K, d time.Duration) (V, bool) {
c.mu.Lock()
defer c.mu.Unlock()
Expand All @@ -168,6 +172,8 @@ func (c *cache[K, V]) GetWithTouch(k K, d time.Duration) (V, bool) {
// It returns the item or nil, the expiration time if one is set (if the item
// never expires a zero value for time.Time is returned), and a bool indicating
// whether the key was found.
// Method doesn't return or clean up expired item. You should explicitly call DeleteExpired() or Delete(k) to clean up
// expired items, or GetOrInsert() or use cache.janitor.
func (c *cache[K, V]) GetWithExpiration(k K) (V, time.Time, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
Expand All @@ -184,6 +190,39 @@ func (c *cache[K, V]) GetWithExpiration(k K) (V, time.Time, bool) {
return item.Object, item.Expiration, true
}

// GetOrInsert gets existing item or, otherwise, insert new one in atomic way.
// Returns the item or nil, and a bool indicating whether the key was found.
// If found item is expired, it will be immediately deleted with `onDeletion` propagation before inserting new value.
func (c *cache[K, V]) GetOrInsert(k K, insertV V, insertD time.Duration) (res V, found bool) {
var evictedItem *keyAndValue[K, V]

c.mu.RLock()
defer func() {
c.mu.RUnlock()
// Out of lock scope
if evictedItem != nil {
c.onDeletion(evictedItem.key, evictedItem.value, OnDeletionStatusEvicted)
}
}()

item, found := c.items[k]
switch {
case !found:
c.set(k, insertV, insertD)
return insertV, false

case item.Expired():
v, _, evicted := c.delete(k)
if evicted {
evictedItem = &keyAndValue[K, V]{k, v}
}
c.set(k, insertV, insertD)
return insertV, false
}

return item.Object, true
}

func (c *cache[K, V]) get(k K) (V, bool) {
item, found := c.items[k]
if !found {
Expand All @@ -209,7 +248,7 @@ func (c *cache[K, V]) Delete(k K) bool {
return deleted
}

func (c *cache[K, V]) delete(key K) (V, bool, bool) {
func (c *cache[K, V]) delete(key K) (v V, deleted bool, evicted bool) {
switch v, found := c.items[key]; {
case !found:
return zero.Zero[V](), false, false
Expand Down
116 changes: 75 additions & 41 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,81 @@ func TestOnEvicted(t *testing.T) {
assert.Equal(t, 4, x)
}

func TestGetWithExpiration(t *testing.T) {
tc := New[string, float64](time.Second, 0)
defer tc.Stop()

a, expiration, found := tc.GetWithExpiration("a")
assert.False(t, found)
assert.EqualValues(t, 0, a)
assert.True(t, expiration.IsZero())

b, expiration, found := tc.GetWithExpiration("d")
assert.False(t, found)
assert.EqualValues(t, 0, b)
assert.True(t, expiration.IsZero())

c, expiration, found := tc.GetWithExpiration("e")
assert.False(t, found)
assert.EqualValues(t, 0, c)
assert.True(t, expiration.IsZero())

tc.Set("a", 1, DefaultExpiration)
tc.Set("d", 1, NoExpiration)
tc.Set("e", 1, 50*time.Millisecond)

x, expiration, found := tc.GetWithExpiration("a")
assert.True(t, found)
assert.EqualValues(t, 1, x)
assert.False(t, expiration.IsZero())

x, expiration, found = tc.GetWithExpiration("d")
assert.True(t, found)
assert.EqualValues(t, 1, x)
assert.True(t, expiration.IsZero())

x, expiration, found = tc.GetWithExpiration("e")
assert.True(t, found)
assert.EqualValues(t, 1, x)
assert.False(t, expiration.IsZero())
assert.Equal(t, tc.items["e"].Expiration, expiration)
assert.True(t, time.Now().Before(expiration))
}

func TestGetOrInsert(t *testing.T) {

t.Run("should return found item", func(t *testing.T) {
tc := New[string, string](time.Second, time.Minute)
defer tc.Stop()

tc.Set("foo", "bar", DefaultExpiration)
x, found := tc.GetOrInsert("foo", "baz", DefaultExpiration)
assert.True(t, found)
assert.Equal(t, "bar", x)
})

t.Run("should delete expired item and insert new one", func(t *testing.T) {
tc := New[string, string](50*time.Millisecond, time.Minute)
defer tc.Stop()

tc.Set("foo", "bar", DefaultExpiration)
<-time.After(100 * time.Millisecond) // wait for expiration

x, found := tc.GetOrInsert("foo", "baz", DefaultExpiration)
assert.False(t, found)
assert.Equal(t, "baz", x)
})

t.Run("should insert new item", func(t *testing.T) {
tc := New[string, string](time.Second, time.Minute)
defer tc.Stop()

x, found := tc.GetOrInsert("foo", "baz", DefaultExpiration)
assert.False(t, found)
assert.Equal(t, "baz", x)
})
}

func BenchmarkCacheGetExpiring(b *testing.B) {
benchmarkCacheGet(b, 5*time.Minute)
}
Expand Down Expand Up @@ -383,44 +458,3 @@ func BenchmarkDeleteExpiredLoop(b *testing.B) {
tc.DeleteExpired()
}
}

func TestGetWithExpiration(t *testing.T) {
tc := New[string, float64](time.Second, 0)
defer tc.Stop()

a, expiration, found := tc.GetWithExpiration("a")
assert.False(t, found)
assert.EqualValues(t, 0, a)
assert.True(t, expiration.IsZero())

b, expiration, found := tc.GetWithExpiration("d")
assert.False(t, found)
assert.EqualValues(t, 0, b)
assert.True(t, expiration.IsZero())

c, expiration, found := tc.GetWithExpiration("e")
assert.False(t, found)
assert.EqualValues(t, 0, c)
assert.True(t, expiration.IsZero())

tc.Set("a", 1, DefaultExpiration)
tc.Set("d", 1, NoExpiration)
tc.Set("e", 1, 50*time.Millisecond)

x, expiration, found := tc.GetWithExpiration("a")
assert.True(t, found)
assert.EqualValues(t, 1, x)
assert.False(t, expiration.IsZero())

x, expiration, found = tc.GetWithExpiration("d")
assert.True(t, found)
assert.EqualValues(t, 1, x)
assert.True(t, expiration.IsZero())

x, expiration, found = tc.GetWithExpiration("e")
assert.True(t, found)
assert.EqualValues(t, 1, x)
assert.False(t, expiration.IsZero())
assert.Equal(t, tc.items["e"].Expiration, expiration)
assert.True(t, time.Now().Before(expiration))
}
19 changes: 19 additions & 0 deletions devbox.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"packages": [
"[email protected]",
"golangci-lint@latest",
"[email protected]"
],
"env": {
"GOPATH": "$PWD/.go",
"GOROOT": "$PWD/.devbox/nix/profile/default/share/go/",
"GOPRIVATE": "github.com/insolar,github.com/soverenio",
"GOTEST": "gotestsum --format dots --",
"PATH": "$PWD/.go/bin:$PATH"
},
"shell": {
"init_hook": [
"make init-devbox || true"
]
}
}
Loading