从RDBMS彻底的过渡到NoSQL,关键的是不是NoSQL中的Key设计咧? 比如原先的登录注册,RDBMS中可能是
user_id, username, password
1 , user , xxxxxxxx
2 , user2 , xxxxxxxx
过渡到Redis,可以这样
users:user password
users:user2 password
检测用户是否已经注册
RDBMS:
select count(*) from table where username = 'user2'
Redis:
Get users:user2
但是抛弃“关系”又怎么实现“关系”呢? 比如用户还有profile。 redis可以以users:username为key。然后存个对象进去。 但是怎么进行搜索呢?比如,我要获取所有注册时间在指定时间的一批用户。当然,可以按照users:username:profile_name挨个存,比如:
users:user2:profile_photo /path/hash.jpg
users:user2:profile_regdate timestamp
users:user3:profile_regdate timestamp
但是,会不会出现,删除某用户的时候,由于特殊原因导致删不干净的问题……比如user3的所有数据正在删除,然后异常了,导致profile_regdate没有删除。而且以后也检测不到了。
再者,好友关系。redis可以这么存:
relation:uid:fuid timestamp
relation:fuid:uid timestamp
假设需求:获取我的好友的所有除我以外的所有好友。
暂时就这么多……不知道表述是否清楚了。
First of all, if you want to use multiple fields, don't store the fields individually, or serialize an object and then store it. This is no different from memcache.
Secondly, you need to understand the fact that we are not using redis to completely replace the relational database. As you said, searching for users by user name does not need to be put into redis at all. Don’t use redis just to use redis.
With redis you can do caching, so you only need to use the primary key as the key, and you can also use its indexing feature to make lists
You can use
for multiple fieldshash
Object storageSave the primary key in one
SET
或者SORTED SET
It is worth noting that redis
SORT
命令可以代替SQL
解决很多查询,比如像上面这种哈希存储方式,如果我们要按timestamp
排序并在一定范围内列出user
comesIt is equivalent to SQL statement
The first question can only be that you are using redis as memcache to understand the hash structure. User information should use hash structure, such as user:1->{username:sss,age:12}
redis supports multi
Use more hash methods to store data in redis. In this way, some associations that need to be used can be saved as key values in the hash. Secondly, some relationships and query method habits originally designed in relational databases need to be adjusted when using redis. We cannot completely copy the previous ideas.