Skip to content

Commit

Permalink
perf: add lru_cache to improve performance with multiple themes
Browse files Browse the repository at this point in the history
These changes should improve the performance caused by the file I/O
when it's running in docker, using lru_cache to save thousands of calls to listdir
when running with a handful of themes defined in COMPREHENSIVE_THEME_DIRS.
  • Loading branch information
Alec4r committed Oct 6, 2022
1 parent 5245c69 commit 1a1e4ad
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lms/djangoapps/courseware/tests/test_comprehensive_theming.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from common.djangoapps import edxmako
from openedx.core.djangoapps.theming.tests.test_util import with_comprehensive_theme
from openedx.core.djangoapps.theming.helpers import get_themes
from openedx.core.djangoapps.theming.helpers_dirs import get_theme_dirs
from openedx.core.lib.tempdir import create_symlink, delete_symlink, mkdtemp_clean


Expand All @@ -20,6 +22,10 @@ def setUp(self):
# Clear the internal staticfiles caches, to get test isolation.
staticfiles.finders.get_finder.cache_clear()

# Clear cache on get_theme methods.
get_themes.cache_clear()
get_theme_dirs.cache_clear()

@with_comprehensive_theme('red-theme')
def test_red_footer(self):
"""
Expand Down
2 changes: 2 additions & 0 deletions openedx/core/djangoapps/theming/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
get_themes_unchecked
)
from openedx.core.lib.cache_utils import request_cached
from functools import lru_cache

logger = getLogger(__name__) # pylint: disable=invalid-name

Expand Down Expand Up @@ -255,6 +256,7 @@ def theme_exists(theme_name, themes_dir=None):
return False


@lru_cache
def get_themes(themes_dir=None):
"""
get a list of all themes known to the system.
Expand Down
2 changes: 2 additions & 0 deletions openedx/core/djangoapps/theming/helpers_dirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os

from path import Path
from functools import lru_cache


def get_theme_base_dirs_from_settings(theme_base_dirs=None):
Expand Down Expand Up @@ -49,6 +50,7 @@ def get_themes_unchecked(themes_base_dirs, project_root=None):
return themes


@lru_cache
def get_theme_dirs(themes_base_dir=None):
"""
Get all the theme dirs directly under a given base dir.
Expand Down
10 changes: 10 additions & 0 deletions openedx/core/djangoapps/theming/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@
get_themes,
strip_site_theme_templates_path
)
from openedx.core.djangoapps.theming.helpers_dirs import get_theme_dirs
from openedx.core.djangoapps.theming.tests.test_util import with_comprehensive_theme
from openedx.core.djangolib.testing.utils import skip_unless_cms, skip_unless_lms


class TestHelpers(TestCase):
"""Test comprehensive theming helper functions."""

def setUp(self):
"""
Clear cache on get_theme methods.
"""
super().setUp()

get_themes.cache_clear()
get_theme_dirs.cache_clear()

def test_get_themes(self):
"""
Tests template paths are returned from enabled theme.
Expand Down

0 comments on commit 1a1e4ad

Please sign in to comment.