Skip to content

Commit 9521706

Browse files
authored
Merge pull request #25 from amirreza8002/refactor
refactoring codebase
2 parents 3ad18bd + dc18b42 commit 9521706

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2996
-3932
lines changed

CHANGES.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
Version 0.3.0
2+
-------------
3+
4+
### New
5+
- now all operations support omitting exceptions
6+
- `make_key`, `make_pattern`, `encode` and `decode` are now functions, so they can be used outside the backend (e.g: when using the raw client)
7+
- `django_valkey.get_valkey_connection` now works with shard client as well
8+
9+
### bug fix
10+
- fixed bug of `omit_exception` not handling generator and async generators
11+
- fixed bug of async `get_valkey_connection` checking if the client is async.
12+
13+
### internal change
14+
- moved all operations from `django_valkey.cache` and `django_valkey.async_cache.cache` to `django_valkey.base`.
15+
- cluster client now uses the same methods as normal client, unless it has to have a specific method.
16+
- prefixing async methods with `a` is done dynamically now; (so no more `aset = set`), it's all handled in `__getattr__`.
17+
- moved async client methods to `django_valkey.base_client.AsyncClientCommands`.
18+
- moved sync client methods to `django_valkey.base_client.ClientCommands`.
19+
- `make_key`, `make_pattern`, `encode`, `decode`, `_decode_iterable_result` are now all sync methods and moved to `django_valkey.base_client.BaseClient`.
20+
- `AsyncHerdClient._pack` and `AsyncHerdClient._unpack` are now sync methods.
21+
- common parts of herd clients now live in `django_valkey.base_client`
22+
- shard client now has `get_client` instead of `get_server`
23+
124
Version 0.2.0
225
-------------
326

django_valkey/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
__version__ = ".".join(map(str, VERSION))
33

44

5-
def get_valkey_connection(alias="default", write=True):
5+
def get_valkey_connection(alias="default", write=True, key=None):
66
"""
77
Helper used for obtaining a raw valkey client.
88
"""
@@ -18,4 +18,7 @@ def get_valkey_connection(alias="default", write=True):
1818
if not hasattr(cache.client, "get_client"):
1919
raise NotImplementedError(error_message)
2020

21+
if hasattr(cache.client, "get_server_name"):
22+
return cache.client.get_client(key=key)
23+
2124
return cache.client.get_client(write)

django_valkey/async_cache/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from inspect import isawaitable
1+
from inspect import iscoroutinefunction
22

33

44
async def get_valkey_connection(alias="default", write=True):
@@ -17,7 +17,7 @@ async def get_valkey_connection(alias="default", write=True):
1717
if not hasattr(cache.client, "get_client"):
1818
raise NotImplementedError(error_message)
1919

20-
if not isawaitable(cache.client.get_client):
20+
if not iscoroutinefunction(cache.client.get_client):
2121
raise "use django_valkey.get_valkey_connection for sync backends"
2222

2323
return await cache.client.get_client(write)

django_valkey/async_cache/cache.py

Lines changed: 4 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1,10 @@
11
from valkey.asyncio.client import Valkey as AValkey
22

3-
from django_valkey.base import BaseValkeyCache
4-
from django_valkey.cache import omit_exception, CONNECTION_INTERRUPTED
3+
from django_valkey.base import BaseValkeyCache, AsyncBackendCommands
54
from django_valkey.async_cache.client.default import AsyncDefaultClient
65

76

