| 117 | ) |
| 118 | @pytest.mark.onlynoncluster |
| 119 | def test_hash_get_from_given_cache(self, r, r2): |
| 120 | cache = r.get_cache() |
| 121 | hash_key = "hash_foo_key" |
| 122 | field_1 = "bar" |
| 123 | field_2 = "bar2" |
| 124 | |
| 125 | # add hash key to redis |
| 126 | r.hset(hash_key, field_1, "baz") |
| 127 | r.hset(hash_key, field_2, "baz2") |
| 128 | # get keys from redis and save them in local cache |
| 129 | assert r.hget(hash_key, field_1) in [b"baz", "baz"] |
| 130 | assert r.hget(hash_key, field_2) in [b"baz2", "baz2"] |
| 131 | # get key from local cache |
| 132 | assert cache.get( |
| 133 | CacheKey( |
| 134 | command="HGET", |
| 135 | redis_keys=(hash_key,), |
| 136 | redis_args=("HGET", hash_key, field_1), |
| 137 | ) |
| 138 | ).cache_value in [ |
| 139 | b"baz", |
| 140 | "baz", |
| 141 | ] |
| 142 | assert cache.get( |
| 143 | CacheKey( |
| 144 | command="HGET", |
| 145 | redis_keys=(hash_key,), |
| 146 | redis_args=("HGET", hash_key, field_2), |
| 147 | ) |
| 148 | ).cache_value in [ |
| 149 | b"baz2", |
| 150 | "baz2", |
| 151 | ] |
| 152 | # change key in redis (cause invalidation) |
| 153 | r2.hset(hash_key, field_1, "barbar") |
| 154 | # Retrieves a new value from server and cache it |
| 155 | assert r.hget(hash_key, field_1) in [b"barbar", "barbar"] |
| 156 | # Make sure that new value was cached |
| 157 | assert cache.get( |
| 158 | CacheKey( |
| 159 | command="HGET", |
| 160 | redis_keys=(hash_key,), |
| 161 | redis_args=("HGET", hash_key, field_1), |
| 162 | ) |
| 163 | ).cache_value in [ |
| 164 | b"barbar", |
| 165 | "barbar", |
| 166 | ] |
| 167 | # The other field is also reset, because the invalidation message contains only the hash key. |
| 168 | assert ( |
| 169 | cache.get( |
| 170 | CacheKey( |
| 171 | command="HGET", |
| 172 | redis_keys=(hash_key,), |
| 173 | redis_args=("HGET", hash_key, field_2), |
| 174 | ) |
| 175 | ) |
| 176 | is None |