{"record":{"id":"ae9e8010bc4b29a1","repo":"redis/redis-py","slug":"given-entry-does-not-belong-to-the-cache","errorCode":null,"errorMessage":"Given entry does not belong to the cache","messagePattern":"Given entry does not belong to the cache","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"redis/cache.py","lineNumber":343,"sourceCode":"\n    def evict_many(self, count: int) -> List[CacheKey]:\n        self._assert_cache()\n        if count > len(self._cache.collection):\n            raise ValueError(\"Evictions count is above cache size\")\n\n        popped_keys = []\n\n        for _ in range(count):\n            popped_entry = self._cache.collection.popitem(last=False)\n            popped_keys.append(popped_entry[0])\n\n        return popped_keys\n\n    def touch(self, cache_key: CacheKey) -> None:\n        self._assert_cache()\n\n        if self._cache.collection.get(cache_key) is None:\n            raise ValueError(\"Given entry does not belong to the cache\")\n\n        self._cache.collection.move_to_end(cache_key)\n\n    def _assert_cache(self):\n        if self.cache is None or not isinstance(self.cache, CacheInterface):\n            raise ValueError(\"Eviction policy should be associated with valid cache.\")\n\n\nclass EvictionPolicy(Enum):\n    LRU = LRUPolicy\n\n\nclass CacheConfig(CacheConfigurationInterface):\n    DEFAULT_CACHE_CLASS = DefaultCache\n    DEFAULT_EVICTION_POLICY = EvictionPolicy.LRU\n    DEFAULT_MAX_SIZE = 10000\n\n    DEFAULT_ALLOW_LIST = [","sourceCodeStart":325,"sourceCodeEnd":361,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/cache.py#L325-L361","documentation":"Raised as ValueError in LRUPolicy.touch when the given cache_key is not present in the cache collection. touch() is meant to mark an existing entry as recently used by calling move_to_end; if the key is absent there is nothing to move, so it refuses.","triggerScenarios":"Calling eviction_policy.touch(key) for a key that was never inserted, was already evicted, or was deleted. This path runs during CSC access patterns that promote recently-used entries.","commonSituations":"A stale key reference after eviction/flush. Concurrent eviction removing the key between a get and a touch. Using touch on a freshly created policy not yet associated with populated cache entries.","solutions":["Check cache.get(key) is not None before calling touch(key).","Catch ValueError around touch if the access pattern tolerates missing keys.","Avoid touching keys that may have been evicted under LRU pressure."],"exampleFix":"# before\npolicy.touch(key)  # ValueError if evicted\n# after\nif cache.get(key) is not None:\n    policy.touch(key)","handlingStrategy":"validation","validationCode":"if cache.get(key) is not None:\n    policy.touch(key)","typeGuard":null,"tryCatchPattern":"try:\n    policy.touch(key)\nexcept ValueError as e:\n    if \"does not belong\" in str(e):\n        pass  # already evicted; ignore\n    else:\n        raise","preventionTips":["Check cache.get(key) before touch to avoid stale references.","Tolerate missing keys in LRU touch paths."],"tags":["cache","eviction","client-side-caching","validation"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}