Redis中五中数据类型的实例

  • 时间:
  • 浏览:0
  • 来源:跟我学网络

spring类型

1. 字符串类型:实际上可以是字符串(包括XML JSON),还有数字(整形 浮点数),二进制(图片 音频 视频),最大不能超过512MB

 2. 设值命令:

set name lgs ex 10  //10秒后过期  px 10000 毫秒过期

setnx name lgs  //不存在键name时才能设置,返回1设置成功;存在的话失败0

set age 29    //存在键age时直接覆盖之前的键值,返回1成功

场景:如果有多客户同时执行setnx,只有一个能设置成功,可做分布式锁

获值命令:get age //存在则返回value, 不存在返回nil

批量设值:mset country china city beijing

批量获取:mget country city address //返回china  beigjin, address为nil   

若没有mget命令,则要执行n次get命令,从而占用网络资源影响性能

使用mget=1次网络请求+redis内部n次查询,一次性返回所有查询结果

3. 计数:

incr age //必须为整数自加1,非整数返回错误,无age键从0自增返回1

decr age //整数age减1,非整数返回错误,无age键从0自减返回-1

incrby age 2 //整数age+2

decrby age 2//整数age -2

 incrbyfloat age 1.1 //整数age+1.1

4. append追加指令:set name hello; append name world //追加后成helloworld

5. 字符串长度:set hello “世界”;strlen hello//结果6,每个中文占3个字节

6. 截取字符串:set name helloworld ; getrange name 2 4//返回 llo

hashmap类型

哈希hash是一个string类型的field和value的映射表,hash适合用于存储对象。

1. 命令  hset key field value

   设值:hset user:1 name lgs         //成功返回1,失败返回0

   取值:hget user:1 name              //返回lgs

   删值:hdel user:1 age               //返回删除的个数

   计算键对应的字段个数:

 hset user:1 name lgs; hset user:1 age 27;

   hlen user:1               //返回2,user:1有两个属性值

   批量设值:hmset user:2 name ll age 28 sex boy //返回OK

   批量取值:hmget user:2 name age sex   //返回三行:ll 28 boy

   判断field是否存在:hexists user:2 name //若存在返回1,不存在返回0

   获取所有field: hkeys user:2            // 返回name age sex三个field

   获取user:2所有value:hvals user:2     // 返回ll 28 boy

   获取user:2所有field与value:hgetall user:2 //name age sex ll 28 boy值

   增加1:

    hincrby user:2 age 1      //age+1

    hincrbyfloat user:2 age 2   //浮点型加2

list类型 

1.先进后出lpush

127.0.0.1:6379> lpush name2 sunlin

(integer) 1

127.0.0.1:6379> lpush name2 dengqiang

(integer) 2

127.0.0.1:6379> lrange name2 0 -1

(1) "dengqiang"

(2) "sunlin"

2.根据索引替换值

127.0.0.1:6379> lset name2 0 aa

OK

127.0.0.1:6379> lrange name2 0 -1

(1) "aa"

(2) "sunlin"

3.先进先出rpush

127.0.0.1:6379> rpush name2 sunlin

(integer) 1

127.0.0.1:6379> rpush name2 dengqiang

(integer) 2

127.0.0.1:6379> lrange name2 0 -1

(1) "sunlin"

(2) "dengqiang"

4.lrem 删除n个指定的值

127.0.0.1:6379> lpush list2 one

(integer) 1

127.0.0.1:6379> lpush list2 two

(integer) 2

127.0.0.1:6379> lpush list2 three

(integer) 3

127.0.0.1:6379> lpush list2 one

(integer) 4

127.0.0.1:6379> lpush list2 one

(integer) 5

127.0.0.1:6379> lpush list2 one

(integer) 6

127.0.0.1:6379> lrange list2 0 -1

1) "one"

2) "one"

3) "one"

4) "three"

5) "two"

6) "one"

127.0.0.1:6379> lrem list2 3 one

(integer) 3

127.0.0.1:6379> lrange list2 0 -1

1) "three"

2) "two"

3) "one"

5.llen 查看集合的数量

127.0.0.1:6379> llen list1

(integer) 4

6.lindex 查看该索引的元素

127.0.0.1:6379> lindex list1 0

"two"

7.lpop 从头部删除元素

127.0.0.1:6379> lrange list1 0 -1

1) "two"

2) "one"

3) "3"

4) "2"

127.0.0.1:6379> lpop list1

"two"

127.0.0.1:6379> lrange list1 0 -1

1) "one"

2) "3"

3) "2"

8.rpop 从尾部删除元素

127.0.0.1:6379> lrange list1 0 -1

1) "one"

2) "3"

3) "2"

127.0.0.1:6379> rpop list1

"2"

127.0.0.1:6379> lrange list1 0 -1

1) "one"

2) "3"

9.rpoplpush 从原集合的尾部删除元素,并放到目标集合的头部

127.0.0.1:6379> lrange list1 0 -1

1) "one"

2) "3"

127.0.0.1:6379> lrange list2 0 -1

1) "one"

2) "three"

3) "two"

4) "one"

127.0.0.1:6379> rpoplpush list1 list2

"3"

127.0.0.1:6379> lrange list1 0 -1

1) "one"

127.0.0.1:6379> lrange list2 0 -1

1) "3"

2) "one"

3) "three"

4) "two"

5) "one"

10.ltrim list3 2 3 保留索引2-3的数据

 set类型

1.sadd添加集合,smembers 集合名 遍历集合数据

127.0.0.1:6379> sadd set1 one

(integer) 1

127.0.0.1:6379> sadd set1 two

(integer) 1

127.0.0.1:6379> sadd set1 two

(integer) 0

