Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Добавил кэширование #320

Open
wants to merge 16 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/django-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ on:
jobs:
django-test:
runs-on: ubuntu-latest

services:
redis:
image: redis
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 6379:6379

steps:
- uses: actions/checkout@v2

Expand Down
28 changes: 27 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions procollab/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"channels",
"taggit",
"django_prometheus",
"cacheops",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -186,20 +187,30 @@
}
}

REDIS_CONN_URL = "redis://127.0.0.1:6379"

CACHES = {
"default": {
"BACKEND": "django_prometheus.cache.backends.filebased.FileBasedCache",
"LOCATION": "/var/tmp/django_cache",
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": REDIS_CONN_URL,
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
},
}
Comment on lines +194 to 199
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если использовать FileBasedCache нельзя тянуть все ключи содержащие project_list, инвалидация становится невозможной.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

очень не хочется на локалке каждый раз поднимать редис чтобы кеш работал, с этим разве нельзя сделать ничего?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно накостылить свой client_class для FileBasedCache, который будет искать по совпадениям в ключах, но не вижу проблем запускать редис в локале, wsl позволяет это одной строчкой сделать

}

CHANNEL_LAYERS = {"default": {"BACKEND": "channels.layers.InMemoryChannelLayer"}}
else:
REDIS_CONN_URL = "redis://redis:6379"

# fixme
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": "redis://redis:6379",
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": REDIS_CONN_URL,
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
},
}
}

Expand Down Expand Up @@ -355,3 +366,9 @@
)

DATA_UPLOAD_MAX_NUMBER_FIELDS = None # for mailing

CACHEOPS_REDIS = REDIS_CONN_URL

CACHEOPS = {
"users.CustomUser": {"ops": "all", "timeout": 60 * 15},
}
5 changes: 5 additions & 0 deletions projects/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from chats.models import ProjectChat
from projects.models import Collaborator, Project
from django.core.cache import cache


@receiver(post_save, sender=Project)
Expand All @@ -20,3 +21,7 @@ def create_project(sender, instance, created, **kwargs):
Collaborator.objects.create(
user=instance.leader, project=instance, role="Основатель"
)

# invalidating cache from ProjectList view
keys = cache.keys("*project_list*")
cache.delete_many(keys)
6 changes: 6 additions & 0 deletions projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from django.views.decorators.cache import cache_page
from django.utils.decorators import method_decorator

from core.permissions import IsStaffOrReadOnly
from core.serializers import SetLikedSerializer
Expand Down Expand Up @@ -113,6 +115,10 @@ def post(self, request, *args, **kwargs):
# set leader to current user
return self.create(request, *args, **kwargs)

@method_decorator(cache_page(60 * 30, cache="default", key_prefix="project_list"))
def get(self, *args, **kwargs):
return super(ProjectList, self).get(*args, **kwargs)


class ProjectDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Project.objects.get_projects_for_detail_view()
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ django-stubs = {extras = ["compatible-mypy"], version = "^4.2.6"}
djangorestframework-stubs = {extras = ["compatible-mypy"], version = "^3.14.4"}
flake8-print = "^5.0.0"
flake8-variables-names = "^0.0.6"
django-cacheops = "^7.0.2"


[build-system]
Expand Down
1 change: 1 addition & 0 deletions vacancy/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib.auth import get_user_model
from rest_framework import serializers

from core.fields import CustomListField
from projects.models import Project
from users.serializers import UserDetailSerializer, CustomListField
from vacancy.models import Vacancy, VacancyResponse
Expand Down
Loading