From b9556211be9975d396530cea214326e2a918804a Mon Sep 17 00:00:00 2001 From: Muhammad Umar Khan <42294172+mumarkhan999@users.noreply.github.com> Date: Thu, 10 Apr 2025 15:22:28 +0500 Subject: [PATCH] chore: fix cache content size calculation error (#36511) Co-authored-by: M Umar Khan --- lms/djangoapps/courseware/courses.py | 5 +++-- openedx/core/djangoapps/content/block_structure/store.py | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lms/djangoapps/courseware/courses.py b/lms/djangoapps/courseware/courses.py index 62bd6bf5c9..5dc4a7ce91 100644 --- a/lms/djangoapps/courseware/courses.py +++ b/lms/djangoapps/courseware/courses.py @@ -795,10 +795,11 @@ def get_assignments_grades(user, course_id, cache_timeout): collected_block_structure = get_block_structure_manager(course_id).get_collected() total_bytes_in_one_mb = 1024 * 1024 - data_size_in_mbs = round(len(pickle.dumps(collected_block_structure)) / total_bytes_in_one_mb, 2) - if data_size_in_mbs < total_bytes_in_one_mb * 2: + data_size_in_bytes = len(pickle.dumps(collected_block_structure)) + if data_size_in_bytes < total_bytes_in_one_mb * 2: cache.set(cache_key, collected_block_structure, cache_timeout) else: + data_size_in_mbs = round(data_size_in_bytes / total_bytes_in_one_mb, 2) # .. custom_attribute_name: collected_block_structure_size_in_mbs # .. custom_attribute_description: contains the data chunk size in MBs. The size on which # the memcached client failed to store value in cache. diff --git a/openedx/core/djangoapps/content/block_structure/store.py b/openedx/core/djangoapps/content/block_structure/store.py index 409dae988e..bb6359b9d3 100644 --- a/openedx/core/djangoapps/content/block_structure/store.py +++ b/openedx/core/djangoapps/content/block_structure/store.py @@ -137,8 +137,9 @@ class BlockStructureStore: """ cache_key = self._encode_root_cache_key(bs_model) total_bytes_in_one_mb = 1024 * 1024 - data_size_in_mbs = round(len(serialized_data) / total_bytes_in_one_mb, 2) - if data_size_in_mbs < total_bytes_in_one_mb * 2: + data_size_in_bytes = len(serialized_data) + data_size_in_mbs = round(data_size_in_bytes / total_bytes_in_one_mb, 2) + if data_size_in_bytes < total_bytes_in_one_mb * 2: self._cache.set(cache_key, serialized_data, timeout=config.cache_timeout_in_seconds()) logger.info("BlockStructure: Added to cache; %s, size: %.2fMB", bs_model, data_size_in_mbs) else: