Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions django_valkey/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ def set(self: BaseValkeyCache, *args, **kwargs) -> bool:
def incr_version(self: BaseValkeyCache, *args, **kwargs) -> int:
return self.client.incr_version(*args, **kwargs)

def decr_version(self: BaseValkeyCache, *args, **kwargs) -> int:
return self.client.decr_version(*args, **kwargs)

def add(self: BaseValkeyCache, *args, **kwargs) -> bool:
return self.client.add(*args, **kwargs)

Expand Down Expand Up @@ -398,6 +401,9 @@ async def set(self: BaseValkeyCache, *args, **kwargs):
async def incr_version(self, *args, **kwargs):
return await self.client.aincr_version(*args, **kwargs)

async def decr_version(self, *args, **kwargs):
return await self.client.adecr_version(*args, **kwargs)

async def add(self, *args, **kwargs):
return await self.client.aadd(*args, **kwargs)

Expand Down
20 changes: 20 additions & 0 deletions django_valkey/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,15 @@ def incr_version(
self.delete(old_key, client=client)
return version + delta

def decr_version(
self: BaseClient,
key: KeyT,
delta: int = 1,
version: int | None = None,
client: Backend | Any | None = None,
) -> int:
return self.incr_version(key=key, delta=-delta, version=version, client=client)

def _incr_version(
self: BaseClient,
key: KeyT,
Expand Down Expand Up @@ -1464,6 +1473,17 @@ async def incr_version(
await self.delete(old_key, client=client)
return version + delta

async def decr_version(
self: BaseClient,
key: KeyT,
delta: int = 1,
version: int | None = None,
client: Backend | Any | None = None,
) -> int:
return await self.incr_version(
key=key, delta=-delta, version=version, client=client
)

async def _incr_version(self, key, delta, version, client) -> tuple:
if version is None:
version = self._backend.version
Expand Down
22 changes: 22 additions & 0 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,28 @@ def test_ttl_incr_version_no_timeout(self, cache: ValkeyCache):

assert my_value == "hello world!"

def test_decr_version(self, cache: ValkeyCache):
cache.set("keytest", 2, version=3)
res = cache.get("keytest", version=3)
assert res == 2

cache.decr_version("keytest", version=3)

res = cache.get("keytest", version=3)
assert res is None

res = cache.get("keytest", version=2)
assert res == 2

def test_ttl_decr_version_no_timeout(self, cache: ValkeyCache):
cache.set("my_key", "hello world!", version=3, timeout=None)

cache.decr_version("my_key", version=3)

my_value = cache.get("my_key", version=2)

assert my_value == "hello world!"

def test_delete_pattern(self, cache: ValkeyCache):
if isinstance(cache.client, DefaultClusterClient):
pytest.skip("cluster client has a specific test")
Expand Down
1 change: 1 addition & 0 deletions tests/test_cache_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def reverse_key(key: str) -> str:
"hlen",
"incr",
"incr_version",
"decr_version",
"keys",
"lock",
"mget",
Expand Down
22 changes: 22 additions & 0 deletions tests/tests_async/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,28 @@ async def test_ttl_incr_version_no_timeout(self, cache: AsyncValkeyCache):

assert my_value == "hello world!"

async def test_decr_version(self, cache: AsyncValkeyCache):
await cache.aset("keytest", 2, version=3)
res = await cache.aget("keytest", version=3)
assert res == 2

await cache.decr_version("keytest", version=3)

res = await cache.aget("keytest", version=3)
assert res is None

res = await cache.aget("keytest", version=2)
assert res == 2

async def test_ttl_decr_version_no_timeout(self, cache: AsyncValkeyCache):
await cache.set("my_key", "hello world!", timeout=None, version=3)

await cache.adecr_version("my_key", version=3)

my_value = await cache.get("my_key", version=2)

assert my_value == "hello world!"

async def test_delete_pattern(self, cache: AsyncValkeyCache):
for key in ["foo-aa", "foo-ab", "foo-bb", "foo-bc"]:
await cache.aset(key, "foo")
Expand Down
1 change: 1 addition & 0 deletions tests/tests_async/test_cache_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"hlen",
"incr",
"incr_version",
"decr_version",
"keys",
"lock",
"mget",
Expand Down