redis/redis-py · warning · ValueError
Given entry does not belong to the cache
Error message
Given entry does not belong to the cache
What it means
Raised as ValueError by LRUPolicy.touch (redis/cache.py:343) when the given cache_key is not present in the collection. touch() is meant to mark an existing LRU entry as most-recently-used (via move_to_end), so touching a key that was never inserted or has been evicted is rejected.
Source
Thrown at redis/cache.py:343
def evict_many(self, count: int) -> List[CacheKey]:
self._assert_cache()
if count > len(self._cache.collection):
raise ValueError("Evictions count is above cache size")
popped_keys = []
for _ in range(count):
popped_entry = self._cache.collection.popitem(last=False)
popped_keys.append(popped_entry[0])
return popped_keys
def touch(self, cache_key: CacheKey) -> None:
self._assert_cache()
if self._cache.collection.get(cache_key) is None:
raise ValueError("Given entry does not belong to the cache")
self._cache.collection.move_to_end(cache_key)
def _assert_cache(self):
if self.cache is None or not isinstance(self.cache, CacheInterface):
raise ValueError("Eviction policy should be associated with valid cache.")
class EvictionPolicy(Enum):
LRU = LRUPolicy
class CacheConfig(CacheConfigurationInterface):
DEFAULT_CACHE_CLASS = DefaultCache
DEFAULT_EVICTION_POLICY = EvictionPolicy.LRU
DEFAULT_MAX_SIZE = 10000
DEFAULT_ALLOW_LIST = [View on GitHub (pinned to da03cdc7e8)
Solutions
- Check membership (cache_key in cache.collection) before calling touch.
- Only touch keys you just inserted or confirmed present.
- Catch the ValueError and treat as a no-op / re-insert if the key should still be cached.
Example fix
# before
policy.touch(maybe_stale_key) # ValueError if absent
# after
if maybe_stale_key in cache.collection:
policy.touch(maybe_stale_key) Defensive patterns
Strategy: validation
Validate before calling
if cache_key in cache.collection:
policy.touch(cache_key) Type guard
def key_in_cache(cache, key) -> bool:
return key in cache.collection Try / catch
try:
policy.touch(cache_key)
except ValueError:
pass # already evicted; nothing to touch Prevention
- Confirm membership before touching.
- Treat a touch miss as a no-op.
When it happens
Trigger: Calling policy.touch(k) for a key that was never added, or that was already evicted; touch invoked on a stale key reference after the cache was pruned.
Common situations: Touching a key whose entry expired/evicted between read and touch; logic that touches keys without confirming membership; concurrent eviction removing the entry first.
Related errors
- Evictions count is above cache size
- Eviction policy should be associated with valid cache.
- Subcommand {subcommand_name} not found in command {command_n
- Command {command_name} not found in commands
- Invalid args in command: {command, *args}
AI-assisted analysis of redis/redis-py@da03cdc7e8 (2026-08-04).
Data as JSON: /data/errors/ae9e8010bc4b29a1.json.
Report an issue: GitHub.