从SQLite移植到Redis

c0d*_*0da 1 database key-value redis

我使用SQLite作为应用程序,其中我曾经存储8-10列.我曾经根据任意数量的这些属性的组合来检索数据.现在我想移植到Redis.所以我正在为它构建一个测试应用程序.

但我无法想到如何设计我的redis系统,以便能够根据任何这些属性检索数据.你们有没有任何建议/经验?

Did*_*zia 5

我认为最好的建议是避免在将某些内容从RDBMS移植到Redis时坚持使用关系模型.除模型外,重要的区别在于关注数据访问路径以及数据结构.

Redis不包含查询语言(而是命令la memcached),因此不能回复任意查询.如果数据的访问路径不是数据结构的一部分,则无法有效地检索数据.

在支持任意查询时,Redis不是最好的NoSQL存储.例如,像MongoDB这样的东西会更好.

现在,如果你真的想用Redis实现你的东西,你可以尝试使用类似于标记引擎的策略.您的记录可以存储在哈希对象中.对于需要支持的任意查询的每个列部分,可以使用集合构建反向索引.

例如:

# Set up the records: one hash object per record
hmset user:1 name Bilbo type Hobbit job None
hmset user:2 name Frodo type Hobbit job None
hmset user:3 name Gandalf type Maiar job Wizard
hmset user:4 name Aragorn type Human job King
hmset user:5 name Boromir type Human job Warrior

# Set up the indexes: one set per value per field
sadd name:Bilbo 1
sadd name:Frodo 2
sadd name:Gandalf 3
sadd name:Aragorn 4
sadd name:Boromir 5
sadd type:Hobbit 1 2
sadd type:Maiar 3
sadd type:Human 4 5
sadd job:None 1 2
sadd job:Wizard 3
sadd job:King 4
sadd job:Warrior 5

# Perform a query: we want the humans who happen to be a king
# We just have to calculate the intersection of the corresponding sets
sinterstore tmp type:Human job:King
sort tmp by nosort get user:*->name get user:*->job get user:*->type
1) "Aragorn"
2) "King"
3) "Human"
Run Code Online (Sandbox Code Playgroud)

通过组合联合,交集,差异,可以实现更复杂的查询.对于非离散值或基于范围的查询,必须使用有序集(zset)(并且可以与常规集合组合).

如果值足够大,则此方法通常非常快.请注意,你没有RDBMS的灵活性(没有正则表达式,没有前缀搜索,范围查询很难处理等等)