-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Streamline client side caching API typing #3216
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3216 +/- ##
==========================================
+ Coverage 91.84% 91.94% +0.09%
==========================================
Files 128 128
Lines 33232 33517 +285
==========================================
+ Hits 30523 30816 +293
+ Misses 2709 2701 -8 ☔ View full report in Codecov by Sentry. |
0436538
to
a55cdb1
Compare
Streamline the typing of the client side caching API. Some of the methods are defining commands of type `str`, while in reality tuples are being sent for those parameters. Add client side cache tests for Sentinels. In order to make this work, fix the sentinel configuration in the docker-compose stack. Add a test for client side caching with a truly custom cache, not just injecting our internal cache structure as custom. Add a test for client side caching where two different types of commands use the same key, to make sure they invalidate each others cached data.
a55cdb1
to
523dc7c
Compare
@@ -696,7 +696,7 @@ def _cache_invalidation_process( | |||
and the second string is the list of keys to invalidate. | |||
(if the list of keys is None, then all keys are invalidated) | |||
""" | |||
if data[1] is not None: | |||
if data[1] is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch! thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surfaced due to the new tests, basically luck.
@pytest.mark.skipif(HIREDIS_AVAILABLE, reason="PythonParser only") | ||
class TestLocalCache: | ||
@pytest.mark.onlynoncluster |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? I think it will fail on cluster
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved the decorator to class level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved back.
@@ -36,7 +41,7 @@ async def test_get_from_cache(self, r, r2): | |||
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): | |||
async def test_cache_lru_eviction(self, r): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? this test checks that if I set the max_size to 3 it will not save more than 3 in the cache
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a discussion about eviction policies. Max size is always bound to an eviction policy. It felt cleaner to express this as "test that the default eviction policy is LRU".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you refer me to this discussion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The discussion was mostly online, in the weekly call. Another argument: all tests for eviction strategies implicitly test the max size of the cache.
redis/_cache.py
Outdated
def set(self, command: str, response: ResponseT, keys_in_command: List[KeyT]): | ||
def set( | ||
self, | ||
command: Union[str, Iterable[str]], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why it need to be also Iterable[str]?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because in all unit tests tuples are being sent for this parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not in unit tests, that is for the get
. In Connection we have this signature for example:
def _add_to_local_cache(
self, command: Tuple[str], response: ResponseT, keys: List[KeysT]
):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So maybe we need only Iterable[str]? I'm not sure...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a broader discussion, I think we can leave it with both for the beta release. In general I think we should not expose to the outside world the way we represent commands in the cache. We should only expose the concept of keys externally, and do the mapping to commands and back only internally, and then we have full freedom of representation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used Sequence
instead of Iterable
and Tuple
. I think it is the most expressive one.
[{"cache": _LocalCache(max_size=3)}], | ||
indirect=True, | ||
) | ||
def test_cache_lru_eviction(self, r): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as in the async test (naming)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, same reason as above.
redis/asyncio/connection.py
Outdated
@@ -730,6 +730,27 @@ def _add_to_local_cache( | |||
): | |||
self.client_cache.set(command, response, keys) | |||
|
|||
def flush_cache(self): | |||
try: | |||
if self.client_cache: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need the if
inside the try
block?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The try
is meant to compensate for the if
? You mean we could leave the if
without the try
? I would find that cleaner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The if-statement will prevent the call to flush/delete_command/invalidate_key that would cause the AttributeError if client_cache is falsy. The try-catch-block will also catch an AttributeError if client_cache is truthy but doesn't provide the method.
I'm missing to much context to say which is more desirable. But if it can be assumed, that a thruthy client_cache always also provides the methods, then the if-statement and try-catch are redundant and it becomes a question of style. You can read more on that here https://realpython.com/python-lbyl-vs-eafp/
Best regards, a random passerby XD
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @CBerndt-Work for your nice explanation. The article is very well written. I realized that I am by now too conditioned towards LBYL, from other languages. Especially here, using AttributeError
, which signals issues with the code structure, not with data. So I went for the if
s.
* Streamline client side caching API typing Streamline the typing of the client side caching API. Some of the methods are defining commands of type `str`, while in reality tuples are being sent for those parameters. Add client side cache tests for Sentinels. In order to make this work, fix the sentinel configuration in the docker-compose stack. Add a test for client side caching with a truly custom cache, not just injecting our internal cache structure as custom. Add a test for client side caching where two different types of commands use the same key, to make sure they invalidate each others cached data. * Fixes after running tests against RE * More test cases * Fix async tests * Tests for raw commands * Change terminology for allow/deny lists * Add test for single connection * Make sure flushing the cache works everywhere * Reenable some tests for cluster too * Align cache typings at abstract level * Use Sequence instead of Iterable for types * Remove some exceptions in favor of ifs --------- Co-authored-by: Gabriel Erzse <gabriel.erzse@redis.com>
* Streamline client side caching API typing Streamline the typing of the client side caching API. Some of the methods are defining commands of type `str`, while in reality tuples are being sent for those parameters. Add client side cache tests for Sentinels. In order to make this work, fix the sentinel configuration in the docker-compose stack. Add a test for client side caching with a truly custom cache, not just injecting our internal cache structure as custom. Add a test for client side caching where two different types of commands use the same key, to make sure they invalidate each others cached data. * Fixes after running tests against RE * More test cases * Fix async tests * Tests for raw commands * Change terminology for allow/deny lists * Add test for single connection * Make sure flushing the cache works everywhere * Reenable some tests for cluster too * Align cache typings at abstract level * Use Sequence instead of Iterable for types * Remove some exceptions in favor of ifs --------- Co-authored-by: Gabriel Erzse <gabriel.erzse@redis.com>
* Streamline client side caching API typing Streamline the typing of the client side caching API. Some of the methods are defining commands of type `str`, while in reality tuples are being sent for those parameters. Add client side cache tests for Sentinels. In order to make this work, fix the sentinel configuration in the docker-compose stack. Add a test for client side caching with a truly custom cache, not just injecting our internal cache structure as custom. Add a test for client side caching where two different types of commands use the same key, to make sure they invalidate each others cached data. * Fixes after running tests against RE * More test cases * Fix async tests * Tests for raw commands * Change terminology for allow/deny lists * Add test for single connection * Make sure flushing the cache works everywhere * Reenable some tests for cluster too * Align cache typings at abstract level * Use Sequence instead of Iterable for types * Remove some exceptions in favor of ifs --------- Co-authored-by: Gabriel Erzse <gabriel.erzse@redis.com>
Pull Request check-list
Please make sure to review and check all of these items:
NOTE: these things are not required to open a PR and can be done
afterwards / while the PR is open.
Description of change
Streamline the typing of the client side caching API. Some of the methods are defining commands of type
str
, while in reality tuples are being sent for those parameters.Add client side cache tests for Sentinels. In order to make this work, fix the sentinel configuration in the docker-compose stack.
Add a test for client side caching with a truly custom cache, not just injecting our internal cache structure as custom.
Add a test for client side caching where two different types of commands use the same key, to make sure they invalidate each others cached data.