linux-redis学习经历三,了解redis数据和基本操作

Xshell 5 (Build 1339)
Copyright (c) 2002-2017 NetSarang Computer, Inc. All rights reserved.

Type `help' to learn how to use Xshell prompt.
[d:\~]$ 

Connecting to 192.168.0.154:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Mon Jan 29 22:45:11 2018 from 192.168.0.122
[root@bogon ~]# cd ../       
[root@bogon /]# /usr/local/redis/bin/redis-server /usr/local/redis/redis.conf 
[root@bogon /]# ps -ef | grep -i redis
root       2666      1  0 17:47 ?        00:00:00 /usr/local/redis/bin/redis-server *:6379
root       2671   2646  0 17:47 pts/0    00:00:00 grep --color=auto -i redis
[root@bogon /]# /usr/local/redis/bin/redis-cli 
127.0.0.1:6379> CONFIG SET requirepass admin
OK
127.0.0.1:6379> auth admin
OK
127.0.0.1:6379> set name gcc
OK
127.0.0.1:6379> get name
"gcc"
//根据key替换value内容
127.0.0.1:6379> getset name gcc_change
"gcc"
127.0.0.1:6379> get name
"gcc_change"
//根据key追加value内容
127.0.0.1:6379> APPEND name _something
(integer) 20
127.0.0.1:6379> get name
"gcc_change_something"
//hash类型,格式可以理解为 map > >;hset设置内容
127.0.0.1:6379> HSET key1 username gcc
(integer) 1
127.0.0.1:6379> hset key1 password 123
(integer) 1
//hget根据主key和副key获取value;主副只是由本人的理解而来,不喜勿记
127.0.0.1:6379> hget key1 username
"gcc"
127.0.0.1:6379> hget key1 password
"123"
//hmset连续设置多个
127.0.0.1:6379> HMSET key2 username gaocc password 321
OK
127.0.0.1:6379> hget key2 username
"gaocc"
127.0.0.1:6379> hget key2 password
"321"
//获取所有主key下的key-value内容
127.0.0.1:6379> HGETALL key1
1) "username"
2) "gcc"
3) "password"
4) "123"
//根据主key和副key删除
127.0.0.1:6379> HDEL key1 username
(integer) 1
127.0.0.1:6379> HGETALL key1
1) "password"
2) "123"
//不能直接根据主key删除
127.0.0.1:6379> HDEL key2
(error) ERR wrong number of arguments for 'hdel' command
//连续删除多个
127.0.0.1:6379> HDEL key2 username password
(integer) 2
127.0.0.1:6379> HGETALL key2
(empty list or set)
//判断hash类型的名叫key1的集合是否存在,1就是存在,0就是不存在
127.0.0.1:6379> hexists key1 password
(integer) 1
127.0.0.1:6379> hexists key1 username
(integer) 0
//hlen查看集合长度
127.0.0.1:6379> hset key1 age 20
(integer) 1
127.0.0.1:6379> hlen key1
(integer) 2
//查看hash集合所有key
127.0.0.1:6379> hkeys key1
1) "password"
2) "age"
//查看hash集合所有value
127.0.0.1:6379> hvals key1
1) "123"
2) "20"

//list集合添加,lpush从左侧开始添加,格式是集合名字 集合内容
127.0.0.1:6379> lpush list1 a b c d
(integer) 4
127.0.0.1:6379> lpush list1 1 2 3 4
(integer) 8
//lrange查看集合所有内容,0 -1表示从头到尾
127.0.0.1:6379> lrange list1 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
5) "d"
6) "c"
7) "b"
8) "a"
//0 5表示索引从0到5就是前六个
127.0.0.1:6379> lrange list1 0 5
1) "4"
2) "3"
3) "2"
4) "1"
5) "d"
6) "c"
//同理从右侧开始添加(一般习惯)
127.0.0.1:6379> rpush list2 a b c d 1 2 3 4
(integer) 8
127.0.0.1:6379> lrange list2 0 5
1) "a"
2) "b"
3) "c"
4) "d"
5) "1"
6) "2"
//llen,list集合的长度
127.0.0.1:6379> llen list1
(integer) 8
//从左侧弹出一个
127.0.0.1:6379> lpop list1
"4"
127.0.0.1:6379> lrange list1 0 -1
1) "3"
2) "2"
3) "1"
4) "d"
5) "c"
6) "b"
7) "a"
//从右侧弹出一个
127.0.0.1:6379> rpop list1
"a"
127.0.0.1:6379> lrange list1 0 -1
1) "3"
2) "2"
3) "1"
4) "d"
5) "c"
6) "b"
//从左侧添加一个
127.0.0.1:6379> lpushx list1 aa
(integer) 7
127.0.0.1:6379> lrange list1 0 -1
1) "aa"
2) "3"
3) "2"
4) "1"
5) "d"
6) "c"
7) "b"
//从右侧添加一个
127.0.0.1:6379> rpushx list1 bb
(integer) 8
127.0.0.1:6379> lrange list1 0 -1
1) "aa"
2) "3"
3) "2"
4) "1"
5) "d"
6) "c"
7) "b"
8) "bb"
//lset,替换指定的数据
127.0.0.1:6379> lset list1 1 3-1
OK
127.0.0.1:6379> lrange list1 0 -1
1) "aa"
2) "3-1"
3) "2"
4) "1"
5) "d"
6) "c"
7) "b"
8) "bb"
//插入指定的数据
127.0.0.1:6379> linsert list1 before 3-1 3-0
(integer) 9
127.0.0.1:6379> lrange list1 0 -1
1) "aa"
2) "3-0"
3) "3-1"
4) "2"
5) "1"
6) "d"
7) "c"
8) "b"
9) "bb"
127.0.0.1:6379> linsert list1 after 3-1 3-2
(integer) 10
127.0.0.1:6379> lrange list1 0 -1
 1) "aa"
 2) "3-0"
 3) "3-1"
 4) "3-2"
 5) "2"
 6) "1"
 7) "d"
 8) "c"
 9) "b"
10) "bb"


//sadd,set集合添加,有唯一性和无序性
127.0.0.1:6379> sadd set1 a b c
(integer) 3
//查询set集合内容
127.0.0.1:6379> smembers set1
1) "b"
2) "a"
3) "c"
127.0.0.1:6379> sadd set1 a d e
(integer) 2
127.0.0.1:6379> smembers set1
1) "b"
2) "a"
3) "c"
4) "d"
5) "e"
//删除set集合内容
127.0.0.1:6379> srem set1 a b
(integer) 2
127.0.0.1:6379> smembers set1
1) "d"
2) "c"
3) "e"
//判断set集合是否存在指定数据,0不存在,1存在
127.0.0.1:6379> sismember set1 a
(integer) 0
127.0.0.1:6379> sismember set1 d
(integer) 1
//sdiff,获取set集合交集,以前一个为基准
127.0.0.1:6379> sadd set2 a b c d f
(integer) 5
127.0.0.1:6379> sdiff set1 set2
1) "e"
127.0.0.1:6379> sdiff set2 set1
1) "b"
2) "a"
3) "f"
//获取集合交集
127.0.0.1:6379> sinter set1 set2
1) "c"
2) "d"
//获取集合并集
127.0.0.1:6379> sunion set1 set2
1) "d"
2) "b"
3) "f"
4) "c"
5) "a"
6) "e"
//获取集合长度
127.0.0.1:6379> scard set1
(integer) 3
//srandmember随机取一个数,或随机取指定个数的数
127.0.0.1:6379> smembers set1
1) "d"
2) "c"
3) "e"
127.0.0.1:6379> srandmember set1
"d"
127.0.0.1:6379> srandmember set1 2
1) "e"
2) "c"

//scored-set,带分值的set,可用来排序;添加格式是zadd 集合名字 数据分数 数据内容 ……
127.0.0.1:6379> zadd sort1 10 a 12 b 9 c
(integer) 3
//zrange查询内容,默认按照分数升序
127.0.0.1:6379> zrange sort1 0 -1
1) "c"
2) "a"
3) "b"
//添加,若存在会覆盖
127.0.0.1:6379> zadd sort1 8 a
(integer) 0
127.0.0.1:6379> zrange sort1 0 -1
1) "a"
2) "c"
3) "b"
//得到分数
127.0.0.1:6379> zscore sort1 b
"12"
127.0.0.1:6379> zadd sort1 8 d
(integer) 1
127.0.0.1:6379> zrange sort1 0 -1
1) "a"
2) "d"
3) "c"
4) "b"
//zrem 删除指定数据
127.0.0.1:6379> zrem sort1 a
(integer) 1
127.0.0.1:6379> zrange sort1 0 -1
1) "d"
2) "c"
3) "b"
//zcard查询集合长度
127.0.0.1:6379> zcard sort1
(integer) 3
//查到集合,并显示分数
127.0.0.1:6379> zrange sort1 0 -1 withscores
1) "d"
2) "8"
3) "c"
4) "9"
5) "b"
6) "12"
//根据分数排名删除前两个
127.0.0.1:6379> zremrangebyrank sort1 0 1
(integer) 2
127.0.0.1:6379> zrange sort1 0 -1
1) "b"
127.0.0.1:6379> keys *
1) "set1"
2) "key1"
3) "list1"
4) "set2"
5) "list2"
6) "sort1"
7) "name"
//查看key对应的数据类型
127.0.0.1:6379> type set1
set
127.0.0.1:6379> type list1
list
127.0.0.1:6379> type sort1
zset
127.0.0.1:6379> type name
string
127.0.0.1:6379> type key1
hash
127.0.0.1:6379> 

你可能感兴趣的:(linux,redis,集合)