1
1
import functools
2
2
import logging
3
3
from collections import OrderedDict
4
- from typing import Any , AsyncGenerator , Optional , Union
4
+ from typing import Any , AsyncIterator , Callable , Optional , Union
5
5
6
- from asgiref .sync import async_to_sync
7
6
from django .conf import settings
8
7
from django .core .cache .backends .base import BaseCache
9
8
from django .utils .module_loading import import_string
19
18
CONNECTION_INTERRUPTED = object ()
20
19
21
20
22
- def omit_exception (method = None , return_value = None ):
21
+ def omit_exception (
22
+ method : Optional [Callable ] = None , return_value : Optional [Any ] = None
23
+ ):
23
24
"""
24
25
Simple decorator that intercepts connection
25
26
errors and ignores these if settings specify this.
@@ -35,7 +36,7 @@ def _decorator(self, *args, **kwargs):
35
36
except ConnectionInterrupted as e :
36
37
if self ._ignore_exceptions :
37
38
if self ._log_ignored_exceptions :
38
- self .logger .error ( str ( e ) )
39
+ self .logger .exception ( "Exception ignored" )
39
40
40
41
return return_value
41
42
raise e .__cause__
@@ -70,10 +71,6 @@ def __init__(self, server, params):
70
71
else None
71
72
)
72
73
73
- def close (self , ** kwargs ):
74
- # TODO Remove this once Django's close_caches implement cache.close_async
75
- async_to_sync (self .close_async )()
76
-
77
74
@property
78
75
def client (self ) -> Union [DefaultClient ]:
79
76
"""
@@ -84,103 +81,94 @@ def client(self) -> Union[DefaultClient]:
84
81
return self ._client
85
82
86
83
@omit_exception
87
- async def set_async (self , * args , ** kwargs ):
88
- """
89
- :param key: the cache key.
90
- :param value: the cache's value.
91
- :param timeout: the timeout in seconds. Will be sent as pexpire.
92
- :param version: cache version.
93
- :param client: the client to use.
94
- :param nx: sets the nx flag. Not set if xx is True.
95
- :param xx: sets the xx flag. Takes precedence over nx if True.
96
- """
84
+ async def aset (self , * args , ** kwargs ):
97
85
return await self .client .set (* args , ** kwargs )
98
86
99
87
@omit_exception
100
- async def incr_version_async (self , * args , ** kwargs ):
88
+ async def aincr_version (self , * args , ** kwargs ):
101
89
return await self .client .incr_version (* args , ** kwargs )
102
90
103
91
@omit_exception
104
- async def add_async (self , * args , ** kwargs ):
92
+ async def aadd (self , * args , ** kwargs ):
105
93
return await self .client .add (* args , ** kwargs )
106
94
107
- async def get_async (self , key , default = None , version = None , client = None ):
108
- value = await self ._get_async (key , default , version , client )
95
+ async def aget (self , key , default = None , version = None , client = None ):
96
+ value = await self ._aget (key , default , version , client )
109
97
if value is CONNECTION_INTERRUPTED :
110
98
value = default
111
99
return value
112
100
113
101
@omit_exception (return_value = CONNECTION_INTERRUPTED )
114
- async def _get_async (self , key , default , version , client ):
102
+ async def _aget (self , key , default , version , client ):
115
103
return await self .client .get (
116
104
key , default = default , version = version , client = client
117
105
)
118
106
119
107
@omit_exception
120
- async def delete_async (self , * args , ** kwargs ):
108
+ async def adelete (self , * args , ** kwargs ):
121
109
return await self .client .delete (* args , ** kwargs )
122
110
123
111
@omit_exception
124
- async def delete_pattern_async (self , * args , ** kwargs ):
112
+ async def adelete_pattern (self , * args , ** kwargs ):
125
113
kwargs ["itersize" ] = kwargs .get ("itersize" , DJANGO_ASYNC_REDIS_SCAN_ITERSIZE )
126
114
return await self .client .delete_pattern (* args , ** kwargs )
127
115
128
116
@omit_exception
129
- async def delete_many_async (self , * args , ** kwargs ):
117
+ async def adelete_many (self , * args , ** kwargs ):
130
118
return await self .client .delete_many (* args , ** kwargs )
131
119
132
120
@omit_exception
133
- async def clear_async (self ) -> None :
121
+ async def aclear (self ) -> None :
134
122
await self .client .clear ()
135
123
136
124
@omit_exception (return_value = {})
137
- async def get_many_async (self , * args , ** kwargs ) -> OrderedDict :
125
+ async def aget_many (self , * args , ** kwargs ) -> OrderedDict :
138
126
return await self .client .get_many (* args , ** kwargs )
139
127
140
128
@omit_exception
141
- async def set_many_async (self , * args , ** kwargs ):
129
+ async def aset_many (self , * args , ** kwargs ):
142
130
return await self .client .set_many (* args , ** kwargs )
143
131
144
132
@omit_exception
145
- async def incr_async (self , * args , ** kwargs ):
133
+ async def aincr (self , * args , ** kwargs ):
146
134
return await self .client .incr (* args , ** kwargs )
147
135
148
136
@omit_exception
149
- async def decr_async (self , * args , ** kwargs ):
137
+ async def adecr (self , * args , ** kwargs ):
150
138
return await self .client .decr (* args , ** kwargs )
151
139
152
140
@omit_exception
153
- async def has_key_async (self , * args , ** kwargs ):
141
+ async def ahas_key (self , * args , ** kwargs ):
154
142
return await self .client .has_key (* args , ** kwargs )
155
143
156
144
@omit_exception
157
- async def keys_async (self , * args , ** kwargs ):
145
+ async def akeys (self , * args , ** kwargs ):
158
146
return await self .client .keys (* args , ** kwargs )
159
147
160
148
@omit_exception
161
- async def iter_keys_async (self , * args , ** kwargs ) -> AsyncGenerator [str , Any ]:
149
+ async def aiter_keys (self , * args , ** kwargs ) -> AsyncIterator [str ]:
162
150
"""
163
151
Returns a coroutine that the dev will
164
152
await since an async generator is returned.
165
153
"""
166
154
return self .client .iter_keys (* args , ** kwargs )
167
155
168
156
@omit_exception
169
- async def ttl_async (self , * args , ** kwargs ) -> Optional [int ]:
157
+ async def attl (self , * args , ** kwargs ) -> Optional [int ]:
170
158
return await self .client .ttl (* args , ** kwargs )
171
159
172
160
@omit_exception
173
- async def persist_async (self , * args , ** kwargs ):
161
+ async def apersist (self , * args , ** kwargs ):
174
162
return await self .client .persist (* args , ** kwargs )
175
163
176
164
@omit_exception
177
- async def expire_async (self , * args , ** kwargs ):
165
+ async def aexpire (self , * args , ** kwargs ):
178
166
return await self .client .expire (* args , ** kwargs )
179
167
180
168
@omit_exception
181
- async def close_async (self , ** kwargs ) -> None :
169
+ async def aclose (self , ** kwargs ) -> None :
182
170
await self .client .close (** kwargs )
183
171
184
172
@omit_exception
185
- async def touch_async (self , * args , ** kwargs ):
173
+ async def atouch (self , * args , ** kwargs ):
186
174
return await self .client .touch (* args , ** kwargs )
0 commit comments