Determine mutability using deep equality

This commit is contained in:
Neil Alexander 2022-06-14 14:29:59 +01:00
parent e1f4a485f5
commit 8147d8367d
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
2 changed files with 13 additions and 13 deletions

View file

@ -38,6 +38,10 @@ type costable interface {
CacheCost() int64
}
type equatable interface {
comparable
}
type CacheSize int64
const (

View file

@ -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
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))
}
*/
c.cache.Del(key)
}