Skip to content

Commit

Permalink
perf: optimize find_all_tags_for with caching and indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
magajh committed Jan 3, 2025
1 parent 0f5626e commit 974c8f7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on ## [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to ## [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v5.0.1 - 2025-01-03

### [4.2.0](https://github.com/eduNEXT/eox-tagging/compare/v5.0.0...v5.0.1) (2025-01-03)

#### Fixed

- Optimized `find_all_tags_for` method in the `Tag` model for better performance on large datasets.
- Added caching for `ContentType` lookups to reduce redundant database queries.
- Added an index on `target_type` and `target_object_id` fields in the `Tag` model to speed up filtering operations.
- Improved query logic by removing unnecessary memory-intensive operations in `find_all_tags_for`.

## v5.0.0 - 2022-10-09

### [4.2.0](https://github.com/eduNEXT/eox-tagging/compare/v4.1.0...v5.0.0) (2022-10-09)
Expand Down
37 changes: 28 additions & 9 deletions eox_tagging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@

PROXY_MODEL_NAME = "opaquekeyproxymodel"

CONTENT_TYPE_CACHE = {}


def get_content_type_cached(model_name):
"""
Retrieves the ContentType object for the given model name from a cache.
This function checks if the ContentType object for the given model name
is already present in the in-memory cache. If it exists, the cached object
is returned. Otherwise, the ContentType is fetched from the database, stored
in the cache, and then returned.
"""
if model_name not in CONTENT_TYPE_CACHE:
CONTENT_TYPE_CACHE[model_name] = ContentType.objects.get(model=model_name)
return CONTENT_TYPE_CACHE[model_name]


class TagQuerySet(QuerySet):
""" Tag queryset used as manager."""
Expand Down Expand Up @@ -74,9 +90,10 @@ def find_all_tags_for(self, target_type, target_id):
except ObjectDoesNotExist:
return self.none()

target_ids = list(target.values_list("id", flat=True))

return self.filter(target_type=ctype, target_object_id__in=target_ids)
return self.filter(
target_type=ctype,
target_object_id__in=target.values_list("id", flat=True)
)

def find_all_tags_by_type(self, object_type):
"""Returns all tags with target_type equals to object_type."""
Expand All @@ -91,27 +108,25 @@ def _get_object_for_this_type(self, object_type, object_id):
Function that given an object type returns the correct content type and a list of objects
associated.
"""
ctype = ContentType.objects.get(model=object_type)
ctype = get_content_type_cached(object_type)
object_type = object_type.lower()

if object_type.lower() == PROXY_MODEL_NAME:
if object_type == PROXY_MODEL_NAME:
object_id = object_id.get("course_id")
object_id = {
"opaque_key": CourseKey.from_string(object_id),
}

if object_type.lower() in ["courseenrollment", "generatedcertificate"]:

if object_type in ["courseenrollment", "generatedcertificate"]:
course_id = object_id.get("course_id")
username = object_id.get("username")
verify_uuid = object_id.get("verify_uuid")
object_id = {}

if course_id:
object_id["course_id"] = CourseKey.from_string(course_id)

if username:
object_id["user__username"] = username

if verify_uuid:
object_id["verify_uuid"] = verify_uuid

Expand Down Expand Up @@ -208,6 +223,10 @@ class Meta:
verbose_name_plural = "tags"
app_label = "eox_tagging"

indexes = [
models.Index(fields=["target_type", "target_object_id"], name="target_index"),
]

def __str__(self):
return str(self.tag_value)

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 5.0.0
current_version = 5.0.1
commit = False
tag = False

Expand Down

0 comments on commit 974c8f7

Please sign in to comment.