-
-
Notifications
You must be signed in to change notification settings - Fork 23
Improve Caching Schema #50
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
8f57a9c
clean up caching schema
Archmonger acae088
remove duplicate comment
Archmonger 38f96af
delete stale keys
Archmonger e3ba4f0
use Django async caching
Archmonger 9b6eaf9
remove Python 3.7 tests
Archmonger 99086b7
need quotes around python-version in matrix
Archmonger b516bb2
use aiofile
Archmonger 95144e7
Merge remote-tracking branch 'upstream/main' into fix-caching
Archmonger a2b4947
re-add spacing in pkg-deps
Archmonger a631c0d
fix docstring
Archmonger fe2dd92
remove useless delete
Archmonger 194aba4
set IDOM_CACHE to the cache backend object
Archmonger 5e24342
Revert "remove useless delete"
Archmonger 7ee012c
remove unused import
Archmonger d07c00d
change py and django reqs
Archmonger fca2201
protect against malicious path attacks
Archmonger 0a340a2
dir agnostic cache key invalidation
Archmonger 1c103dc
clean up comments
Archmonger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
channels <4.0.0 | ||
idom >=0.34.0, <0.35.0 | ||
aiofile >=3.0, <4.0 |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,34 @@ | ||
import asyncio | ||
import functools | ||
import os | ||
|
||
from django.core.cache import caches | ||
from aiofile import async_open | ||
from django.core.exceptions import SuspiciousOperation | ||
from django.http import HttpRequest, HttpResponse | ||
from idom.config import IDOM_WED_MODULES_DIR | ||
|
||
from .config import IDOM_WEB_MODULE_CACHE, IDOM_WEB_MODULE_LRU_CACHE_SIZE | ||
|
||
|
||
if IDOM_WEB_MODULE_CACHE is None: | ||
|
||
def async_lru_cache(*lru_cache_args, **lru_cache_kwargs): | ||
def async_lru_cache_decorator(async_function): | ||
@functools.lru_cache(*lru_cache_args, **lru_cache_kwargs) | ||
def cached_async_function(*args, **kwargs): | ||
coroutine = async_function(*args, **kwargs) | ||
return asyncio.ensure_future(coroutine) | ||
|
||
return cached_async_function | ||
|
||
return async_lru_cache_decorator | ||
|
||
@async_lru_cache(IDOM_WEB_MODULE_LRU_CACHE_SIZE) | ||
async def web_modules_file(request: HttpRequest, file: str) -> HttpResponse: | ||
file_path = IDOM_WED_MODULES_DIR.current.joinpath(*file.split("/")) | ||
return HttpResponse(file_path.read_text(), content_type="text/javascript") | ||
|
||
else: | ||
_web_module_cache = caches[IDOM_WEB_MODULE_CACHE] | ||
|
||
async def web_modules_file(request: HttpRequest, file: str) -> HttpResponse: | ||
file = IDOM_WED_MODULES_DIR.current.joinpath(*file.split("/")).absolute() | ||
last_modified_time = os.stat(file).st_mtime | ||
cache_key = f"{file}:{last_modified_time}" | ||
|
||
response = _web_module_cache.get(cache_key) | ||
if response is None: | ||
response = HttpResponse(file.read_text(), content_type="text/javascript") | ||
_web_module_cache.set(cache_key, response, timeout=None) | ||
|
||
return response | ||
from .config import IDOM_CACHE | ||
|
||
|
||
async def web_modules_file(request: HttpRequest, file: str) -> HttpResponse: | ||
"""Gets JavaScript required for IDOM modules at runtime. These modules are | ||
returned from cache if available.""" | ||
web_modules_dir = IDOM_WED_MODULES_DIR.current | ||
path = web_modules_dir.joinpath(*file.split("/")).absolute() | ||
|
||
# Prevent attempts to walk outside of the web modules dir | ||
if str(web_modules_dir) != os.path.commonpath((path, web_modules_dir)): | ||
raise SuspiciousOperation( | ||
"Attempt to access a directory outside of IDOM_WED_MODULES_DIR." | ||
) | ||
|
||
# Fetch the file from cache, if available | ||
last_modified_time = os.stat(path).st_mtime | ||
cache_key = f"django_idom:web_module:{str(path).lstrip(str(web_modules_dir))}" | ||
response = await IDOM_CACHE.aget(cache_key, version=last_modified_time) | ||
if response is None: | ||
async with async_open(path, "r") as fp: | ||
response = HttpResponse(await fp.read(), content_type="text/javascript") | ||
await IDOM_CACHE.adelete(cache_key) | ||
await IDOM_CACHE.aset( | ||
cache_key, response, timeout=None, version=last_modified_time | ||
) | ||
return response |
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.
Uh oh!
There was an error while loading. Please reload this page.