Skip to content
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

bugfix: Unique caches for simple_cache_middleware instances #2979

Merged

Conversation

fselmo
Copy link
Collaborator

@fselmo fselmo commented Jun 5, 2023

What was wrong?

closes #2977

How was it fixed?

  • For undefined cache arguments to *construct_simple_cache_middleware methods, only create the cache at the middleware definition, not at the constructor method. This makes sure that a fresh cache is created each time the middleware is called (once for each instance). This was previously the case and I believe I broke it when we refactored the LRU cache out of this middleware.

  • Tests added inspired by @cygnusv

Todo:

Cute Animal Picture

20230604_154722

fselmo added a commit to fselmo/web3.py that referenced this pull request Jun 5, 2023
@fselmo fselmo force-pushed the bugfix-simple-cache-unique-cache-per-instance branch from 29aed4f to 9c32974 Compare June 5, 2023 22:54
@fselmo fselmo marked this pull request as ready for review June 5, 2023 23:08
@fselmo fselmo requested review from pacrob, reedsa and kclowes June 5, 2023 23:08
At present, if the ``simple_cache_middleware`` is imported and added to multiple instances of ``Web3``, these instances end up sharing a cache. This happens because the middleware is only constructed once and where we instantiate the cache if it is ``None`` is in the ``construct_simple_cache_middleware`` method. By moving the cache instantiation into the actual ``simple_cache_middleware``, we ensure that with each call to ``simple_cache_middleware``, a fresh cache will be instantiated if the ``cache`` was ``None`` to begin with.
@fselmo fselmo force-pushed the bugfix-simple-cache-unique-cache-per-instance branch from 9c32974 to e0f1786 Compare June 6, 2023 15:22
Copy link
Contributor

@reedsa reedsa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking, just one suggestion for docs.

web3/middleware/cache.py Show resolved Hide resolved
Copy link
Contributor

@pacrob pacrob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good. An explanation + (maybe usage example?) in the docs would be sweet, but could be a separate PR too.

@fselmo fselmo requested a review from pacrob June 6, 2023 19:19
@fselmo
Copy link
Collaborator Author

fselmo commented Jun 6, 2023

Code looks good. An explanation + (maybe usage example?) in the docs would be sweet, but could be a separate PR too.

I updated the documentation for now and made sure it was up to date (it was poor to begin with and not up to date). I think I'm not understanding what makes this stand out from other middlewares and needs its own explanation other than the doc description.

Maybe we create a ticket to better highlight that request and it becomes as separate PR?

@fselmo fselmo merged commit 60cfce3 into ethereum:main Jun 6, 2023
fselmo added a commit that referenced this pull request Jun 6, 2023
@fselmo fselmo deleted the bugfix-simple-cache-unique-cache-per-instance branch June 6, 2023 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

web3.middlewares.simple_cache_middleware only works correctly for a single web3 instance
4 participants