diff --git a/src/cachelib/memcached.py b/src/cachelib/memcached.py index 462d7352..36617390 100644 --- a/src/cachelib/memcached.py +++ b/src/cachelib/memcached.py @@ -202,12 +202,20 @@ def clear(self) -> bool: def inc(self, key: str, delta: int = 1) -> _t.Optional[int]: key = self._normalize_key(key) - value = (self._client.get(key) or 0) + delta + if self.pylibmc_used: + with self._client.reserve(block=self.blocking) as mc: + value = (mc.get(key) or 0) + delta + else: + value = (self._client.get(key) or 0) + delta return value if self.set(key, value) else None def dec(self, key: str, delta: int = 1) -> _t.Optional[int]: key = self._normalize_key(key) - value = (self._client.get(key) or 0) - delta + if self.pylibmc_used: + with self._client.reserve(block=self.blocking) as mc: + value = (mc.get(key) or 0) - delta + else: + value = (self._client.get(key) or 0) - delta return value if self.set(key, value) else None def import_preferred_memcache_lib(self, servers: _t.Any, threads: int) -> _t.Any: