-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
test_cache.py
275 lines (250 loc) · 10.6 KB
/
test_cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import time
import pytest
import pytest_asyncio
from redis._cache import _LocalCache
from redis.utils import HIREDIS_AVAILABLE
@pytest_asyncio.fixture
async def r(request, create_redis):
cache = request.param.get("cache")
kwargs = request.param.get("kwargs", {})
r = await create_redis(protocol=3, client_cache=cache, **kwargs)
yield r, cache
@pytest_asyncio.fixture()
async def local_cache():
yield _LocalCache()
@pytest.mark.skipif(HIREDIS_AVAILABLE, reason="PythonParser only")
class TestLocalCache:
@pytest.mark.onlynoncluster
@pytest.mark.parametrize("r", [{"cache": _LocalCache()}], indirect=True)
async def test_get_from_cache(self, r, r2):
r, cache = r
# add key to redis
await r.set("foo", "bar")
# get key from redis and save in local cache
assert await r.get("foo") == b"bar"
# get key from local cache
assert cache.get(("GET", "foo")) == b"bar"
# change key in redis (cause invalidation)
await r2.set("foo", "barbar")
# send any command to redis (process invalidation in background)
await r.ping()
# the command is not in the local cache anymore
assert cache.get(("GET", "foo")) is None
# get key from redis
assert await r.get("foo") == b"barbar"
@pytest.mark.parametrize("r", [{"cache": _LocalCache(max_size=3)}], indirect=True)
async def test_cache_max_size(self, r):
r, cache = r
# add 3 keys to redis
await r.set("foo", "bar")
await r.set("foo2", "bar2")
await r.set("foo3", "bar3")
# get 3 keys from redis and save in local cache
assert await r.get("foo") == b"bar"
assert await r.get("foo2") == b"bar2"
assert await r.get("foo3") == b"bar3"
# get the 3 keys from local cache
assert cache.get(("GET", "foo")) == b"bar"
assert cache.get(("GET", "foo2")) == b"bar2"
assert cache.get(("GET", "foo3")) == b"bar3"
# add 1 more key to redis (exceed the max size)
await r.set("foo4", "bar4")
assert await r.get("foo4") == b"bar4"
# the first key is not in the local cache anymore
assert cache.get(("GET", "foo")) is None
@pytest.mark.parametrize("r", [{"cache": _LocalCache(ttl=1)}], indirect=True)
async def test_cache_ttl(self, r):
r, cache = r
# add key to redis
await r.set("foo", "bar")
# get key from redis and save in local cache
assert await r.get("foo") == b"bar"
# get key from local cache
assert cache.get(("GET", "foo")) == b"bar"
# wait for the key to expire
time.sleep(1)
# the key is not in the local cache anymore
assert cache.get(("GET", "foo")) is None
@pytest.mark.parametrize(
"r", [{"cache": _LocalCache(max_size=3, eviction_policy="lfu")}], indirect=True
)
async def test_cache_lfu_eviction(self, r):
r, cache = r
# add 3 keys to redis
await r.set("foo", "bar")
await r.set("foo2", "bar2")
await r.set("foo3", "bar3")
# get 3 keys from redis and save in local cache
assert await r.get("foo") == b"bar"
assert await r.get("foo2") == b"bar2"
assert await r.get("foo3") == b"bar3"
# change the order of the keys in the cache
assert cache.get(("GET", "foo")) == b"bar"
assert cache.get(("GET", "foo")) == b"bar"
assert cache.get(("GET", "foo3")) == b"bar3"
# add 1 more key to redis (exceed the max size)
await r.set("foo4", "bar4")
assert await r.get("foo4") == b"bar4"
# test the eviction policy
assert len(cache.cache) == 3
assert cache.get(("GET", "foo")) == b"bar"
assert cache.get(("GET", "foo2")) is None
@pytest.mark.onlynoncluster
@pytest.mark.parametrize(
"r",
[{"cache": _LocalCache(), "kwargs": {"decode_responses": True}}],
indirect=True,
)
async def test_cache_decode_response(self, r):
r, cache = r
await r.set("foo", "bar")
# get key from redis and save in local cache
assert await r.get("foo") == "bar"
# get key from local cache
assert cache.get(("GET", "foo")) == "bar"
# change key in redis (cause invalidation)
await r.set("foo", "barbar")
# send any command to redis (process invalidation in background)
await r.ping()
# the command is not in the local cache anymore
assert cache.get(("GET", "foo")) is None
# get key from redis
assert await r.get("foo") == "barbar"
@pytest.mark.parametrize(
"r",
[{"cache": _LocalCache(), "kwargs": {"cache_blacklist": ["LLEN"]}}],
indirect=True,
)
async def test_cache_blacklist(self, r):
r, cache = r
# add list to redis
await r.lpush("mylist", "foo", "bar", "baz")
assert await r.llen("mylist") == 3
assert await r.lindex("mylist", 1) == b"bar"
assert cache.get(("LLEN", "mylist")) is None
assert cache.get(("LINDEX", "mylist", 1)) == b"bar"
@pytest.mark.parametrize("r", [{"cache": _LocalCache()}], indirect=True)
async def test_cache_return_copy(self, r):
r, cache = r
await r.lpush("mylist", "foo", "bar", "baz")
assert await r.lrange("mylist", 0, -1) == [b"baz", b"bar", b"foo"]
res = cache.get(("LRANGE", "mylist", 0, -1))
assert res == [b"baz", b"bar", b"foo"]
res.append(b"new")
check = cache.get(("LRANGE", "mylist", 0, -1))
assert check == [b"baz", b"bar", b"foo"]
@pytest.mark.onlynoncluster
@pytest.mark.parametrize(
"r",
[{"cache": _LocalCache(), "kwargs": {"decode_responses": True}}],
indirect=True,
)
async def test_csc_not_cause_disconnects(self, r):
r, cache = r
id1 = await r.client_id()
await r.mset({"a": 1, "b": 1, "c": 1, "d": 1, "e": 1})
assert await r.mget("a", "b", "c", "d", "e") == ["1", "1", "1", "1", "1"]
id2 = await r.client_id()
# client should get value from client cache
assert await r.mget("a", "b", "c", "d", "e") == ["1", "1", "1", "1", "1"]
assert cache.get(("MGET", "a", "b", "c", "d", "e")) == [
"1",
"1",
"1",
"1",
"1",
]
await r.mset({"a": 2, "b": 2, "c": 2, "d": 2, "e": 2})
id3 = await r.client_id()
# client should get value from redis server post invalidate messages
assert await r.mget("a", "b", "c", "d", "e") == ["2", "2", "2", "2", "2"]
await r.mset({"a": 3, "b": 3, "c": 3, "d": 3, "e": 3})
# need to check that we get correct value 3 and not 2
assert await r.mget("a", "b", "c", "d", "e") == ["3", "3", "3", "3", "3"]
# client should get value from client cache
assert await r.mget("a", "b", "c", "d", "e") == ["3", "3", "3", "3", "3"]
await r.mset({"a": 4, "b": 4, "c": 4, "d": 4, "e": 4})
# need to check that we get correct value 4 and not 3
assert await r.mget("a", "b", "c", "d", "e") == ["4", "4", "4", "4", "4"]
# client should get value from client cache
assert await r.mget("a", "b", "c", "d", "e") == ["4", "4", "4", "4", "4"]
id4 = await r.client_id()
assert id1 == id2 == id3 == id4
@pytest.mark.skipif(HIREDIS_AVAILABLE, reason="PythonParser only")
@pytest.mark.onlycluster
class TestClusterLocalCache:
@pytest.mark.parametrize("r", [{"cache": _LocalCache()}], indirect=True)
async def test_get_from_cache(self, r, r2):
r, cache = r
# add key to redis
await r.set("foo", "bar")
# get key from redis and save in local cache
assert await r.get("foo") == b"bar"
# get key from local cache
assert cache.get(("GET", "foo")) == b"bar"
# change key in redis (cause invalidation)
await r2.set("foo", "barbar")
# send any command to redis (process invalidation in background)
node = r.get_node_from_key("foo")
await r.ping(target_nodes=node)
# the command is not in the local cache anymore
assert cache.get(("GET", "foo")) is None
# get key from redis
assert await r.get("foo") == b"barbar"
@pytest.mark.parametrize(
"r",
[{"cache": _LocalCache(), "kwargs": {"decode_responses": True}}],
indirect=True,
)
async def test_cache_decode_response(self, r):
r, cache = r
await r.set("foo", "bar")
# get key from redis and save in local cache
assert await r.get("foo") == "bar"
# get key from local cache
assert cache.get(("GET", "foo")) == "bar"
# change key in redis (cause invalidation)
await r.set("foo", "barbar")
# send any command to redis (process invalidation in background)
node = r.get_node_from_key("foo")
await r.ping(target_nodes=node)
# the command is not in the local cache anymore
assert cache.get(("GET", "foo")) is None
# get key from redis
assert await r.get("foo") == "barbar"
@pytest.mark.skipif(HIREDIS_AVAILABLE, reason="PythonParser only")
@pytest.mark.onlynoncluster
class TestSentinelLocalCache:
async def test_get_from_cache(self, local_cache, master):
await master.set("foo", "bar")
# get key from redis and save in local cache
assert await master.get("foo") == b"bar"
# get key from local cache
assert local_cache.get(("GET", "foo")) == b"bar"
# change key in redis (cause invalidation)
await master.set("foo", "barbar")
# send any command to redis (process invalidation in background)
await master.ping()
# the command is not in the local cache anymore
assert local_cache.get(("GET", "foo")) is None
# get key from redis
assert await master.get("foo") == b"barbar"
@pytest.mark.parametrize(
"sentinel_setup",
[{"kwargs": {"decode_responses": True}}],
indirect=True,
)
async def test_cache_decode_response(self, local_cache, sentinel_setup, master):
await master.set("foo", "bar")
# get key from redis and save in local cache
assert await master.get("foo") == "bar"
# get key from local cache
assert local_cache.get(("GET", "foo")) == "bar"
# change key in redis (cause invalidation)
await master.set("foo", "barbar")
# send any command to redis (process invalidation in background)
await master.ping()
# the command is not in the local cache anymore
assert local_cache.get(("GET", "foo")) is None
# get key from redis
assert await master.get("foo") == "barbar"