127.0.0.1:6379> smembers set1

1) "two"

2) "one"

2.srem删除集合中的指定元素

127.0.0.1:6379> smembers set1

1) "aa"

2) "two"

3) "one"

127.0.0.1:6379> srem set1 aa

(integer) 1

127.0.0.1:6379> smembers set1

1) "two"

2) "one"

3.spop 从集合中随机删除元素

spop set1

4.sdiff 2个集合比较,并返回第一个集合的差集

127.0.0.1:6379> smembers set1

1) "two"

2) "one"

127.0.0.1:6379> smembers set2

1) "ghy"

2) "one"

127.0.0.1:6379> sdiff set1 set2

1) "two"

5.sdiffstore 2个集合比较,并返回第一个集合的差集保存进另一个集合中

127.0.0.1:6379> sdiffstore set3 set1 set2

(integer) 1

127.0.0.1:6379> smembers set3

1) "two"

6.sinter 2个集合比较,并返回第一个集合的交集

127.0.0.1:6379> smembers set1

1) "two"

2) "one"

127.0.0.1:6379> smembers set2

1) "ghy"

2) "one"

127.0.0.1:6379> sinter set1 set2

1) "one"

7.sinterstore 2个集合比较,并返回第一个集合的交集保存进另一个集合中

127.0.0.1:6379> smembers set1

1) "two"

2) "one"

127.0.0.1:6379> smembers set2

1) "ghy"

2) "one"

127.0.0.1:6379> sinterstore set5 set1 set2

(integer) 1

127.0.0.1:6379> smembers set5

1) "one"

8.sunion 2个集合比较,并返回两集合的并集

127.0.0.1:6379> smembers set1

1) "two"

2) "one"

127.0.0.1:6379> smembers set2

1) "ghy"

2) "one"

127.0.0.1:6379> sunion set1 set2

1) "two"

2) "one"

3) "ghy"

9.sunionstore 2个集合比较,并返回第一个集合的并集保存进另一个集合中

127.0.0.1:6379> smembers set1

1) "two"

2) "one"

127.0.0.1:6379> smembers set2

1) "ghy"

2) "one"

127.0.0.1:6379> sunionstore set9 set1 set2

(integer) 1

127.0.0.1:6379> smembers set9

1) "two"

2) "one"

3) "ghy"

10.smove 将set1的two元素移动到set2中

smove set1 set2 two

11.scard 查看set集合的集合数

scard set1

12.sismember 判断该元素是否在集合中

127.0.0.1:6379> smembers set1

1) "one"

127.0.0.1:6379> sismember set1 one

(integer) 1

127.0.0.1:6379> sismember set1 one22

(integer) 0

13.srandmember 随机在集合中取出一个元素

srandmember set2

 zset有序集合

127.0.0.1:6379> zrange zset1 0 -1 withscores

1) "one"

2) "1"

3) "two"

4) "3"

127.0.0.1:6379> zrem zset1 two

(integer) 1

127.0.0.1:6379> zrange zset1 0 -1 withscores

1) "one"

2) "1"

3.zincrby 增加指定元素的索引数

127.0.0.1:6379> zrange zset1 0 -1 withscores

1) "one"

2) "1"

127.0.0.1:6379> zincrby zset1 2 one

"3"

127.0.0.1:6379> zrange zset1 0 -1 withscores

1) "one"

2) "3"

4.zrank 集合先升序排序后,再查询元素的索引

127.0.0.1:6379> zrange zset1 0 -1 withscores

1) "one"

2) "two"

127.0.0.1:6379> zrank set1 one

(integer) 0

5.zrevrank 集合先降序排序后,再查询元素的索引

127.0.0.1:6379> zrange zset1 0 -1 withscores

1) "one"

2) "two"

127.0.0.1:6379> zrevrank set1 two

(integer) 0

6.zrevrange 获得集合并降序排序

127.0.0.1:6379> zrevrange 获得集合 set1 0 -1

1) "two"

2) "one"

7.zrangebyscore 按照withscores的序号查询集合/从1开始

127.0.0.1:6379> zrange set1 0 -1 withscores的序号查询集合/从1开始

 1) "one"

 2) "1"

 3) "two"

 4) "2"

 5) "thee"

 6) "3"

 7) "sdg"

 8) "4"

 9) "weg"

10) "5"

127.0.0.1:6379> zrangebyscore set1 2 4 withscores

1) "two"

2) "2"

3) "thee"

4) "3"

5) "sdg"

6) "4"

8.zcount 返回指定下标的元素个数

127.0.0.1:6379> zcount set1 2 4

(integer) 3

9.zcard 返回集合所有元素个数

127.0.0.1:6379> zcard set1

(integer) 5

10.zremrangebyscore 删除指定范围下标的元素

127.0.0.1:6379> zrange set1 0 -1 withscores

 1) "one"

 2) "1"

 3) "two"

 4) "2"

 5) "thee"

 6) "3"

 7) "sdg"

 8) "4"

 9) "weg"

10) "5"

127.0.0.1:6379> zremrangebyscore set1 2 3

(integer) 2

127.0.0.1:6379> zrange set1 0 -1 withscores

1) "one"

2) "1"

3) "sdg"

4) "4"

5) "weg"

6) "5"

11.zremrangebyrank 删除指定范围索引的元素

127.0.0.1:6379> zrange set1 0 -1 withscores

1) "one"

2) "1"

3) "sdg"

4) "4"

5) "weg"

6) "5"

127.0.0.1:6379> zremrangebyrank set1 1 1

(integer) 1

127.0.0.1:6379> zrange set1 0 -1 withscores

1) "one"

2) "1"

3) "weg"

4) "5"