Skip to content

Commit

Permalink
[DONE] Use cache to store video data (#969)
Browse files Browse the repository at this point in the history
* Use cache to store video data - get it in context - create command to store video data

* change name of video context processor

* add some pydoc

* update cache timeout to 600 seconds - use DRY and call context cache video data instead of repeat code

* use delete many to clear cache
  • Loading branch information
ptitloup authored Oct 3, 2023
1 parent 6ead37b commit 483ca6f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pod/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
# Local contexts
"pod.main.context_processors.context_settings",
"pod.main.context_processors.context_footer",
"pod.video.context_processors.context_navbar",
"pod.video.context_processors.context_video_data",
"pod.video.context_processors.context_video_settings",
"pod.authentication.context_processors.context_authentication_settings",
"pod.recorder.context_processors.context_recorder_settings",
Expand Down
74 changes: 44 additions & 30 deletions pod/video/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.db.models import OuterRef

from datetime import timedelta
from django.core.cache import cache
from django.contrib.sites.shortcuts import get_current_site
from pod.video_encode_transcript.models import EncodingVideo
from pod.video_encode_transcript.models import PlaylistVideo
Expand All @@ -19,7 +20,7 @@
HIDE_USER_FILTER = getattr(django_settings, "HIDE_USER_FILTER", False)
OEMBED = getattr(django_settings, "OEMBED", False)
USE_STATS_VIEW = getattr(django_settings, "USE_STATS_VIEW", False)

CACHE_VIDEO_DEFAULT_TIMEOUT = getattr(django_settings, "CACHE_VIDEO_DEFAULT_TIMEOUT", 600)
__AVAILABLE_VIDEO_FILTER__ = {
"encoding_in_progress": False,
"is_draft": False,
Expand Down Expand Up @@ -78,37 +79,50 @@ def context_video_settings(request):
return new_settings


def context_navbar(request):
types = (
Type.objects.filter(
sites=get_current_site(request),
video__is_draft=False,
video__sites=get_current_site(request),
def context_video_data(request):
"""Get video data in cache, if not, create and add it in cache."""
types = cache.get('TYPES')
if types is None:
types = (
Type.objects.filter(
sites=get_current_site(request),
video__is_draft=False,
video__sites=get_current_site(request),
)
.distinct()
.annotate(video_count=Count("video", distinct=True))
)
.distinct()
.annotate(video_count=Count("video", distinct=True))
)

disciplines = (
Discipline.objects.filter(
site=get_current_site(request),
video__is_draft=False,
video__sites=get_current_site(request),
cache.set("TYPES", types, timeout=CACHE_VIDEO_DEFAULT_TIMEOUT)

disciplines = cache.get('DISCIPLINES')
if disciplines is None:
disciplines = (
Discipline.objects.filter(
site=get_current_site(request),
video__is_draft=False,
video__sites=get_current_site(request),
)
.distinct()
.annotate(video_count=Count("video", distinct=True))
)
.distinct()
.annotate(video_count=Count("video", distinct=True))
)

v_filter = get_available_videos_filter(request)

aggregate_videos = v_filter.aggregate(duration=Sum("duration"), number=Count("id"))

VIDEOS_COUNT = aggregate_videos["number"]
VIDEOS_DURATION = (
str(timedelta(seconds=aggregate_videos["duration"]))
if aggregate_videos["duration"]
else 0
)
cache.set("DISCIPLINES", disciplines, timeout=CACHE_VIDEO_DEFAULT_TIMEOUT)

VIDEOS_COUNT = cache.get('VIDEOS_COUNT')
VIDEOS_DURATION = cache.get('VIDEOS_DURATION')
if VIDEOS_COUNT is None:
v_filter = get_available_videos_filter(request)
aggregate_videos = v_filter.aggregate(
duration=Sum("duration"),
number=Count("id")
)
VIDEOS_COUNT = aggregate_videos["number"]
cache.set("VIDEOS_COUNT", VIDEOS_COUNT, timeout=CACHE_VIDEO_DEFAULT_TIMEOUT)
VIDEOS_DURATION = (
str(timedelta(seconds=aggregate_videos["duration"]))
if aggregate_videos["duration"]
else 0
)
cache.set("VIDEOS_DURATION", VIDEOS_DURATION, timeout=CACHE_VIDEO_DEFAULT_TIMEOUT)

return {
"TYPES": types,
Expand Down
28 changes: 28 additions & 0 deletions pod/video/management/commands/cache_video_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from django.core.management.base import BaseCommand
from django.core.cache import cache
from pod.video.context_processors import context_video_data
from django.core import serializers
import json


class Command(BaseCommand):
"""Command to store video data in cache."""
help = "Store video data in django cache : " \
+ "types, discipline, video count and videos duration"

def handle(self, *args, **options):
"""Function called to store video data in cache."""
cache.delete_many(['DISCIPLINES', 'VIDEOS_COUNT', 'VIDEOS_DURATION', 'TYPES'])
video_data = context_video_data(request=None)
msg = 'Successfully store video data in cache'
for data in video_data:
try:
msg += "\n %s : %s" % (
data,
json.dumps(serializers.serialize("json", video_data[data]))
)
except (TypeError, AttributeError):
msg += "\n %s : %s" % (data, video_data[data])
self.stdout.write(
self.style.SUCCESS(msg)
)

0 comments on commit 483ca6f

Please sign in to comment.