PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 12, 2022

[FIXED] How to clear cache for Django with Docker?

 November 12, 2022     django, docker, docker-compose, memcached, python     No comments   

Issue

I have a Django project that I am using with memcached and Docker. When I use sudo docker-compose up in development I'd like the entire cache to be cleared. Rather than disabling caching wholesale while in development mode, is there a way to run cache.clear() as noted in this question on each re-run of sudo docker-compose up?

I am not sure whether this should go in:

  1. docker-entrypoint.sh
  2. Dockerfile
  3. docker-compose.yml
  4. Somewhere else?

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

Solution

As per this answer you can add a service that's executed before the memcached service that clears out the cache. As it looks like you're using Linux Alpine, you can add this service to docker-compose.yml:

clearcache:
    command: [sh, -c, "python manage.py clear_cache"] 

and then add to memcached:

memcached:
    ...
    depends_on:
     - clearcache

There's also an example in there that does it in the same command and not relying on a separate service (though personally I don't like that).

For the cache clearing command, this answer has some useful discussion and posts.

clear_cache.py:

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')


Answered By - Nobilis
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing