Repository navigation
refactor(cache): split cache.py into smaller modules (#422) - #436
Merged
Merged
Conversation
cache.py held key building, vary handling, Cache-Control rendering, stored-response handling, response rendering and the decorator in one 1,800-line file. Move each part into a private module beside it: - _key_builders.py: build_cache_key, default_key_builder, key builder checks - _vary.py: vary= validation and key components - _cache_control.py: CacheControl, the decorator's header, unshareable checks - _stored_response.py: cacheable headers, Age, ETags, 304s, the stored entry - _rendering.py: running the handler, the response, dependency headers - _callables.py: whether a handler or key builder returns a coroutine cache.py keeps the decorator, invalidate() and the credential bypass, and still exports every name it defined (now listed in __all__). Every module logs under the documented fastapi_cachex.cache logger. The only code change is _entry_for(), which builds the stored CacheEntry so the clock behind stored_at and Age lives in one module. No behaviour change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #422.
fastapi_cachex/cache.pyhad grown to about 1,800 lines. This PR splits it into private modules next to it. The public API and behaviour do not change._key_builders.pybuild_cache_key,default_key_builder, key builder checks_vary.pyvary=validation and the key components it adds_cache_control.pyCacheControl, the decorator'sCache-Control, checks for responses that must not be shared_stored_response.pyAge, ETags, 304s, the storedCacheEntry_rendering.py_callables.pycache.pyinvalidate(), the credential-bypass warnings (about 970 lines)How
ast.get_source_segment. All of them are byte-identical, with one exception: the decorator now calls the new_entry_for()instead of building theCacheEntryinline, so the clock behindstored_atandAgelives in one module._now,_ADAPTER_ATTR, the_BypassWarnerlock) exists in exactly one place.Compatibility
fastapi_cachex.cachedefined on master is still importable from it. These names are now listed in__all__, andtests/test_cache_module.pypins them.fastapi_cachex.cachelogger. The new test checks this too.docs/apireferences (fastapi_cachex.cache.cache,invalidate,default_key_builder) still resolve.cache.pyonly imported for its own use are no longer reachable throughfastapi_cachex.cache. For example,from fastapi_cachex.cache import CacheEntrynow fails; import it fromfastapi_cachex.types. Their documented homes are unchanged.fastapi_cachex.cache._nowmust patchfastapi_cachex._stored_response._now. The repository's own tests are repointed.changelog.d/README.md).Docs
CACHE_FLOW.md(EN and zh-TW) no longer says that all the behaviour lives incache.py. It now names the module for each part.Checks
mypy fastapi_cachex --strict,mypy testsandmypy scripts--strict_key_buildersimported from_renderingmoved to_callables.py, and the__all__comment was reworded.