From 4a648a9052c4169763110ca816185814ac1922b6 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 7 Jul 2022 11:41:42 +0100 Subject: [PATCH] Include key in cost --- internal/caching/impl_ristretto.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/internal/caching/impl_ristretto.go b/internal/caching/impl_ristretto.go index 6964d5ded..72f7392e4 100644 --- a/internal/caching/impl_ristretto.go +++ b/internal/caching/impl_ristretto.go @@ -147,13 +147,19 @@ func (c *RistrettoCachePartition[K, V]) setWithCost(key K, value V, cost int64) } func (c *RistrettoCachePartition[K, V]) Set(key K, value V) { - var cost int64 - if cv, ok := any(value).(string); ok { - cost = int64(len(cv)) + var keyCost int64 + var valueCost int64 + if ck, ok := any(key).(string); ok { + keyCost = int64(len(ck)) } else { - cost = int64(unsafe.Sizeof(value)) + keyCost = int64(unsafe.Sizeof(key)) } - c.setWithCost(key, value, cost) + if cv, ok := any(value).(string); ok { + valueCost = int64(len(cv)) + } else { + valueCost = int64(unsafe.Sizeof(value)) + } + c.setWithCost(key, value, keyCost+valueCost) } func (c *RistrettoCachePartition[K, V]) Unset(key K) {