Python 如何在 Django 中使用 redis?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3801379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use redis with Django?
提问by meder omuraliev
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?
我听说过 redis-cache 但它究竟是如何工作的?通过以某种方式缓存 rdbms 查询,它是否用作 django 和我的 rdbms 之间的层?
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.
还是应该直接用作数据库?我对此表示怀疑,因为那个 github 页面不包含任何登录详细信息,所以没有设置.. 只是告诉您设置一些配置属性。
采纳答案by Spike Gronim
This Python module for Redis has a clear usage example in the readme: http://github.com/andymccurdy/redis-py
这个用于Redis的Python模块在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.
Redis 被设计为 RAM 缓存。它支持键的基本 GET 和 SET 以及字典等集合的存储。您可以通过将输出存储在 Redis 中来缓存 RDBMS 查询。目标是加速您的 Django 站点。在需要速度之前不要开始使用 Redis 或任何其他缓存 - 不要过早地优化。
回答by ostergaard
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).
Redis 基本上是一个带有大量花里胡哨的“内存中”KV 存储。它非常灵活。您可以将其用作临时存储(如缓存)或永久存储(如数据库)(其他答案中提到的注意事项)。
When combined with Django the best/most common use case for Redis is probably to cache 'responses' and sessions.
与 Django 结合使用时,Redis 的最佳/最常见用例可能是缓存“响应”和会话。
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/.
这里有一个后端https://github.com/sebleier/django-redis-cache/和 Django 文档中的优秀文档:https: //docs.djangoproject.com/en/1.3/topics/cache/。
I've recently started using https://github.com/erussell/django-redis-statusto monitor my cache - works a charm. (Configure maxmemory on redis or the results aren't so very useful).
我最近开始使用https://github.com/erussell/django-redis-status来监控我的缓存 - 很有魅力。(在 redis 上配置 maxmemory 否则结果不是很有用)。
回答by Stefan Schubert-Peters
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.
仅仅因为 Redis 将东西存储在内存中并不意味着它就是一个缓存。我见过有人使用它作为数据的持久存储。
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.
它可以用作缓存暗示它可用作高性能存储。如果您的 Redis 系统出现故障,您可能会丢失未再次写回磁盘的数据。有一些方法可以减轻这种危险,例如热备份副本。如果您的数据是“关键任务”,例如您经营银行或商店,Redis 可能不是您的最佳选择。但是,如果您编写具有持久实时数据或一些社交互动内容的高流量游戏,并且将数据丢失的可能性控制在完全可以接受的范围内,那么 Redis 可能值得一看。
Anyway, the point remains, yes, Redis can be used as a database.
无论如何,重点仍然是,是的,Redis 可以用作数据库。
回答by Dmitrii Mikhailov
回答by Muhammad Faizan Fareed
Redis as a Primary database
Redis 作为主数据库
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
是的,您可以使用 Redis 键值存储作为主数据库。Redis 不仅存储键值对,它还支持不同的数据结构,如
- List
- Set
- Sorted set
- Hashes
- Bitmaps
- Hyperloglogs
- 列表
- 放
- 排序集
- 哈希值
- 位图
- 超级日志
Redis is in memory key-value store so you must aware of it if Redis server failure occurred your data will be lost.
Redis 是在内存中的键值存储,因此您必须意识到如果 Redis 服务器发生故障,您的数据将丢失。
Redis can also persist data check official doc.
Redis 也可以持久化数据检查官方文档。
Redis Persistence Official doc
Redis as a Cache
Redis 作为缓存
Yes Redis reside between in Django and RDBMS.
是的,Redis 位于 Django 和 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
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
如何在 Django 中使用 Redis
We can use redis python client redis-pyfor Django application.
我们可以将 redis python 客户端redis-py用于 Django 应用程序。
Redis python client redis-py Github
Redis python 客户端 redis-py Github
We can use Django-redisfor django cache backend.
我们可以使用Django-redis作为 django 缓存后端。
Django-redisbuild on redis-pyand added extra features related to django application.
Django-redis基于redis-py构建,并添加了与 django 应用程序相关的额外功能。
Other libraries also exists.
其他库也存在。
Redis use cases and data types
Redis 用例和数据类型
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
使用 Redis 的大型科技公司
Twitter GitHub Weibo Pinterest Snapchat Craigslist Digg StackOverflow Flickr

