Skip to content

Commit

Permalink
added cache for goods list
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-lymar committed Jun 3, 2024
1 parent 70fb2d9 commit e7927e3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions goods/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core.cache import cache
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated

Expand All @@ -9,3 +10,12 @@ class GoodsViewSet(viewsets.ModelViewSet[Product]):
queryset = Product.objects.all()
serializer_class = GoodsSerializer
permission_classes = (IsAuthenticated,)

def get_queryset(self):
cached_goods = cache.get("goods_list")
if cached_goods:
return cached_goods
else:
goods = super().get_queryset()
cache.set("goods_list", goods, 60 * 15)
return goods
2 changes: 1 addition & 1 deletion melnichanka/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"LOCATION": f"redis://{REDIS_HOST}:{REDIS_PORT}/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
},
}
}

Expand Down

0 comments on commit e7927e3

Please sign in to comment.