redis常用操作(string,list) 使用set添加数据 新值会替代旧值,没有提示 127.0.0.1:6379> set key1 shuaiOK127.0.0.1:6379> set key2 linuxOK127.0.0.1:6379> set key1 linuxOK127.0.0.1:6379> get key1 "linux"使用setnx添加数据时,会检测数据是否存在
0表示值存在,没有替换;1表示值不存在,添加进了数据库 127.0.0.1:6379> setnx key1 aaa(integer) 0127.0.0.1:6379> setnx key3 aaa(integer) 1127.0.0.1:6379> get key1"linux"127.0.0.1:6379> get key3"aaa"设定数据的过期时间 #值在前,过期时间在后127.0.0.1:6379> set key3 aaa ex 100OK#过期时间在前,值在后127.0.0.1:6379> setex key 60 aaaOKlist是区分左右的,从左边添加,左变取出,是“栈”,左边添加,右边取出,是“列表”。同理右边添加,右边取出。是“栈”,右边添加,左边取出“列表”。 127.0.0.1:6379> LRANGE list2 0 -11) "ccc"2) "bbb"3) "aaa"127.0.0.1:6379> RPOP list2 "aaa"127.0.0.1:6379> LPOP list2"ccc"127.0.0.1:6379> LRANGE list2 0 -11) "bbb"list插入值 #在bbb值后面,插入值ccc,前面插入值aaa127.0.0.1:6379> LRANGE list2 0 -11) "bbb"127.0.0.1:6379> LINSERT list2 after bbb ccc(integer) 2127.0.0.1:6379> LRANGE list2 0 -11) "bbb"2) "ccc"127.0.0.1:6379> LINSERT list2 before bbb aaa(integer) 3127.0.0.1:6379> LRANGE list2 0 -11) "aaa"2) "bbb"3) "ccc"修改元素值 127.0.0.1:6379> LRANGE list2 0 -11) "aaa"2) "bbb"3) "ccc"#这里的1表示元素的位置而不是元素的值,位置从0开始127.0.0.1:6379> lset list2 1 111OK127.0.0.1:6379> LRANGE list2 0 -11) "aaa"2) "111"3) "ccc"查看元素的值 127.0.0.1:6379> LRANGE list2 0 -11) "aaa"2) "111"3) "ccc"127.0.0.1:6379> LINDEX list2 1"111"127.0.0.1:6379> LINDEX list2 2"ccc"查看列表中有多少个元素 127.0.0.1:6379> LLEN list2(integer) 3set #集合中添加元素 127.0.0.1:6379> SADD set1 aaa bbb(integer) 2#查看集合中所有的元素127.0.0.1:6379> SMEMBERS set11) "aaa"2) "a"3) "c"4) "b"5) "bbb"#删除指定的元素127.0.0.1:6379> SREM set1 aaa bbb(integer) 2127.0.0.1:6379> SMEMBERS set11) "a"2) "c"3) "b"#随机取一个元素,并删除 127.0.0.1:6379> SPOP set1 "c"127.0.0.1:6379> SMEMBERS set11) "a"2) "b"#查看两集合的差集127.0.0.1:6379> SDIFF set2 set11) "1"2) "2"127.0.0.1:6379> SDIFF set1 set2 1) "b"这时,两集合顺序不同得到的结果不同。结果就是,前面一个集合减去他们的交集。#求差集并存储,存储到set3里127.0.0.1:6379> SDIFFSTORE set3 set1 set2(integer) 1127.0.0.1:6379> SMEMBERS set31) "b"#求交集127.0.0.1:6379> SINTER set1 set21) "a"2) "c"3) "2"#求交集并存储到set4中127.0.0.1:6379> SINTERSTORE set4 set1 set2(integer) 3127.0.0.1:6379> SMEMBERS set41) "a"2) "c"3) "2"#求并集127.0.0.1:6379> SUNION set1 set21) "a"2) "c"3) "1"4) "b"5) "2"#求并集并存储set5中127.0.0.1:6379> SUNIONSTORE set5 set1 set2(integer) 5127.0.0.1:6379> SMEMBERS set51) "a"2) "c"3) "1"4) "b"5) "2"#判断一个元素是否在一个集合中,没有就是0,有就是1127.0.0.1:6379> SISMEMBER set1 a(integer) 1127.0.0.1:6379> SISMEMBER set1 aaa(integer) 0#从某集合随机取指定数量的元素,但不删除:127.0.0.1:6379> srandmember set1 31) "ddd"2) "bbb"3) "ccc"127.0.0.1:6379> SMEMBERS set11) "aaa"2) "mmm"3) "ddd"4) "bbb"5) "ccc"- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
sort set #创建有序集合127.0.0.1:6379> zadd zset1 11 123(integer) 1127.0.0.1:6379> zadd zset1 1 abc(integer) 1127.0.0.1:6379> ZRANGE zset1 0 -11) "abc"2) "123"#显示所有元素127.0.0.1:6379> ZRANGE zset1 0 -11) "abc"2) "123"3) "shau"4) "aoi"#反序显示所有元素,并带分值127.0.0.1:6379> ZREVRANGE zset1 0 -11) "aoi"2) "shau"3) "abc"#删除指定元素127.0.0.1:6379> ZREM zset1 123(integer) 1127.0.0.1:6379> ZRANGE zset1 0 -11) "abc"2) "shau"3) "aoi"#返回元素的索引值127.0.0.1:6379> zrank zset1 shau(integer) 1#返回元素的索引值,反向显示127.0.0.1:6379> ZREVRank zset1 aoi(integer) 0#返回集合中所有元素的个数127.0.0.1:6379> ZCARD zset1 (integer) 3#查看某分值范围内的元素个数127.0.0.1:6379> zcount zset1 1 20(integer) 2127.0.0.1:6379> zcount zset1 1 10(integer) 1#查看某分值范围内的元素及分值127.0.0.1:6379> ZRANGEBYSCORE zset1 1 201) "abc"2) "shau"#删除索引范围的元素127.0.0.1:6379> ZREMRANGEBYRANK zset1 0 1(integer) 2127.0.0.1:6379> ZRANGE zset1 0 -11) "aoi"#删除指定分值范围内的元素127.0.0.1:6379> ZREMRANGEBYSCORE zset1 1 6- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
hash #添加数据127.0.0.1:6379> hset hset1 a 1(integer) 1#批量添加数据127.0.0.1:6379> hmset hset1 b 2 c 3 d 4OK#查看所有数据127.0.0.1:6379> HGETALL hset1 1) "a"2) "1"3) "b"4) "2"5) "c"6) "3"7) "d"8) "4"#批量查看值127.0.0.1:6379> hmget hset1 b c 1) "2"2) "3"#删除指定的值127.0.0.1:6379> hdel hset1 b(integer) 1127.0.0.1:6379> HGETALL hset11) "a"2) "1"3) "c"4) "3"5) "d"6) "4"#打印所有的key127.0.0.1:6379> HKEYS hset11) "a"2) "c"3) "d"#打印所有的值127.0.0.1:6379> HVALS hset11) "1"2) "3"3) "4"#查看hash有几个filed127.0.0.1:6379> HLEN hset1(integer) 3
|