简体   繁体   English

如何使用Django在任务和视图之间共享缓存?

[英]How to share cache between a task and a view with Django?

In my django project, I got a task running every 5 minutes (with Celery and Redis as a broker): 在我的django项目中,我每5分钟运行一次任务(使用Celery和Redis作为代理):

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 : @Satevg我使用docker-compose,这是我的文件:

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 当我查看两个容器中的文件系统时,我可以看到文件夹/ tmp / cache,Web应用程序甚至可以在其上写东西;当我查看工作人员的容器/ tmp / cache文件夹时,我可以看到更新后的缓存

UPDATE2: UPDATE2:

The webapp can write to the cache. Webapp可以写入缓存。

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 : 在工作人员的容器上,当任务尝试从缓存读取时,我可以在/ tmp / cache文件夹中看到缓存文件:

print(cache.get('test'))

It says : 它说 :

None

When the task tries to write from cache it still gets Errno13 当任务尝试从缓存写入时,它仍会显示Errno13

As we figured out in comments, celery and app are working in different docker containers. 正如我们在评论中指出的那样,celery和app在不同的docker容器中工作。

django.core.cache.backends.filebased.FileBasedCache serializes and stores each cache value as a separate file. django.core.cache.backends.filebased.FileBasedCache序列化每个缓存值并将其存储为单独的文件。 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. 解决方案是使用docker卷 ,以便在这两个容器之间共享/tmp/cache文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM