如何在Django中使用redis?

问题:如何在Django中使用redis?

我听说过redis-cache,但是它是如何工作的呢?通过以某种方式缓存rdbms查询,它是否被用作Django和我的rdbms之间的一层?

还是应该将其直接用作数据库?我对此表示怀疑,因为该github页面不包含任何登录详细信息,所以没有设置..只是告诉您设置一些config属性。

I’ve heard of redis-cache but how exactly does it work? Is it used as a layer between django and my rdbms, by caching the rdbms queries somehow?

Or is it supposed to be used directly as the database? Which I doubt, since that github page doesn’t cover any login details, no setup.. just tells you to set some config property.


回答 0

这个Redis的Python模块在自述文件中有一个清晰的用法示例:http : //github.com/andymccurdy/redis-py

Redis被设计为RAM缓存。它支持键的基本GET和SET以及字典等集合的存储。您可以通过将RDBMS查询的输出存储在Redis中来缓存它们。目标是加快Django网站的速度。在需要速度之前,不要开始使用Redis或任何其他缓存-不要过早地进行优化。

This Python module for Redis has a clear usage example in the readme: http://github.com/andymccurdy/redis-py

Redis is designed to be a RAM cache. It supports basic GET and SET of keys plus the storing of collections such as dictionaries. You can cache RDBMS queries by storing their output in Redis. The goal would be to speed up your Django site. Don’t start using Redis or any other cache until you need the speed – don’t prematurely optimize.


回答 1

仅仅因为Redis将事物存储在内存中并不意味着它就是缓存。我见过人们将它用作数据的持久存储。

可以将其用作高速缓存暗示它可以用作高性能存储。如果您的Redis系统出现故障,则可能会丢失尚未再次写回到磁盘的数据。有一些方法可以减轻这种危险,例如,热备份副本。如果您的数据是“关键任务”,例如您经营一家银行或商店,那么Redis可能不是您的最佳选择。但是,如果您使用持久的实时数据或一些社交互动的东西来编写流量高的游戏,并且将数据丢失的可能性控制在可以接受的范围内,那么Redis可能值得一看。

无论如何,重点仍然存在,是的,Redis可以用作数据库。

Just because Redis stores things in-memory does not mean that it is meant to be a cache. I have seen people using it as a persistent store for data.

That it can be used as a cache is a hint that it is useful as a high-performance storage. If your Redis system goes down though you might loose data that was not been written back onto the disk again. There are some ways to mitigate such dangers, e.g. a hot-standby replica. If your data is ‘mission-critical’, like if you run a bank or a shop, Redis might not be the best pick for you. But if you write a high-traffic game with persistent live data or some social-interaction stuff and manage the probability of data-loss to be quite acceptable, then Redis might be worth a look.

Anyway, the point remains, yes, Redis can be used as a database.


回答 2

Redis基本上是一个“记忆中”的KV存储,带有大量的铃声和口哨声。它非常灵活。您可以将其用作临时存储(例如缓存)或永久存储(例如数据库)(带有其他答案中提到的警告)。

当与Django结合使用时,Redis的最佳/最常见用例可能是缓存“响应”和会话。

这里有一个后端https://github.com/sebleier/django-redis-cache/和Django文档中的出色文档,这里是https://docs.djangoproject.com/en/1.3/topics/cache/

我最近开始使用https://github.com/erussell/django-redis-status监视我的缓存-很有魅力。(在redis上配置maxmemory或结果不是很有用)。

Redis is basically an ‘in memory’ KV store with loads of bells and whistles. It is extremely flexible. You can use it as a temporary store, like a cache, or a permanent store, like a database (with caveats as mentioned in other answers).

When combined with Django the best/most common use case for Redis is probably to cache ‘responses’ and sessions.

There’s a backend here https://github.com/sebleier/django-redis-cache/ and excellent documentation in the Django docs here: https://docs.djangoproject.com/en/1.3/topics/cache/ .

I’ve recently started using https://github.com/erussell/django-redis-status to monitor my cache – works a charm. (Configure maxmemory on redis or the results aren’t so very useful).


回答 3

您还可以将Redis用作Django应用程序中分布式任务的队列。您可以将其用作CeleryPython RQ的消息代理。

You can also use Redis as a queue for distributed tasks in your Django app. You can use it as a message broker for Celery or Python RQ.


回答 4

Redis作为主数据库

是的,您可以将Redis键值存储用作主数据库。Redis不仅存储键值对,还支持不同的数据结构,例如

  1. 清单
  2. 排序集
  3. 散列
  4. 位图
  5. 超级日志

Redis数据类型官方文档

Redis在内存键值存储中,因此如果Redis服务器发生故障,您必须知道它,您的数据将丢失。

Redis还可以保留数据检查官方文档。

Redis Persistence官方文档


Redis作为缓存

是的,Redis驻留在Django和RDBMS之间。

这个怎么运作

given a URL, try finding that page in the cache if the page is in the cache: return the cached page else: generate the page save the generated page in the cache (for next time) return the generated page

Django的缓存框架Official Doc


如何在Django中使用Redis

我们可以将redis python客户端redis-py用于Django应用程序。

Redis Python客户端redis-py Github

我们可以将Django-redis用于Django缓存后端。

Django-redis基于redis-py构建,并添加了与Django应用程序相关的额外功能。

Django-redis doc Github

还存在其他库。


Redis用例和数据类型

一些用例

  • 会话缓存
  • 实时分析
  • Web缓存
  • 排行榜

按核心数据结构类型划分的顶级Redis用例


使用Redis的大型科技公司

 Twitter GitHub Weibo Pinterest Snapchat Craigslist Digg StackOverflow Flickr 

Redis as a Primary database

Yes you can use Redis key-value store as a primary database. Redis not only store key-value pairs it also support different data structures like

  1. List
  2. Set
  3. Sorted set
  4. Hashes
  5. Bitmaps
  6. Hyperloglogs

Redis Data types Official doc

Redis is in memory key-value store so you must aware of it if Redis server failure occurred your data will be lost.

Redis can also persist data check official doc.

Redis Persistence Official doc


Redis as a Cache

Yes Redis reside between in Django and RDBMS.

How it works

given a URL, try finding that page in the cache if the page is in the cache: return the cached page else: generate the page save the generated page in the cache (for next time) return the generated page

Django’s cache framework Official Doc


How can use Redis with Django

We can use redis python client redis-py for Django application.

Redis python client redis-py Github

We can use Django-redis for django cache backend.

Django-redis build on redis-py and added extra features related to django application.

Django-redis doc Github

Other libraries also exists.


Redis use cases and data types

Some use cases

  • Session cache
  • Real time analytics
  • Web caching
  • Leaderboards

Top Redis Use Cases by Core Data structure types


Big Tech companies using Redis

 Twitter GitHub Weibo Pinterest Snapchat Craigslist Digg StackOverflow Flickr