首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不使用django的Redis缓存

不使用django的Redis缓存
EN

Stack Overflow用户
提问于 2015-03-25 16:22:46
回答 2查看 5.2K关注 0票数 1

我在一个ubuntu EC2节点上有一个django项目,我想要设置一个缓存,我正在跟踪http://michal.karzynski.pl/blog/2013/07/14/using-redis-as-django-session-store-and-cache-backend/,以便为此使用redis。在这篇文章中,作者提到了https://docs.djangoproject.com/en/1.7/topics/cache/,基于此,我可以:

代码语言:javascript
复制
(env1)ubuntu@ip-172-31-22-65:~/projects/tp$ python manage.py shell
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import django
>>> import redis
>>> from django.core.cache import cache
>>> cache.set('my_key','hi world')
>>> cache.get('my_key')
'hi world'

我目前的django视图包含;

代码语言:javascript
复制
def index(token):

    html = calculator(token)
    print('here1')

    import redis
    from django.core.cache import cache
    cache.set('my_key', 'hello, world!', 60*60*12)
    print('here2')

    return html

但是,当我触发索引函数时,没有任何东西保存到缓存中。我从命令行查过了。

我怎样才能让缓存正常工作?

编辑:

代码语言:javascript
复制
>>> print(settings.CACHES)
{'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-25 17:50:10

关键是缓存配置,它应该是:

代码语言:javascript
复制
CACHES = {
    'default': {
        'BACKEND': 'redis_cache.RedisCache',
        'LOCATION': '/var/run/redis/redis.sock',
    },
}

(cf http://michal.karzynski.pl/blog/2013/07/14/using-redis-as-django-session-store-and-cache-backend/)

票数 5
EN

Stack Overflow用户

发布于 2021-06-26 14:19:30

Rapha l Braud答复的最新版本:

代码语言:javascript
复制
 'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': '/var/run/redis/redis.sock',
    },
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29261104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档