8-
class AsyncValkeyCache(BaseValkeyCache[AsyncDefaultClient, AValkey]):
7+
class AsyncValkeyCache(
8+
BaseValkeyCache[AsyncDefaultClient, AValkey], AsyncBackendCommands
9+
):
910
DEFAULT_CLIENT_CLASS = "django_valkey.async_cache.client.default.AsyncDefaultClient"
10-
11-
mset = BaseValkeyCache.amset
12-
13-
keys = BaseValkeyCache.akeys
14-
15-
iter_keys = BaseValkeyCache.aiter_keys
16-
17-
ttl = BaseValkeyCache.attl
18-
19-
pttl = BaseValkeyCache.apttl
20-
21-
persist = BaseValkeyCache.apersist
22-
23-
expire = BaseValkeyCache.aexpire
24-
25-
expire_at = BaseValkeyCache.aexpire_at
26-
27-
pexpire = BaseValkeyCache.apexpire
28-
29-
pexpire_at = BaseValkeyCache.apexpire_at
30-
31-
lock = alock = get_lock = BaseValkeyCache.aget_lock
32-
33-
sadd = BaseValkeyCache.asadd
34-
35-
scard = BaseValkeyCache.ascard
36-
37-
sdiff = BaseValkeyCache.asdiff
38-
39-
sdiffstore = BaseValkeyCache.asdiffstore
40-
41-
sinter = BaseValkeyCache.asinter
42-
43-
sinterstore = BaseValkeyCache.asinterstore
44-
45-
sismember = BaseValkeyCache.asismember
46-
47-
smembers = BaseValkeyCache.asmembers
48-
49-
smove = BaseValkeyCache.asmove
50-
51-
spop = BaseValkeyCache.aspop
52-
53-
srandmember = BaseValkeyCache.asrandmember
54-
55-
srem = BaseValkeyCache.asrem
56-
57-
sscan = BaseValkeyCache.asscan
58-
59-
sscan_iter = BaseValkeyCache.asscan_iter
60-
61-
smismember = BaseValkeyCache.asmismember
62-
63-
sunion = BaseValkeyCache.asunion
64-
65-
sunionstore = BaseValkeyCache.asunionstore
66-
67-
hset = BaseValkeyCache.ahset
68-
69-
hdel = BaseValkeyCache.ahdel
70-
71-
hlen = BaseValkeyCache.ahlen
72-
73-
hkeys = BaseValkeyCache.ahkeys
74-
75-
hexists = BaseValkeyCache.ahexists
76-
77-
@omit_exception
78-
async def set(self, *args, **kwargs):
79-
return await self.client.aset(*args, **kwargs)
80-
81-
aset = set
82-
83-
@omit_exception
84-
async def incr_version(self, *args, **kwargs):
85-
return await self.client.aincr_version(*args, **kwargs)
86-
87-
aincr_version = incr_version
88-
89-
@omit_exception
90-
async def add(self, *args, **kwargs):
91-
return await self.client.aadd(*args, **kwargs)
92-
93-
aadd = add
94-
95-
async def get(self, key, default=None, version=None, client=None):
96-
value = await self._get(key, default, version, client)
97-
if value is CONNECTION_INTERRUPTED:
98-
value = default
99-
return value
100-
101-
aget = get
102-
103-
@omit_exception(return_value=CONNECTION_INTERRUPTED)
104-
async def _get(self, key, default=None, version=None, client=None):
105-
return await self.client.aget(key, default, version, client)
106-
107-
async def delete(self, *args, **kwargs):
108-
result = await self.client.adelete(*args, **kwargs)
109-
return bool(result)
110-
111-
adelete = delete
112-
113-
@omit_exception
114-
async def delete_many(self, *args, **kwargs):
115-
return await self.client.adelete_many(*args, **kwargs)
116-
117-
adelete_many = delete_many
118-
119-
@omit_exception
120-
async def clear(self):
121-
return await self.client.aclear()
122-
123-
aclear = clear
124-
125-
@omit_exception(return_value={})
126-
async def get_many(self, *args, **kwargs):
127-
return await self.client.aget_many(*args, **kwargs)
128-
129-
aget_many = get_many
130-
131-
@omit_exception
132-
async def set_many(self, *args, **kwargs):
133-
return await self.client.aset_many(*args, **kwargs)
134-
135-
aset_many = set_many
136-
137-
@omit_exception
138-
async def incr(self, *args, **kwargs):
139-
return await self.client.aincr(*args, **kwargs)
140-
141-
aincr = incr
142-
143-
@omit_exception
144-
async def decr(self, *args, **kwargs):
145-
return await self.client.adecr(*args, **kwargs)
146-
147-
adecr = decr
148-
149-
@omit_exception
150-
async def has_key(self, *args, **kwargs):
151-
return await self.client.ahas_key(*args, **kwargs)
152-
153-
ahas_key = has_key
154-
155-
@omit_exception
156-
async def aclose(self, *args, **kwargs):
157-
return await self.client.aclose()
158-
159-
@omit_exception
160-
async def touch(self, *args, **kwargs):
161-
return await self.client.touch(*args, **kwargs)
162-
163-
atouch = touch

0 commit comments

Comments
 (0)