开发者

How to integrate Redis with SQLAlchemy

I'm learning to use SQLAlchemy connected to a SQL database for 12 standard relational tables (e.g. SQLite or PostgreSQL). But then I'd like t开发者_运维知识库o use Redis with Python for a couple of tables, particularly for Redis's fast set manipulation. I realise that Redis is NoSQL, but can I integrate this with SQLAlchemy for the benefit of the session and thread handling that SQLAlchemy has?

Is there a Redis SA dialect? I couldn't find it, which probably means that I'm missing some basic point. Is there a better architecture I should look at to use two different types of database?


While it is possible to set up an ORM that puts data in redis, it isn't a particularly good idea. ORMs are designed to expose standard SQL features. Many things that are standard in SQL such as querying on arbitrary columns are not available in redis unless you do a lot of extra work. At the same time redis has features such as set manipulation that do not exist in standard SQL so will not be used by the ORM.

Your best option is probably to write your code to interact directly with redis rather than trying to use an inappropriate abstraction - Generally you will find that the code to get data out of redis is quite a bit simpler than the SQL code that justifies using an ORM.


Redis is very good at what it does, storing key values and making simple atomic operations, but if you want to use it as a relational database you're really gonna SUFFER!, as I had... and here is my story...

I've done something like that, making several objects to abstracting all the redis internals exposing primitives queries (I called filters in my code), get, set, updates, and a lot more methods that you can expect from a ORM and in fact if you are dealing only with localhost, you're not going to perceive any slowness in your application, you can use redis as a relational database but if in any time you try to move your database into another host, that will represent a lot of problems in terms of network transmission, I end up with a bunch of re-hacked classes using redis and his pipes, which it make my program like 900% faster, making it usable in the local network, anyway I'm starting to move my database library to postgres.

The lesson of this history is to never try to make a relational database with the key value model, works great at basic operations, but the price of not having the possibility to make relations in your server comes with a high cost.

Returning to your question, I don't know any project to make an adapter to sqlalchemy for redis, and I think that nobody are going to be really interested in something like that, because of the nature of each project.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