Skip to content

Commit b3d4a25

Browse files
committed
Remove cache for likes count, reduce cache timeout
1 parent c678956 commit b3d4a25

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

core/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
ONE_DAY_IN_SECONDS = 60 * 60 * 24
22
ONE_WEEK_IN_SECONDS = ONE_DAY_IN_SECONDS * 7
3-
VIEWS_CACHING_TIMEOUT = 60 * 15
4-
LIKES_CACHING_TIMEOUT = 60 * 15
3+
VIEWS_CACHING_TIMEOUT = 60 * 1
4+
LIKES_CACHING_TIMEOUT = 60 * 1

core/services.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from django.contrib.contenttypes.models import ContentType
33
from django.core.cache import cache
44

5-
from core.constants import VIEWS_CACHING_TIMEOUT, LIKES_CACHING_TIMEOUT
5+
from core.constants import VIEWS_CACHING_TIMEOUT
66
from core.models import Like, View, Link
77

88
User = get_user_model()
@@ -36,15 +36,18 @@ def get_fans(obj):
3636

3737
def get_likes_count(obj):
3838
obj_type = ContentType.objects.get_for_model(obj)
39-
# cache this
40-
likes_count = cache.get(f"likes_count_{obj_type}_{obj.id}")
41-
if likes_count is None:
42-
likes_count = User.objects.filter(
43-
likes__content_type=obj_type, likes__object_id=obj.id
44-
).count()
45-
# cache for LIKES_CACHING_TIMEOUT seconds
46-
cache.set(f"likes_count_{obj_type}_{obj.id}", likes_count, LIKES_CACHING_TIMEOUT)
47-
return likes_count
39+
# todo: temp comment
40+
# likes_count = cache.get(f"likes_count_{obj_type}_{obj.id}")
41+
# if likes_count is None:
42+
# likes_count = User.objects.filter(
43+
# likes__content_type=obj_type, likes__object_id=obj.id
44+
# ).count()
45+
# # cache for LIKES_CACHING_TIMEOUT seconds
46+
# cache.set(f"likes_count_{obj_type}_{obj.id}", likes_count, LIKES_CACHING_TIMEOUT)
47+
return User.objects.filter(
48+
likes__content_type=obj_type, likes__object_id=obj.id
49+
).count()
50+
# return likes_count
4851

4952

5053
def set_like(obj, user, is_liked):

0 commit comments

Comments
 (0)