diff options
Diffstat (limited to 'vendor/codeberg.org/gruf/go-cache')
| -rw-r--r-- | vendor/codeberg.org/gruf/go-cache/v3/result/cache.go | 37 | ||||
| -rw-r--r-- | vendor/codeberg.org/gruf/go-cache/v3/result/error.go | 22 | ||||
| -rw-r--r-- | vendor/codeberg.org/gruf/go-cache/v3/result/key.go | 13 | 
3 files changed, 29 insertions, 43 deletions
diff --git a/vendor/codeberg.org/gruf/go-cache/v3/result/cache.go b/vendor/codeberg.org/gruf/go-cache/v3/result/cache.go index 45b7c16d7..a12209316 100644 --- a/vendor/codeberg.org/gruf/go-cache/v3/result/cache.go +++ b/vendor/codeberg.org/gruf/go-cache/v3/result/cache.go @@ -178,10 +178,8 @@ func (c *Cache[Value]) Load(lookup string, load func() (Value, error), keyParts  		c.cache.Lock()  		defer c.cache.Unlock() -		// Attempt to cache this result. -		if key, ok := c.storeResult(res); !ok { -			return zero, ConflictError{key} -		} +		// Cache this result +		c.storeResult(res)  	}  	// Catch and return error @@ -211,11 +209,8 @@ func (c *Cache[Value]) Store(value Value, store func() error) error {  	c.cache.Lock()  	defer c.cache.Unlock() -	// Attempt to cache result, only return conflict -	// error if the appropriate flag has been set. -	if key, ok := c.storeResult(result); !ok { -		return ConflictError{key} -	} +	// Cache this result +	c.storeResult(result)  	return nil  } @@ -285,7 +280,7 @@ func (c *Cache[Value]) Cap() int {  	return c.cache.Cache.Cap()  } -func (c *Cache[Value]) storeResult(res result[Value]) (string, bool) { +func (c *Cache[Value]) storeResult(res result[Value]) {  	for _, key := range res.Keys {  		pkeys := key.key.pkeys @@ -293,16 +288,18 @@ func (c *Cache[Value]) storeResult(res result[Value]) (string, bool) {  		pkey, ok := pkeys[key.value]  		if ok { -			// Look for overlap with non error keys, -			// as an overlap for some but not all keys -			// could produce inconsistent results. +			// Get the overlapping result with this key.  			entry, _ := c.cache.Cache.Get(pkey) -			if entry.Value.Error == nil { -				return key.value, false -			} -			// Delete existing error result -			c.cache.Cache.Delete(pkey) +			// From conflicting entry, drop this key, this +			// will prevent eviction cleanup key confusion. +			entry.Value.Keys.drop(key.key.name) + +			if len(entry.Value.Keys) == 0 { +				// We just over-wrote the only lookup key for +				// this value, so we drop its primary key too +				c.cache.Cache.Delete(pkey) +			}  		}  	} @@ -324,13 +321,11 @@ func (c *Cache[Value]) storeResult(res result[Value]) (string, bool) {  	}, func(_ int64, item *ttl.Entry[int64, result[Value]]) {  		c.cache.Evict(item)  	}) - -	return "", true  }  type result[Value any] struct {  	// keys accessible under -	Keys []cachedKey +	Keys cacheKeys  	// cached value  	Value Value diff --git a/vendor/codeberg.org/gruf/go-cache/v3/result/error.go b/vendor/codeberg.org/gruf/go-cache/v3/result/error.go deleted file mode 100644 index fa26083bf..000000000 --- a/vendor/codeberg.org/gruf/go-cache/v3/result/error.go +++ /dev/null @@ -1,22 +0,0 @@ -package result - -import "errors" - -// ErrUnkownLookup ... -var ErrUnknownLookup = errors.New("unknown lookup identifier") - -// IsConflictErr returns whether error is due to key conflict. -func IsConflictErr(err error) bool { -	_, ok := err.(ConflictError) -	return ok -} - -// ConflictError is returned on cache key conflict. -type ConflictError struct { -	Key string -} - -// Error returns the message for this key conflict error. -func (c ConflictError) Error() string { -	return "cache conflict for key \"" + c.Key + "\"" -} diff --git a/vendor/codeberg.org/gruf/go-cache/v3/result/key.go b/vendor/codeberg.org/gruf/go-cache/v3/result/key.go index 0dc1276a5..000633221 100644 --- a/vendor/codeberg.org/gruf/go-cache/v3/result/key.go +++ b/vendor/codeberg.org/gruf/go-cache/v3/result/key.go @@ -77,6 +77,19 @@ func (sk structKeys) generate(a any) []cachedKey {  	return keys  } +type cacheKeys []cachedKey + +// drop will drop the cachedKey with lookup name from receiving cacheKeys slice. +func (ck *cacheKeys) drop(name string) { +	_ = *ck // move out of loop +	for i := range *ck { +		if (*ck)[i].key.name == name { +			(*ck) = append((*ck)[:i], (*ck)[i+1:]...) +			break +		} +	} +} +  // cachedKey represents an actual cached key.  type cachedKey struct {  	// key is a reference to the structKey this  | 
