Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
clean up caching schema
  • Loading branch information
Archmonger committed Jan 12, 2022
commit 8f57a9c11288e6c88700aa993a8778897d193309
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,15 @@ You may configure additional options as well:
# the base URL for all IDOM-releated resources
IDOM_BASE_URL: str = "_idom/"

# Set cache size limit for loading JS files for IDOM.
# Only applies when not using Django's caching framework (see below).
IDOM_WEB_MODULE_LRU_CACHE_SIZE: int | None = None

# Maximum seconds between two reconnection attempts that would cause the client give up.
# 0 will disable reconnection.
IDOM_WS_MAX_RECONNECT_DELAY: int = 604800

# Configure a cache for loading JS files
CACHES = {
# Configure a cache for loading JS files for IDOM
"idom_web_modules": {"BACKEND": ...},
# If the above cache is not configured, then we'll use the "default" instead
"default": {"BACKEND": ...},
# If "idom" cache is not configured, then we'll use the "default" instead
"idom": {"BACKEND": ...},
}
```

Expand Down
16 changes: 3 additions & 13 deletions src/django_idom/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,7 @@
IDOM_WEB_MODULES_URL = IDOM_BASE_URL + "web_module/"
IDOM_WS_MAX_RECONNECT_DELAY = getattr(settings, "IDOM_WS_MAX_RECONNECT_DELAY", 604800)

_CACHES = getattr(settings, "CACHES", {})
if _CACHES:
if "idom_web_modules" in getattr(settings, "CACHES", {}):
IDOM_WEB_MODULE_CACHE = "idom_web_modules"
else:
IDOM_WEB_MODULE_CACHE = DEFAULT_CACHE_ALIAS
if "idom" in getattr(settings, "CACHES", {}):
IDOM_CACHE = "idom"
else:
IDOM_WEB_MODULE_CACHE = None


# the LRU cache size for the route serving IDOM_WEB_MODULES_DIR files
IDOM_WEB_MODULE_LRU_CACHE_SIZE = getattr(
settings, "IDOM_WEB_MODULE_LRU_CACHE_SIZE", None
)
IDOM_CACHE = DEFAULT_CACHE_ALIAS
45 changes: 12 additions & 33 deletions src/django_idom/views.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,22 @@
import asyncio
import functools
import os

from django.core.cache import caches
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
from .config import IDOM_CACHE


if IDOM_WEB_MODULE_CACHE is None:
idom_cache = caches[IDOM_CACHE]

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
async def web_modules_file(request: HttpRequest, file: str) -> HttpResponse:
"""Gets a web modules file. Web modules files from cache"""
path = IDOM_WED_MODULES_DIR.current.joinpath(*file.split("/")).absolute()
last_modified_time = os.stat(path).st_mtime
cache_key = f"django_idom:{path}"
response = idom_cache.get(cache_key, version=last_modified_time)
if response is None:
response = HttpResponse(path.read_text(), content_type="text/javascript")
idom_cache.set(cache_key, response, timeout=None, version=last_modified_time)
return response