Skip to content
Snippets Groups Projects
Verified Commit 756eb1dc authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Implement base fo caching

This adds a private module _caching that wraps both the Django caching frameworks and the ucache
libray. Both happen to expose the same (or at least sufficiently identical) API. If neither is
usable, a dummy cache class mocking the API, but doing nothing, is instantiated.
parent 8c0842b6
No related branches found
No related tags found
No related merge requests found
from typing import Any, Optional
try:
# Try importing Django app settings to find out whether we are running inside Django
from django.core.exceptions import ImproperlyConfigured
from ..django import settings as django_settings
except (ImportError, ImproperlyConfigured):
USING_DJANGO = False
else:
from django.core.cache import caches as django_caches
USING_DJANGO = True
finally:
del ImproperlyConfigured
try:
# Try importings ucache's most simple backend
from ucache import SqliteCache as UCSqliteCache
except ImportError:
USING_UCACHE = False
else:
USING_UCACHE = True
if USING_DJANGO:
# Get Django cache designated by BBB_CACHE_NAME setting, or "default"
cache = django_caches[django_settings.CACHE_NAME]
elif USING_UCACHE:
from getpass import getuser
from pathlib import Path
from tempfile import gettempdir
# Create temporary directory to hold cache data
tempdir = Path(gettempdir(), f"python-bigbluebutton2-{getuser()}")
tempdir.mkdir(mode=0o700, parents=True, exist_ok=True)
cache = UCSqliteCache(str(tempdir.joinpath("cache.sqlite")))
else:
# No caching backend found, make caching a no-op
class DummyCache:
def _dummy(self, *args, **lwargs) -> None:
return
get = _dummy
get_many = _dummy
set = _dummy
set_many = _dummy
delete = _dummy
delete_many = _dummy
flush = _dummy
clean_expired = _dummy
__getitem__ = get
__setitem__ = set
__delitem__ = delete
cache = DummyCache()
...@@ -6,6 +6,7 @@ from dataclasses import dataclass, field ...@@ -6,6 +6,7 @@ from dataclasses import dataclass, field
from enum import Enum from enum import Enum
from typing import TYPE_CHECKING, Any, Dict, Optional from typing import TYPE_CHECKING, Any, Dict, Optional
from ._caching import cache
from .util import camel_to_snake, snake_to_camel, to_field_type from .util import camel_to_snake, snake_to_camel, to_field_type
if TYPE_CHECKING: # pragma: no cover if TYPE_CHECKING: # pragma: no cover
......
...@@ -22,6 +22,7 @@ from uuid import uuid1 ...@@ -22,6 +22,7 @@ from uuid import uuid1
import requests import requests
import xmltodict import xmltodict
from ._caching import cache
from .attendee import Attendee from .attendee import Attendee
from .loadbalancing import CHECKERS as lb_checkers from .loadbalancing import CHECKERS as lb_checkers
from .meeting import Meeting from .meeting import Meeting
......
...@@ -4,6 +4,7 @@ import logging ...@@ -4,6 +4,7 @@ import logging
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Dict, Optional from typing import TYPE_CHECKING, Any, Dict, Optional
from ._caching import cache
from .attendee import Attendee from .attendee import Attendee
from .util import camel_to_snake, snake_to_camel, to_field_type from .util import camel_to_snake, snake_to_camel, to_field_type
......
...@@ -24,3 +24,5 @@ USER_ID_CALLBACK = import_string( ...@@ -24,3 +24,5 @@ USER_ID_CALLBACK = import_string(
ROLE_CALLBACK = import_string( ROLE_CALLBACK = import_string(
getattr(_settings, f"{_PREFIX}_ROLE_CALLBACK", "bigbluebutton.django.util.get_role") getattr(_settings, f"{_PREFIX}_ROLE_CALLBACK", "bigbluebutton.django.util.get_role")
) )
CACHE_NAME = getattr(_settings, f"{_PREFIX}_CACHE_NAME", "default")
...@@ -534,6 +534,14 @@ optional = false ...@@ -534,6 +534,14 @@ optional = false
python-versions = "*" python-versions = "*"
version = "5.4.5" version = "5.4.5"
[[package]]
category = "main"
description = "a little orm"
name = "peewee"
optional = true
python-versions = "*"
version = "3.13.3"
[[package]] [[package]]
category = "dev" category = "dev"
description = "plugin and hook calling mechanisms for python" description = "plugin and hook calling mechanisms for python"
...@@ -1020,6 +1028,14 @@ version = "2.1" ...@@ -1020,6 +1028,14 @@ version = "2.1"
[package.dependencies] [package.dependencies]
pytz = "*" pytz = "*"
[[package]]
category = "main"
description = "lightweight and efficient caching library"
name = "ucache"
optional = true
python-versions = "*"
version = "0.1.4"
[[package]] [[package]]
category = "main" category = "main"
description = "HTTP library with thread-safe connection pooling, file post, and more." description = "HTTP library with thread-safe connection pooling, file post, and more."
...@@ -1063,12 +1079,13 @@ docs = ["sphinx", "jaraco.packaging (>=3.2)", "rst.linker (>=1.9)"] ...@@ -1063,12 +1079,13 @@ docs = ["sphinx", "jaraco.packaging (>=3.2)", "rst.linker (>=1.9)"]
testing = ["jaraco.itertools", "func-timeout"] testing = ["jaraco.itertools", "func-timeout"]
[extras] [extras]
caching = ["ucache", "peewee"]
cli = ["click", "toml", "tabulate"] cli = ["click", "toml", "tabulate"]
django = ["dicttoxml", "django"] django = ["dicttoxml", "django"]
sysstat = ["sadf"] sysstat = ["sadf"]
[metadata] [metadata]
content-hash = "5aadc276268bfd2164e4162aff25813b2211483da339ab438ba8cc78135a536b" content-hash = "ac17d9cc14afbb5058f3099a1a0effc1e126c841d8cba57f9712b38cafe8c73b"
python-versions = "^3.6" python-versions = "^3.6"
[metadata.files] [metadata.files]
...@@ -1364,6 +1381,9 @@ pbr = [ ...@@ -1364,6 +1381,9 @@ pbr = [
{file = "pbr-5.4.5-py2.py3-none-any.whl", hash = "sha256:579170e23f8e0c2f24b0de612f71f648eccb79fb1322c814ae6b3c07b5ba23e8"}, {file = "pbr-5.4.5-py2.py3-none-any.whl", hash = "sha256:579170e23f8e0c2f24b0de612f71f648eccb79fb1322c814ae6b3c07b5ba23e8"},
{file = "pbr-5.4.5.tar.gz", hash = "sha256:07f558fece33b05caf857474a366dfcc00562bca13dd8b47b2b3e22d9f9bf55c"}, {file = "pbr-5.4.5.tar.gz", hash = "sha256:07f558fece33b05caf857474a366dfcc00562bca13dd8b47b2b3e22d9f9bf55c"},
] ]
peewee = [
{file = "peewee-3.13.3.tar.gz", hash = "sha256:1269a9736865512bd4056298003aab190957afe07d2616cf22eaf56cb6398369"},
]
pluggy = [ pluggy = [
{file = "pluggy-0.13.1-py2.py3-none-any.whl", hash = "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"}, {file = "pluggy-0.13.1-py2.py3-none-any.whl", hash = "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"},
{file = "pluggy-0.13.1.tar.gz", hash = "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0"}, {file = "pluggy-0.13.1.tar.gz", hash = "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0"},
...@@ -1603,6 +1623,9 @@ tzlocal = [ ...@@ -1603,6 +1623,9 @@ tzlocal = [
{file = "tzlocal-2.1-py2.py3-none-any.whl", hash = "sha256:e2cb6c6b5b604af38597403e9852872d7f534962ae2954c7f35efcb1ccacf4a4"}, {file = "tzlocal-2.1-py2.py3-none-any.whl", hash = "sha256:e2cb6c6b5b604af38597403e9852872d7f534962ae2954c7f35efcb1ccacf4a4"},
{file = "tzlocal-2.1.tar.gz", hash = "sha256:643c97c5294aedc737780a49d9df30889321cbe1204eac2c2ec6134035a92e44"}, {file = "tzlocal-2.1.tar.gz", hash = "sha256:643c97c5294aedc737780a49d9df30889321cbe1204eac2c2ec6134035a92e44"},
] ]
ucache = [
{file = "ucache-0.1.4.tar.gz", hash = "sha256:f2f3d6c10d036d1fdd297d08372586d7868a5ac748da6def38f5e8d7cf6238b1"},
]
urllib3 = [ urllib3 = [
{file = "urllib3-1.25.9-py2.py3-none-any.whl", hash = "sha256:88206b0eb87e6d677d424843ac5209e3fb9d0190d0ee169599165ec25e9d9115"}, {file = "urllib3-1.25.9-py2.py3-none-any.whl", hash = "sha256:88206b0eb87e6d677d424843ac5209e3fb9d0190d0ee169599165ec25e9d9115"},
{file = "urllib3-1.25.9.tar.gz", hash = "sha256:3018294ebefce6572a474f0604c2021e33b3fd8006ecd11d62107a5d2a963527"}, {file = "urllib3-1.25.9.tar.gz", hash = "sha256:3018294ebefce6572a474f0604c2021e33b3fd8006ecd11d62107a5d2a963527"},
......
...@@ -40,6 +40,8 @@ sadf = { version = "^0.1.2", optional = true } ...@@ -40,6 +40,8 @@ sadf = { version = "^0.1.2", optional = true }
django = {version = "^3.0.6", optional = true} django = {version = "^3.0.6", optional = true}
dicttoxml = {version = "^1.7.4", optional = true} dicttoxml = {version = "^1.7.4", optional = true}
defusedxml = "^0.6.0" defusedxml = "^0.6.0"
ucache = {version = "^0.1.4", optional = true}
peewee = {version = "^3.13.3", optional = true}
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
pytest = "^5.4.2" pytest = "^5.4.2"
...@@ -65,6 +67,7 @@ sphinxcontrib-django = "^0.5.1" ...@@ -65,6 +67,7 @@ sphinxcontrib-django = "^0.5.1"
[tool.poetry.extras] [tool.poetry.extras]
cli = ["click", "toml", "tabulate", "xdg"] cli = ["click", "toml", "tabulate", "xdg"]
django = ["dicttoxml", "django"] django = ["dicttoxml", "django"]
caching = ["ucache", "peewee"]
sysstat = ["sadf"] sysstat = ["sadf"]
[tool.poetry.scripts] [tool.poetry.scripts]
......
...@@ -7,7 +7,7 @@ envlist = py38 ...@@ -7,7 +7,7 @@ envlist = py38
whitelist_externals = poetry whitelist_externals = poetry
skip_install = true skip_install = true
envdir = {toxworkdir}/globalenv envdir = {toxworkdir}/globalenv
commands_pre = poetry install -E cli -E django -E sysstat commands_pre = poetry install -E cli -E django -E sysstat -E caching
commands = commands =
poetry run pytest --doctest-modules --cov-report=term-missing --cov=bigbluebutton/ {posargs} bigbluebutton/ tests/ poetry run pytest --doctest-modules --cov-report=term-missing --cov=bigbluebutton/ {posargs} bigbluebutton/ tests/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment