Skip to content

Commit

Permalink
function renamed (typo fixed)
Browse files Browse the repository at this point in the history
  • Loading branch information
dadrus committed Nov 10, 2024
1 parent e7e24cc commit 53b1934
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ func (c *Cache[K, V]) set(key K, value V, ttl time.Duration) *Item[K, V] {
item.update(value, ttl)

if c.options.totalCost != 0 {
oldItemCosts := c.options.costsCalFunc(key, oldValue)
newItemCosts := c.options.costsCalFunc(key, value)
oldItemCosts := c.options.costsCalcFunc(key, oldValue)
newItemCosts := c.options.costsCalcFunc(key, value)

c.costs = c.costs - oldItemCosts + newItemCosts

Expand Down Expand Up @@ -174,7 +174,7 @@ func (c *Cache[K, V]) set(key K, value V, ttl time.Duration) *Item[K, V] {
c.updateExpirations(true, elem)

if c.options.totalCost != 0 {
c.costs += c.options.costsCalFunc(key, value)
c.costs += c.options.costsCalcFunc(key, value)

for c.costs > c.options.totalCost {
c.evict(EvictionReasonTotalCostExceeded, c.items.lru.Back())
Expand Down Expand Up @@ -283,7 +283,7 @@ func (c *Cache[K, V]) evict(reason EvictionReason, elems ...*list.Element) {
delete(c.items.values, item.key)

if c.options.totalCost != 0 {
c.costs -= c.options.costsCalFunc(item.key, item.value)
c.costs -= c.options.costsCalcFunc(item.key, item.value)
}

c.items.lru.Remove(elems[i])
Expand Down
4 changes: 2 additions & 2 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ func prepCache(maxCost uint64, ttl time.Duration, keys ...string) *Cache[string,
c.options.ttl = ttl
if maxCost != 0 {
c.options.totalCost = maxCost
c.options.costsCalFunc = func(key string, item string) uint64 {
c.options.costsCalcFunc = func(key string, item string) uint64 {
// 72 bytes are used by the Item struct
// 2 * 16 bytes are used by the used string headers (key and item)
return uint64(104 + len(key) + len(item))
Expand Down Expand Up @@ -1295,7 +1295,7 @@ func addToCache(c *Cache[string, string], ttl time.Duration, keys ...string) {
c.items.expQueue.push(elem)

if c.options.totalCost != 0 {
c.costs += c.options.costsCalFunc(key, value)
c.costs += c.options.costsCalcFunc(key, value)
}
}
}
4 changes: 2 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type CostCalcFunc[K comparable, V any] func(key K, item V) uint64
type options[K comparable, V any] struct {
capacity uint64
totalCost uint64
costsCalFunc CostCalcFunc[K, V]
costsCalcFunc CostCalcFunc[K, V]
ttl time.Duration
loader Loader[K, V]
disableTouchOnHit bool
Expand Down Expand Up @@ -89,6 +89,6 @@ func WithDisableTouchOnHit[K comparable, V any]() Option[K, V] {
func WithTotalCost[K comparable, V any](s uint64, callback CostCalcFunc[K, V]) Option[K, V] {
return optionFunc[K, V](func(opts *options[K, V]) {
opts.totalCost = s
opts.costsCalFunc = callback
opts.costsCalcFunc = callback
})
}
2 changes: 1 addition & 1 deletion options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ func Test_WithTotalCost(t *testing.T) {
WithTotalCost[string, string](1024, func(key string, item string) uint64 { return 1 }).apply(&opts)

assert.Equal(t, uint64(1024), opts.totalCost)
assert.Equal(t, uint64(1), opts.costsCalFunc("test", "foo"))
assert.Equal(t, uint64(1), opts.costsCalcFunc("test", "foo"))
}

0 comments on commit 53b1934

Please sign in to comment.