|
1 | 1 | import time
|
2 | 2 |
|
| 3 | +import pytest |
| 4 | +from cachetools import TTLCache, LRUCache, LFUCache |
| 5 | + |
| 6 | +import redis |
3 | 7 | from redis import Redis, RedisCluster
|
| 8 | +from redis.utils import HIREDIS_AVAILABLE |
| 9 | +from tests.conftest import _get_client |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture() |
| 13 | +def r(request): |
| 14 | + use_cache = request.param.get("use_cache", False) |
| 15 | + cache = request.param.get("cache") |
| 16 | + kwargs = request.param.get("kwargs", {}) |
| 17 | + protocol = request.param.get("protocol", 3) |
| 18 | + single_connection_client = request.param.get("single_connection_client", False) |
| 19 | + with _get_client( |
| 20 | + redis.Redis, |
| 21 | + request, |
| 22 | + protocol=protocol, |
| 23 | + single_connection_client=single_connection_client, |
| 24 | + use_cache=use_cache, |
| 25 | + cache=cache, |
| 26 | + **kwargs, |
| 27 | + ) as client: |
| 28 | + yield client, cache |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.skipif(HIREDIS_AVAILABLE, reason="PythonParser only") |
| 32 | +class TestCache: |
| 33 | + @pytest.mark.parametrize("r", [{"cache": TTLCache(128, 300), "use_cache": True}], indirect=True) |
| 34 | + @pytest.mark.onlynoncluster |
| 35 | + def test_get_from_cache(self, r, r2): |
| 36 | + r, cache = r |
| 37 | + # add key to redis |
| 38 | + r.set("foo", "bar") |
| 39 | + # get key from redis and save in local cache |
| 40 | + assert r.get("foo") == b"bar" |
| 41 | + # get key from local cache |
| 42 | + assert cache.get(("GET", "foo")) == b"bar" |
| 43 | + # change key in redis (cause invalidation) |
| 44 | + r2.set("foo", "barbar") |
| 45 | + # Retrieves a new value from server and cache it |
| 46 | + assert r.get("foo") == b"barbar" |
| 47 | + # Make sure that new value was cached |
| 48 | + assert cache.get(("GET", "foo")) == b"barbar" |
| 49 | + |
| 50 | + @pytest.mark.parametrize( |
| 51 | + "r", |
| 52 | + [{"cache": LRUCache(3), "use_cache": True}], |
| 53 | + indirect=True, |
| 54 | + ) |
| 55 | + def test_cache_lru_eviction(self, r): |
| 56 | + r, cache = r |
| 57 | + # add 3 keys to redis |
| 58 | + r.set("foo", "bar") |
| 59 | + r.set("foo2", "bar2") |
| 60 | + r.set("foo3", "bar3") |
| 61 | + # get 3 keys from redis and save in local cache |
| 62 | + assert r.get("foo") == b"bar" |
| 63 | + assert r.get("foo2") == b"bar2" |
| 64 | + assert r.get("foo3") == b"bar3" |
| 65 | + # get the 3 keys from local cache |
| 66 | + assert cache.get(("GET", "foo")) == b"bar" |
| 67 | + assert cache.get(("GET", "foo2")) == b"bar2" |
| 68 | + assert cache.get(("GET", "foo3")) == b"bar3" |
| 69 | + # add 1 more key to redis (exceed the max size) |
| 70 | + r.set("foo4", "bar4") |
| 71 | + assert r.get("foo4") == b"bar4" |
| 72 | + # the first key is not in the local cache anymore |
| 73 | + assert cache.get(("GET", "foo")) is None |
| 74 | + |
| 75 | + @pytest.mark.parametrize("r", [{"cache": TTLCache(maxsize=128, ttl=1), "use_cache": True}], indirect=True) |
| 76 | + def test_cache_ttl(self, r, cache): |
| 77 | + r, cache = r |
| 78 | + # add key to redis |
| 79 | + r.set("foo", "bar") |
| 80 | + # get key from redis and save in local cache |
| 81 | + assert r.get("foo") == b"bar" |
| 82 | + # get key from local cache |
| 83 | + assert cache.get(("GET", "foo")) == b"bar" |
| 84 | + # wait for the key to expire |
| 85 | + time.sleep(1) |
| 86 | + # the key is not in the local cache anymore |
| 87 | + assert cache.get(("GET", "foo")) is None |
4 | 88 |
|
| 89 | + @pytest.mark.parametrize( |
| 90 | + "r", |
| 91 | + [{"cache": LFUCache(3), "use_cache": True}], |
| 92 | + indirect=True, |
| 93 | + ) |
| 94 | + def test_cache_lfu_eviction(self, r, cache): |
| 95 | + r, cache = r |
| 96 | + # add 3 keys to redis |
| 97 | + r.set("foo", "bar") |
| 98 | + r.set("foo2", "bar2") |
| 99 | + r.set("foo3", "bar3") |
| 100 | + # get 3 keys from redis and save in local cache |
| 101 | + assert r.get("foo") == b"bar" |
| 102 | + assert r.get("foo2") == b"bar2" |
| 103 | + assert r.get("foo3") == b"bar3" |
| 104 | + # change the order of the keys in the cache |
| 105 | + assert cache.get(("GET", "foo")) == b"bar" |
| 106 | + assert cache.get(("GET", "foo")) == b"bar" |
| 107 | + assert cache.get(("GET", "foo3")) == b"bar3" |
| 108 | + # add 1 more key to redis (exceed the max size) |
| 109 | + r.set("foo4", "bar4") |
| 110 | + assert r.get("foo4") == b"bar4" |
| 111 | + # test the eviction policy |
| 112 | + assert cache.currsize == 3 |
| 113 | + assert cache.get(("GET", "foo")) == b"bar" |
| 114 | + assert cache.get(("GET", "foo2")) is None |
5 | 115 |
|
6 |
| -def test_standalone_cached_get_and_set(): |
7 |
| - r = Redis(use_cache=True, protocol=3) |
8 |
| - assert r.set("key", 5) |
9 |
| - assert r.get("key") == b"5" |
| 116 | + @pytest.mark.parametrize( |
| 117 | + "r", |
| 118 | + [{"cache": LRUCache(maxsize=128), "use_cache": True}], |
| 119 | + indirect=True, |
| 120 | + ) |
| 121 | + def test_cache_ignore_not_allowed_command(self, r): |
| 122 | + r, cache = r |
| 123 | + # add fields to hash |
| 124 | + assert r.hset("foo", "bar", "baz") |
| 125 | + # get random field |
| 126 | + assert r.hrandfield("foo") == b"bar" |
| 127 | + assert cache.get(("HRANDFIELD", "foo")) is None |
10 | 128 |
|
11 |
| - r2 = Redis(protocol=3) |
12 |
| - r2.set("key", "foo") |
| 129 | + @pytest.mark.parametrize( |
| 130 | + "r", |
| 131 | + [{"cache": LRUCache(maxsize=128), "use_cache": True}], |
| 132 | + indirect=True, |
| 133 | + ) |
| 134 | + def test_cache_invalidate_all_related_responses(self, r, cache): |
| 135 | + r, cache = r |
| 136 | + # Add keys |
| 137 | + assert r.set("foo", "bar") |
| 138 | + assert r.set("bar", "foo") |
13 | 139 |
|
14 |
| - time.sleep(0.5) |
| 140 | + # Make sure that replies was cached |
| 141 | + assert r.mget("foo", "bar") == [b"bar", b"foo"] |
| 142 | + assert cache.get(("MGET", "foo", "bar")) == [b"bar", b"foo"] |
15 | 143 |
|
16 |
| - after_invalidation = r.get("key") |
17 |
| - print(f'after invalidation {after_invalidation}') |
18 |
| - assert after_invalidation == b"foo" |
| 144 | + # Invalidate one of the keys and make sure that all associated cached entries was removed |
| 145 | + assert r.set("foo", "baz") |
| 146 | + assert r.get("foo") == b"baz" |
| 147 | + assert cache.get(("MGET", "foo", "bar")) is None |
| 148 | + assert cache.get(("GET", "foo")) == b"baz" |
19 | 149 |
|
| 150 | + @pytest.mark.parametrize( |
| 151 | + "r", |
| 152 | + [{"cache": LRUCache(maxsize=128), "use_cache": True}], |
| 153 | + indirect=True, |
| 154 | + ) |
| 155 | + def test_cache_flushed_on_server_flush(self, r, cache): |
| 156 | + r, cache = r |
| 157 | + # Add keys |
| 158 | + assert r.set("foo", "bar") |
| 159 | + assert r.set("bar", "foo") |
| 160 | + assert r.set("baz", "bar") |
20 | 161 |
|
21 |
| -def test_cluster_cached_get_and_set(): |
22 |
| - cluster_url = "redis://localhost:16379/0" |
| 162 | + # Make sure that replies was cached |
| 163 | + assert r.get("foo") == b"bar" |
| 164 | + assert r.get("bar") == b"foo" |
| 165 | + assert r.get("baz") == b"bar" |
| 166 | + assert cache.get(("GET", "foo")) == b"bar" |
| 167 | + assert cache.get(("GET", "bar")) == b"foo" |
| 168 | + assert cache.get(("GET", "baz")) == b"bar" |
23 | 169 |
|
24 |
| - r = RedisCluster.from_url(cluster_url, use_cache=True, protocol=3) |
25 |
| - assert r.set("key", 5) |
26 |
| - assert r.get("key") == b"5" |
| 170 | + # Flush server and trying to access cached entry |
| 171 | + assert r.flushall() |
| 172 | + assert r.get("foo") is None |
| 173 | + assert cache.currsize == 0 |
27 | 174 |
|
28 |
| - r2 = RedisCluster.from_url(cluster_url, use_cache=True, protocol=3) |
29 |
| - r2.set("key", "foo") |
30 | 175 |
|
31 |
| - time.sleep(0.5) |
32 |
| - |
33 |
| - after_invalidation = r.get("key") |
34 |
| - print(f'after invalidation {after_invalidation}') |
35 |
| - assert after_invalidation == b"foo" |
| 176 | +# def test_cluster_cached_get_and_set(): |
| 177 | +# cluster_url = "redis://localhost:16379/0" |
| 178 | +# |
| 179 | +# r = RedisCluster.from_url(cluster_url, use_cache=True, protocol=3) |
| 180 | +# assert r.set("key", 5) |
| 181 | +# assert r.get("key") == b"5" |
| 182 | +# |
| 183 | +# r2 = RedisCluster.from_url(cluster_url, use_cache=True, protocol=3) |
| 184 | +# r2.set("key", "foo") |
| 185 | +# |
| 186 | +# time.sleep(0.5) |
| 187 | +# |
| 188 | +# after_invalidation = r.get("key") |
| 189 | +# print(f'after invalidation {after_invalidation}') |
| 190 | +# assert after_invalidation == b"foo" |
0 commit comments