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

View file

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

View file

@ -79,8 +79,8 @@ 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 && !reflect.DeepEqual(v, value) {
panic(fmt.Sprintf("invalid use of immutable cache tries to replace value of %v", key))
if v, ok := c.cache.Get(key); ok && v != nil && !reflect.DeepEqual(v, value) {
panic(fmt.Sprintf("invalid use of immutable cache tries to change value of %v from %v to %v", key, v, value))
}
}
cost := int64(1)
@ -91,9 +91,6 @@ func (c *RistrettoCachePartition[K, V]) Set(key K, value V) {
}
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))
}
@ -101,11 +98,11 @@ func (c *RistrettoCachePartition[K, V]) Unset(key K) {
}
func (c *RistrettoCachePartition[K, V]) Get(key K) (value V, ok bool) {
if c.cache == nil {
v, ok := c.cache.Get(key)
if !ok || v == nil {
var empty V
return empty, false
}
v, ok := c.cache.Get(key)
value, ok = v.(V)
return
}