Skip to content

Commit 93b3cf9

Browse files
cache public trends (#18)
* cache public trends * trends cache timeout config
1 parent 47f9e58 commit 93b3cf9

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

api/views/trends.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
from datetime import timedelta
22

3+
from django.core.cache import cache
34
from django.db.models import Count
45
from django.http import HttpRequest
56
from django.utils import timezone
67

78
from activities.models import Hashtag, Post
89
from api import schemas
910
from api.decorators import scope_required
11+
from core.models import Config
1012
from hatchway import api_view
1113

1214

@@ -15,12 +17,14 @@
1517
def trends_tags(
1618
request: HttpRequest,
1719
limit: int = 10,
18-
offset: int | None = None,
20+
offset: int = 0,
1921
) -> list[schemas.Tag]:
20-
if limit > 40:
21-
limit = 40
22+
popular_tags = cache.get("trends_tags", None)
23+
if popular_tags is None:
24+
popular_tags = Hashtag.popular(limit=100, offset=0)
25+
cache.set("trends_tags", popular_tags, Config.system.cache_timeout_trends)
2226
return schemas.Tag.map_from_hashtags(
23-
Hashtag.popular(limit=limit, offset=offset),
27+
popular_tags[offset : offset + limit],
2428
domain=request.domain,
2529
identity=request.identity,
2630
)
@@ -31,19 +35,28 @@ def trends_tags(
3135
def trends_statuses(
3236
request: HttpRequest,
3337
limit: int = 10,
34-
offset: int | None = None,
38+
offset: int = 0,
3539
) -> list[schemas.Status]:
36-
if limit > 40:
37-
limit = 40
38-
if offset is None:
39-
offset = 0
40-
since = timezone.now().date() - timedelta(days=7)
40+
popular_post_ids = cache.get("trends_statuses", None)
41+
if popular_post_ids is None:
42+
since = timezone.now().date() - timedelta(days=7)
43+
popular_post_ids = list(
44+
Post.objects.not_hidden()
45+
.public()
46+
.filter(author__discoverable=True)
47+
.filter(published__gte=since)
48+
.annotate(num_interactions=Count("interactions"))
49+
.filter(num_interactions__gte=1)
50+
.order_by("-num_interactions", "-published")
51+
.values_list("id", flat=True)[:100]
52+
)
53+
cache.set(
54+
"trends_statuses", popular_post_ids, Config.system.cache_timeout_trends
55+
)
4156
posts = (
4257
Post.objects.not_hidden()
43-
.visible_to(request.identity)
44-
.filter(published__gte=since)
45-
.annotate(num_interactions=Count("interactions"))
46-
.order_by("-num_interactions", "-published")[offset : offset + limit]
58+
.filter(id__in=popular_post_ids[offset : offset + limit])
59+
.order_by("-published")
4760
)
4861
return schemas.Status.map_from_post(list(posts), request.identity)
4962

core/models/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ class SystemOptions(pydantic.BaseModel):
248248
cache_timeout_page_timeline: int = 60 * 3
249249
cache_timeout_page_post: int = 60 * 2
250250
cache_timeout_identity_feed: int = 60 * 5
251+
cache_timeout_trends: int = 60 * 60
251252

252253
restricted_usernames: str = "\n".join(
253254
[

0 commit comments

Comments
 (0)