redis 常用操作

 

redis 取值decode
version = rds_extra.get("ddz_42112048_version").decode()


删除所有key
删除当前数据库中的所有Key
flushdb

//删除所有数据库中的key
flushall

 



模糊删除
>>> import redis
>>> r = redis.StrictRedis(host='localhost', port=6379, db=0)
>>> r.delete(*r.keys('*test*'))
rds_mongo.delete(*rds_mongo.keys('*UserDayStat{}*'.format(device_id)))


1.链接redis

 

2.keys * 获取所有key

127.0.0.1:6379[5]> keys *
  1) "UserDayStat|4793799761@2019-07-08"
  2) "UserWeekStat|4865137051@2019-07-07"
  3) "UserGameStat|4795903911@173991"

 

3.keys *进行模糊匹配*

127.0.0.1:6379[5]> keys *2019-07-07*
1) "UserWeekStat|4865137051@2019-07-07"
2) "UserWeekStat|4793799761@2019-07-07"
3) "UserWeekStat|4795903911@2019-07-07"

 

4. incr "a_test"  rds_extra.incr(key)  redis计数器 如果不存在key为1 存在+1  计数器

127.0.0.1:6379[5]> INCR "a_test"
(integer) 5
127.0.0.1:6379[5]> get  "a_test"
"5"
127.0.0.1:6379[5]> INCR "a_test"
(integer) 6
127.0.0.1:6379[5]> get  "a_test"
"6"

 

5.expire 设置过期时间 设置过期时间为10秒

127.0.0.1:6379[5]> EXPIRE a_test 10
(integer) 1
127.0.0.1:6379[5]> get a_test

 

6.hmset 设置key对应的dict 

127.0.0.1:6379[5]> HMSET test_dict t1 "h1" t2 "h3"
OK
127.0.0.1:6379[5]> hget test_dict t1
"h1"
127.0.0.1:6379[5]> hget test_dict t2
"h3"

 

7.hgetall 获取key对应的所有值为dict 遍历

127.0.0.1:6379[5]> HMSET test_dict t1 "h1" t2 "h3" t3 "h4"
OK
127.0.0.1:6379[5]> HGETALL test_dict
1) "t1"
2) "h1"
3) "t2"
4) "h3"
5) "t3"
6) "h4"

 

8 setex 命令为指定的 key 设置值及其过期时间。如果 key 已经存在,setex 命令将会替换旧的值。

 加锁 

setnx 设置锁 命令在设置成功时返回 1 , 设置失败时返回 0

127.0.0.1:6379[5]> SETEX test_dict 10 t1
OK
127.0.0.1:6379[5]> HGET test_dict t1
(nil)

redis> EXISTS job                # job 不存在
(integer) 0
redis> SETNX job "programmer" # job 设置成功 (integer) 1 redis> SETNX job "code-farmer" # 尝试覆盖 job ,失败 (integer) 0 redis> GET job # 没有被覆盖 "programmer"




 

9 del删除key

127.0.0.1:6379[5]> set cccc 10
OK
127.0.0.1:6379[5]> get cccc
"10"
127.0.0.1:6379[5]> del cccc
(integer) 1
127.0.0.1:6379[5]> get cccc
(nil)

 

10 lpush 命令将一个或多个值插入到列表头部 列表插入值 从左插入

127.0.0.1:6379[3]> LPUSH test_1_list [1,2,3,4,4,5,6,76,7]
(integer) 14
127.0.0.1:6379[3]> LTRIM test_1_list 0 9
OK
127.0.0.1:6379[3]> LRANGE test_1_list 0 9
 1) "[1,2,3,4,4,5,6,76,7]"
 2) "[1,2,3,4,4,5,6,76,7]"
 3) "[1,2,3,4,4,5,6,76,7]"
 4) "[1,2,3,4,4,5,6,76,7]"
 5) "[1,2,3,4,4,5,6,76,7]"
 6) "[1,2,3,4,4,5,6,76,7]"
 7) "[1,2,3,4,4,5,6,76,7]"
 8) "[1,2,3,4,4,5,6,76,7]"
 9) "[1,2,3,4,4,5,6,76,7]"
10) "[1,2,3,4,4,5,6,76,7]"

 

11 zadd 添加一个有序集合 key唯一不重复  zscore 返回 对应key 的成员,或 key 不存在,返回 nil

    zrevrangebyscore 有序集合递减排序    zrevrange 根据 value的值进行排名     zrevrank  返回对应key的排名

  zincrby 命令对有序集合中指定成员的分数加上增量 increment 负数为减

 

  

127.0.0.1:6379[5]> zadd test 2 apple2 3 apple3 4 apple4 
(integer) 1
127.0.0.1:6379[5]> ZRANGE test 0 10 WITHSCORES
1) "apple1"
2) "1"
3) "apple2"
4) "2"
5) "apple3"
6) "3"
7) "apple4"
8) "4"

# 获取值 zscore
127.0.0.1:6379[5]> ZSCORE test apple1
"3"

# 有序集合递减排序 ZREVRANGEBYSCORE
127.0.0.1:6379[5]> ZADD salary 5000 tom
(integer) 0
127.0.0.1:6379[5]> ZADD salary 7500 peter
(integer) 0
127.0.0.1:6379[5]> zadd salary 3500 joe
(integer) 0
127.0.0.1:6379[5]>  ZREVRANGEBYSCORE salary 10000 2000
1) "peter"
2) "tom"
3) "joe"

# zrevrange 根据value的值排名 0-5 前5名
127.0.0.1:6379[5]> zrevrange salary 0 5
1) "jie"
2) "jack"
3) "peter"
4) "tom"
5) "joe"
6) "yingjie"


# zrevrank 返回对应key排名
127.0.0.1:6379[5]> zrevrank salary tom
(integer) 3


# zincrby 对指定的key 相加 负数为相减

127.0.0.1:6379[5]> zincrby salary 20000 yingjie
"21200"
127.0.0.1:6379[5]> zrevrange salary 0 5
1) "yingjie"
2) "jie"
3) "jack"
4) "peter"
5) "tom"
6) "joe"

#获取某个人的排名
ZREVRANK T0_room 1250571556

# 获取集合人数
zcard T0_room

# redis 查询有序集合所有人成绩
ZRANGE T0|room 0 -1 WITHSCORES 

 

 

redis 设置字符串

rds_con_game.set(name=rds_key, value=1) # 设置值
rds_con_game.expire(name=rds_key, time=86400 - seconds) #设置过期时间
rds_con_game.get(name=rds_key) # 获取值

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2019-07-08 16:42  星河赵  阅读(310)  评论(0编辑  收藏  举报

导航