关于redis的问题,我的理解正确否?
伊谢尔伦
伊谢尔伦 2017-04-21 10:57:54
0
4
603

从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

假设需求:获取我的好友的所有除我以外的所有好友。

暂时就这么多……不知道表述是否清楚了。

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(4)
左手右手慢动作

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 hashObject storage

for multiple fields
HMSET user:1234 name "hello" password "123456" timestamp "12321321"

Save the primary key in one SET或者SORTED SET

SADD user_list 1234

It is worth noting that redis SORT命令可以代替SQL解决很多查询,比如像上面这种哈希存储方式,如果我们要按timestamp排序并在一定范围内列出usercomes

SORT user_list BY user:*->timestamp GET # LIMIT 0 10 DESC

It is equivalent to SQL statement

SELECT * FROM user ORDER BY timestamp DESC LIMIT 0 10
刘奇

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}

PHPzhong

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template