Skip to content

Commit

Permalink
[#31] Add boolean value return in Set
Browse files Browse the repository at this point in the history
Add return a boolean value when setting a key-value item with too high a cost.
  • Loading branch information
maypok86 committed Jan 11, 2024
1 parent 53faae3 commit e9406df
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
23 changes: 13 additions & 10 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,26 @@ func (c *Cache[K, V]) afterGet(got *node.Node[K, V]) {
}
}

// Set associates the value with the key in this cache. If the cache previously
// contained a value associated with the key, the old value is replaced by the new value.
func (c *Cache[K, V]) Set(key K, value V) {
c.set(key, value, 0)
// Set associates the value with the key in this cache.
//
// If it returns false, then the key-value item had too much cost and the Set was dropped.
func (c *Cache[K, V]) Set(key K, value V) bool {
return c.set(key, value, 0)
}

// SetWithTTL associates the value with the key in this cache and sets the custom ttl for this key-value pair.
// SetWithTTL associates the value with the key in this cache and sets the custom ttl for this key-value item.
//
// If the cache previously contained a value associated with the key, the old value is replaced by the new value.
func (c *Cache[K, V]) SetWithTTL(key K, value V, ttl time.Duration) {
// If it returns false, then the key-value item had too much cost and the SetWithTTL was dropped.
func (c *Cache[K, V]) SetWithTTL(key K, value V, ttl time.Duration) bool {
ttl = (ttl + time.Second - 1) / time.Second
expiration := unixtime.Now() + uint32(ttl)
c.set(key, value, expiration)
return c.set(key, value, expiration)
}

func (c *Cache[K, V]) set(key K, value V, expiration uint32) {
func (c *Cache[K, V]) set(key K, value V, expiration uint32) bool {
cost := c.costFunc(key, value)
if cost > c.policy.MaxAvailableCost() {
return
return false
}

n := node.New(key, value, expiration, cost)
Expand All @@ -169,6 +170,8 @@ func (c *Cache[K, V]) set(key K, value V, expiration uint32) {
// insert
c.writeBuffer.Insert(node.NewAddTask(n))
}

return true
}

// Delete removes the association for this key from the cache.
Expand Down
28 changes: 28 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,34 @@ func TestCache_SetWithTTL(t *testing.T) {
}
}

func TestCache_SetWithCost(t *testing.T) {
size := 10
c, err := MustBuilder[int, int](size).Cost(func(key int, value int) uint32 {
return uint32(key)
}).Build()
if err != nil {
t.Fatalf("can not create cache: %v", err)
}

goodCost := int(c.policy.MaxAvailableCost())
badCost := goodCost + 1

added := c.Set(goodCost, 1)
if !added {
t.Fatalf("Set was dropped, even though it shouldn't have been. Max available cost: %d, actual cost: %d",
c.policy.MaxAvailableCost(),
c.costFunc(goodCost, 1),
)
}
added = c.Set(badCost, 1)
if added {
t.Fatalf("Set wasn't dropped, though it should have been. Max available cost: %d, actual cost: %d",
c.policy.MaxAvailableCost(),
c.costFunc(badCost, 1),
)
}
}

func TestCache_Ratio(t *testing.T) {
b, err := NewBuilder[uint64, uint64](100)
if err != nil {
Expand Down

0 comments on commit e9406df

Please # to comment.