我有一个Django项目,我正在使用memcached和Docker。当我在开发中使用sudo docker-compose up
时,我希望清除整个缓存。而不是在开发模式下禁用缓存批发,有没有办法运行cache.clear()
,如this question每次重新运行sudo docker-compose up
时所述?
我不确定这是否应该进入:
docker-entrypoint.sh
Dockerfile
docker-compose.yml
version: "3"
services:
redis:
image: "redis:alpine"
command: "redis-server --requirepass ${REDISPASS} --bind 0.0.0.0"
ports:
- '6379:6379'
memcached:
image: "memcached:latest"
ports:
- '11211:11211'
nginx:
image: nginx:latest
volumes:
- ./configuration/nginx/conf.d/:/etc/nginx/conf.d
- ./configuration/nginx/realtime:/etc/nginx/realtime
- ./static_cdn/static_root/static:/static
ports:
- 80:80
- 443:443
depends_on:
- app_main
- app_async_app1
- app_async_app2
app_main:
command: "djangoAppName.settings.prod ${SECRET_KEY} 1 ${DB_PASS} ${REDISPASS}"
image: "django_image_name"
ports:
- 0003:0003
volumes:
- ./static_cdn/static_root/:/static_cdn/
depends_on:
- redis
- memcached
app_async_app2:
command: "djangoAppName.settings.prod ${SECRET_KEY} 2 ${DB_PASS} ${REDISPASS}"
image: "django_image_name"
ports:
- 0002:0002
depends_on:
- redis
- memcached
- app_main
app_async_app1:
command: "djangoAppName.settings.prod ${SECRET_KEY} 3 ${DB_PASS} ${REDISPASS}"
image: "django_image_name"
depends_on:
- redis
- memcached
- app_main
ports:
- 0001:0001
react:
command: "npm run"
image: "django_image_name"
volumes:
- ./static_cdn/static_root/:/static_cdn/
depends_on:
- memcached
- app_main
根据this answer,您可以添加在清除缓存的memcached服务之前执行的服务。由于您看起来正在使用Linux Alpine,因此您可以将此服务添加到docker-compose.yml
:
clearcache:
command: [sh, -c, "python manage.py clear_cache"]
然后添加到memcached
:
memcached:
...
depends_on:
- clearcache
还有一个例子就是在同一个命令中执行它而不依赖于单独的服务(尽管我个人并不喜欢)。
对于缓存清除命令,this answer有一些有用的讨论和帖子。
clear_擦车.朋友:
from django.core.management.base import BaseCommand
from django.core.cache import cache
class Command(BaseCommand):
def handle(self, *args, **kwargs):
cache.clear()
self.stdout.write('Cleared cache\n')