In my django project, I got a task running every 5 minutes (with Celery and Redis as a broker):
from django.core.cache import cache
@shared_task()
@celery.task(base=QueueOnce)
def cache_date():
cache.set('date', datetime.now)
print('Cached date : ', cache.get('date'))
And it's running fine, printing the new cached date everytime it runs
But then, somewhere in one of my views I try to do this :
from django.core.cache import cache
def get_cached_date():
print('Cached date :', cache.get('date')
And then it prints "Cached date : None"
Here's my cache settings :
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': '/tmp/cache',
}
}
I don't get it, why is the value available in one place and not in the other while I'm using a singe location file ? Am I trying to do something wrong?
UPDATE
@Satevg I use docker-compose, here is my file :
services:
redis:
image: redis
worker:
image: project
depends_on:
- redis
- webserver
command: project backend worker
volumes:
- cache:/tmp/cache
webserver:
image: project
depends_on:
- redis
volumes:
- cache:/tmp/cache
volumes:
cache:
I tried to share the volumes like this, but when my task tries to write to the cache I get :
OSError: [Errno 13] Permission denied: '/tmp/cache/tmpAg_TAc'
When I look at the filesystems in both containers I can see the folder /tmp/cache, the web app can even write on it and when I look on the worker's container /tmp/cache folder I can see the updated cache
UPDATE2:
The webapp can write to the cache.
cache.set('test', 'Test')
On the worker's container, I can see the cache file on the /tmp/cache folder When the task tries to read from cache :
print(cache.get('test'))
It says :
None
When the task tries to write from cache it still gets Errno13
As we figured out in comments, celery and app are working in different docker containers.
django.core.cache.backends.filebased.FileBasedCache
serializes and stores each cache value as a separate file. But these files are in different file systems.
The solution is to use docker volumes in order to share /tmp/cache
folder between these two containers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.