Redis 可以写出到像 PostgreSQL 这样的数据库吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17033031/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:57:28  来源:igfitidea点击:

Can Redis write out to a database like PostgreSQL?

pythondjangopostgresqlredisredis-cache

提问by deadlock

I've been using PostgreSQL for the longest time. All of my data lives inside Postgres. I've recently looked into redis and it has a lot of powerful features that would otherwise take a couple of lines in Django (python) to do. Redis data is persistent as long the machine it's running on doesn't go down and you can configure it to write out the data it's storing to disk every 1000 keys or every 5 minutes or so depending on your choice.

我使用 PostgreSQL 的时间最长。我所有的数据都保存在 Postgres 中。我最近研究了 redis,它有很多强大的功能,否则在 Django (python) 中需要几行代码才能完成。只要运行它的机器不停机,Redis 数据就是持久的,您可以将其配置为每 1000 个键或每 5 分钟左右将其存储的数据写出到磁盘,具体取决于您的选择。

Redis would make a great cache and it would certainly replace a lot of functions I have written in python (up voting a user's post, viewing their friends list etc...). But my concern is, all of this data would some how need to be translated over to postgres. I don't trust storing this data in redis. I see redis as a temporary storage solution for quick retrieval of information. It's extremely fast and this far outweighs doing repetitive queries against postgres.

Redis 将成为一个很好的缓存,它肯定会取代我用 Python 编写的许多功能(对用户的帖子进行投票,查看他们的朋友列表等......)。但我担心的是,所有这些数据都需要转换为 postgres。我不相信将这些数据存储在 redis 中。我将 redis 视为一种用于快速检索信息的临时存储解决方案。它非常快,远远超过对 postgres 进行重复查询。

I'm assuming the only way I could technically write the redis data to the database is to save() whatever I get from the 'get' query from redis to the postgres database through Django.

我假设我可以在技术上将 redis 数据写入数据库的唯一方法是 save() 我通过 Django 从 redis 到 postgres 数据库的“get”查询中获得的任何内容。

That's the only solution I could think of. Do you know of any other solutions to this problem?

这是我能想到的唯一解决方案。你知道这个问题的任何其他解决方案吗?

回答by Craig Ringer

Redis is increasingly used as a caching layer, much like a more sophisticated memcached, and is very useful in this role. You usually use Redis as a write-throughcache for data you want to be durable, and write-backfor data you might want to accumulate then batch write (where you can afford to lose recent data).

Redis 越来越多地用作缓存层,很像更复杂的 memcached,并且在此角色中非常有用。您通常将 Redis 用作您希望持久化的数据的直写缓存,以及您可能希望积累然后批量写入的数据的回写(您可以承受丢失最近数据的后果)。

PostgreSQL's LISTENand NOTIFYsystem is very useful for doing selective cache invalidation, letting you purge records from Redis when they're updated in PostgreSQL.

PostgreSQLLISTENNOTIFY系统对于执行选择性缓存失效非常有用,让您在 PostgreSQL 中更新记录时从 Redis 中清除记录。

For combining it with PostgreSQL, you will find the Redis foreign data wrapper provider that Andrew Dunstain and Dave Page are working onvery interesting.

为了将其与 PostgreSQL 结合,您会发现Andrew Dunstain 和 Dave Page 正在研究的 Redis 外部数据包装器提供程序非常有趣。

I'm not aware of any tool that makes Redis into a transparent write-back cache for PostgreSQL. Their data models are probably too different for this to work well. Usually you write changes to PostgreSQL and invalidate their Redis cache entries using listen/notify to a cache manager worker, or you queue changes in Redis then have your app read them out and write them into Pg in chunks.

我不知道有什么工具可以使 Redis 成为 PostgreSQL 的透明回写缓存。他们的数据模型可能太不同,无法正常工作。通常,您将更改写入 PostgreSQL 并使用侦听/通知缓存管理器工作器使它们的 Redis 缓存条目无效,或者您在 Redis 中排队更改,然后让您的应用程序读取它们并将它们分块写入 Pg。

回答by vectorselector

Redis is persistent if configured to be so, both through snapshots and a kind of WAL called AOF. Loads of people use it as a primary datastore. https://redis.io/topics/persistence

Redis 通过快照和一种称为 AOF 的 WAL 配置为持久性。很多人将其用作主要数据存储。 https://redis.io/topics/persistence

If one is referring to the greater world of Redis compatible (resp protocol) datastores, many are not limited to in-memory storage: https://keydb.dev/http://ssdb.io/and many more...

如果人们指的是 Redis 兼容(resp 协议)数据存储的更大世界,那么许多不限于内存存储:https: //keydb.dev/ http://ssdb.io/等等...