Skip to content

Commit bd0fc58

Browse files
authored
Merge pull request #63 from amirreza8002/decr_version
add decr_version
2 parents cf122e1 + 4ba9a0d commit bd0fc58

File tree

6 files changed

+72
-0
lines changed

6 files changed

+72
-0
lines changed

django_valkey/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ def set(self: BaseValkeyCache, *args, **kwargs) -> bool:
210210
def incr_version(self: BaseValkeyCache, *args, **kwargs) -> int:
211211
return self.client.incr_version(*args, **kwargs)
212212

213+
def decr_version(self: BaseValkeyCache, *args, **kwargs) -> int:
214+
return self.client.decr_version(*args, **kwargs)
215+
213216
def add(self: BaseValkeyCache, *args, **kwargs) -> bool:
214217
return self.client.add(*args, **kwargs)
215218

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

404+
async def decr_version(self, *args, **kwargs):
405+
return await self.client.adecr_version(*args, **kwargs)
406+
401407
async def add(self, *args, **kwargs):
402408
return await self.client.aadd(*args, **kwargs)
403409

django_valkey/base_client.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,15 @@ def incr_version(
331331
self.delete(old_key, client=client)
332332
return version + delta
333333

334+
def decr_version(
335+
self: BaseClient,
336+
key: KeyT,
337+
delta: int = 1,
338+
version: int | None = None,
339+
client: Backend | Any | None = None,
340+
) -> int:
341+
return self.incr_version(key=key, delta=-delta, version=version, client=client)
342+
334343
def _incr_version(
335344
self: BaseClient,
336345
key: KeyT,
@@ -1464,6 +1473,17 @@ async def incr_version(
14641473
await self.delete(old_key, client=client)
14651474
return version + delta
14661475

1476+
async def decr_version(
1477+
self: BaseClient,
1478+
key: KeyT,
1479+
delta: int = 1,
1480+
version: int | None = None,
1481+
client: Backend | Any | None = None,
1482+
) -> int:
1483+
return await self.incr_version(
1484+
key=key, delta=-delta, version=version, client=client
1485+
)
1486+
14671487
async def _incr_version(self, key, delta, version, client) -> tuple:
14681488
if version is None:
14691489
version = self._backend.version

tests/test_backend.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,28 @@ def test_ttl_incr_version_no_timeout(self, cache: ValkeyCache):
536536

537537
assert my_value == "hello world!"
538538

539+
def test_decr_version(self, cache: ValkeyCache):
540+
cache.set("keytest", 2, version=3)
541+
res = cache.get("keytest", version=3)
542+
assert res == 2
543+
544+
cache.decr_version("keytest", version=3)
545+
546+
res = cache.get("keytest", version=3)
547+
assert res is None
548+
549+
res = cache.get("keytest", version=2)
550+
assert res == 2
551+
552+
def test_ttl_decr_version_no_timeout(self, cache: ValkeyCache):
553+
cache.set("my_key", "hello world!", version=3, timeout=None)
554+
555+
cache.decr_version("my_key", version=3)
556+
557+
my_value = cache.get("my_key", version=2)
558+
559+
assert my_value == "hello world!"
560+
539561
def test_delete_pattern(self, cache: ValkeyCache):
540562
if isinstance(cache.client, DefaultClusterClient):
541563
pytest.skip("cluster client has a specific test")

tests/test_cache_options.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def reverse_key(key: str) -> str:
3636
"hlen",
3737
"incr",
3838
"incr_version",
39+
"decr_version",
3940
"keys",
4041
"lock",
4142
"mget",

tests/tests_async/test_backend.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,28 @@ async def test_ttl_incr_version_no_timeout(self, cache: AsyncValkeyCache):
516516

517517
assert my_value == "hello world!"
518518

519+
async def test_decr_version(self, cache: AsyncValkeyCache):
520+
await cache.aset("keytest", 2, version=3)
521+
res = await cache.aget("keytest", version=3)
522+
assert res == 2
523+
524+
await cache.decr_version("keytest", version=3)
525+
526+
res = await cache.aget("keytest", version=3)
527+
assert res is None
528+
529+
res = await cache.aget("keytest", version=2)
530+
assert res == 2
531+
532+
async def test_ttl_decr_version_no_timeout(self, cache: AsyncValkeyCache):
533+
await cache.set("my_key", "hello world!", timeout=None, version=3)
534+
535+
await cache.adecr_version("my_key", version=3)
536+
537+
my_value = await cache.get("my_key", version=2)
538+
539+
assert my_value == "hello world!"
540+
519541
async def test_delete_pattern(self, cache: AsyncValkeyCache):
520542
for key in ["foo-aa", "foo-ab", "foo-bb", "foo-bc"]:
521543
await cache.aset(key, "foo")

tests/tests_async/test_cache_options.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"hlen",
3131
"incr",
3232
"incr_version",
33+
"decr_version",
3334
"keys",
3435
"lock",
3536
"mget",

0 commit comments

Comments
 (0)