From 8147d8367da20ffa86f382be4a6c1f810fb576c1 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Tue, 14 Jun 2022 14:29:59 +0100 Subject: [PATCH] Determine mutability using deep equality --- internal/caching/caches.go | 4 ++++ internal/caching/impl_ristretto.go | 22 +++++++++------------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/caching/caches.go b/internal/caching/caches.go index 1b456717c..f04fb175f 100644 --- a/internal/caching/caches.go +++ b/internal/caching/caches.go @@ -38,6 +38,10 @@ type costable interface { CacheCost() int64 } +type equatable interface { + comparable +} + type CacheSize int64 const ( diff --git a/internal/caching/impl_ristretto.go b/internal/caching/impl_ristretto.go index d6eadc79e..c74387502 100644 --- a/internal/caching/impl_ristretto.go +++ b/internal/caching/impl_ristretto.go @@ -1,6 +1,8 @@ package caching import ( + "fmt" + "reflect" "time" "github.com/dgraph-io/ristretto" @@ -76,15 +78,11 @@ type RistrettoCachePartition[K keyable, V any] struct { } func (c *RistrettoCachePartition[K, V]) Set(key K, value V) { - /* - if !c.Mutable { - if v, ok := c.cache.Get(key); ok { - if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", value) { // TODO: this is yucky - panic(fmt.Sprintf("invalid use of immutable cache tries to replace value of %v", key)) - } - } + if !c.Mutable { + if v, ok := c.cache.Get(key); ok && !reflect.DeepEqual(v, value) { + panic(fmt.Sprintf("invalid use of immutable cache tries to replace value of %v", key)) } - */ + } cost := int64(1) if cv, ok := any(value).(costable); ok { cost = int64(cv.CacheCost()) @@ -96,11 +94,9 @@ func (c *RistrettoCachePartition[K, V]) Unset(key K) { if c.cache == nil { return } - /* - if !c.Mutable { - panic(fmt.Sprintf("invalid use of immutable cache tries to unset value of %v", key)) - } - */ + if !c.Mutable { + panic(fmt.Sprintf("invalid use of immutable cache tries to unset value of %v", key)) + } c.cache.Del(key) }