??Redis???????????
???????????? ???????[ 2015/11/6 11:25:09 ] ????????Redis ?????
????????????????redis?????????????????????????????????DEV??DBA??????????review???sql????????memcached???????????DBA?????redis??DBA?????????????????????????????
??????????Щ???????kv??????????????????????????????????????????????????????????????
????????????
????????????????????????? ???????????????????
??????????????????
????mysql> select * from login;
????+---------+----------------+-------------+---------------------+
????| user_id | name | login_times | last_login_time |
????+---------+----------------+-------------+---------------------+
????| 1 | ken thompson | 5 | 2011-01-01 00:00:00 |
????| 2 | dennis ritchie | 1 | 2011-02-01 00:00:00 |
????| 3 | Joe Armstrong | 2 | 2011-03-01 00:00:00 |
????+---------+----------------+-------------+---------------------+
????user_id?????????name??????????login_times?????????????????????????????login_times??????????last_login_time???????????
????redis?????
?????????????????KV??????????????£?
????key ?????????????????
????value ???
??????????e?????????????????????????????php-admin for redis???????????e????????user:1 user:2??key??????顣????????????????????kv??????????£?
????Set login:1:login_times 5
????Set login:2:login_times 1
????Set login:3:login_times 2
????Set login:1:last_login_time 2011-1-1
????Set login:2:last_login_time 2011-2-1
????Set login:3:last_login_time 2011-3-1
????set login:1:name ”ken thompson“
????set login:2:name “dennis ritchie”
????set login:3:name ”Joe Armstrong“
???????????????????????£????get??set??????????????????????????????????????
???????????????????????id???????????????????????????????????name??id?????????????????????????????????
????set "login:ken thompson:id" 1
????set "login:dennis ritchie:id" 2
????set "login: Joe Armstrong:id" 3
????????????????????????????????(python??)??r??redis????name?????????????????
????#????????id
????uid = r.get("login:%s:id" % name)
????#???????????????
????ret = r.incr("login:%s:login_times" % uid)
????#????????????????
????ret = r.set("login:%s:last_login_time" % uid?? datetime.datetime.now())
???????????????????id????????????????????????????????????????kv??????????????????btree pk????????hash??Ч??????á?
????????????????????????????N?????????????????????????????????sql????
????select * from login order by last_login_time desc limit N
????DBA??????????????????????????????last_login_time?????????????м????????leafblock ???????????N???????????N?Σ?Ч????á?
?????????????????????????????????????????????????????????δ????DEV???
????select * from login order by login_times desc limit N
????DBA??????????login_time??????????????????о????е?????????????????????????????
???????????????????洢??????????????????????????????淽??????????????е???????????????ζ???????????????????sql?????·???????????????е????????·????????????ζ???????????????????????????????????????????
????????????????????????????м????????kv?????
????redis????????????????????? ???????μ?N?????????????????????????????????????????????????????????δ???????????????????????????????????????????????????N??????????
????#???????????????????
????ret = r.lpush("login:last_login_times"?? uid)
????#???????????Nλ
????ret = redis.ltrim("login:last_login_times"?? 0?? N-1)
???????????????μ?????id?????μ??????
????last_login_list = r.lrange("login:last_login_times"?? 0?? N-1)
???????????????????????????????????????????sorted set????????????????????????????洢?????sorted set??
????zadd login:login_times 5 1
????zadd login:login_times 1 2
????zadd login:login_times 2 3
?????????????????????????????????sorted set?????????
????#??????????????????1
????ret = r.zincrby("login:login_times"?? 1?? uid)
?????????λ??????????????????????????????????N?????????
????ret = r.zrevrange("login:login_times"?? 0?? N-1)
?????????????DEV??????2?д?????DBA??????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11