1. 实验环境
ubntu16.04,用以下命令检查操作系统的基本配置
1) 检查操作系统的位数: getconfig LONG_BIT
2) 检查操作系统的内核版本:uname -a
3) 检查操作系统版本:lsb_release -a
4) redis安装:apt-get install redis-srver
5) 检查redis的运行状态:
(1) service redis status
(2) sudo netstat -tlp |grep redis
6)配置文件,/etc/redis/redis.conf
2. redis-cli
1)连接
redis-cli 或者 redis-cli -h 127.0.0.1 -p 6379
用法:redis-cli [OPTIONS] [cmd [arg [arg ...]]]
-h <主机ip>,默认是127.0.0.1
-p <端口>,默认是6379
-a <密码>,如果redis加锁,需要传递密码
--help,显示帮助信息
2)info查看服务器的信息
查看全部 info
info server
info clients
info Memory
info Persistence
info Stats
info Replication
info CPU
info Cluster
info Keyspace
3)选择数据库
缺省为0, 用select
select 1
4) 查看所有的键 keys
3. redis数据类型
支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。
1)字符串
127.0.0.1:6379> set name "zhangsan"
OK
127.0.0.1:6379> get name
"zhangsan"
127.0.0.1:6379> set temperature 30.10
OK
127.0.0.1:6379> get temperature
"30.10"
127.0.0.1:6379>
127.0.0.1:6379> incrbyfolat temperature 0.2
(error) ERR unknown command 'incrbyfolat'
127.0.0.1:6379>
127.0.0.1:6379> incrbyfloat temperature 0.2
"30.3"
127.0.0.1:6379> incrbyfloat temperature -0.1
"30.2"
127.0.0.1:6379> incr temperature
(error) ERR value is not an integer or out of range
127.0.0.1:6379>
2)Redis 哈希(Hash)
HSET key field value
HMSET key field value [field value ...]
127.0.0.1:6379> hmset No.1 name "zhangsan" age "30"
OK
127.0.0.1:6379> hgetall No.1
1) "name"
2) "zhangsan"
3) "age"
4) "30"
127.0.0.1:6379> hget No.1 age
"30"
127.0.0.1:6379> hincrby No.1 age 1
(integer) 31
127.0.0.1:6379> hgetall No.1
1) "name"
2) "zhangsan"
3) "age"
4) "31"
127.0.0.1:6379>
3)Redis 列表(List)
Redis列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)。
127.0.0.1:6379> llen mp#001
(integer) 0
127.0.0.1:6379>
127.0.0.1:6379> lpush mp#001 20.1
(integer) 1
127.0.0.1:6379> lpush mp#001 20.3
(integer) 2
127.0.0.1:6379> lpush mp#001 20.6
(integer) 3
127.0.0.1:6379> lpush mp#001 20.1
(integer) 4
127.0.0.1:6379> lpush mp#001 20.1
(integer) 5
127.0.0.1:6379> lpush mp#001 20.2
(integer) 6
127.0.0.1:6379> lpush mp#001 20.2
(integer) 7
127.0.0.1:6379> lpush mp#001 20.2\3
(integer) 8
127.0.0.1:6379> lrem mp#001 7
(error) ERR wrong number of arguments for 'lrem' command
127.0.0.1:6379> lrem mp#001 7 20.2\3
(integer) 1
127.0.0.1:6379> lrange mp#001 1 10
1) "20.2"
2) "20.1"
3) "20.1"
4) "20.6"
5) "20.3"
6) "20.1"
127.0.0.1:6379> llen mp#001
(integer) 7
127.0.0.1:6379> rpop mp#001
"20.1"
127.0.0.1:6379>
4)Redis 集合(set)
Redis的Set是String类型的无序集合。集合成员是唯一的,集合中不能出现重复的数据。
127.0.0.1:6379> sadd my_hosts db01
(integer) 1
127.0.0.1:6379> sadd my_hosts db02
(integer) 1
127.0.0.1:6379> smembers my_hosts
1) "db01"
2) "db02"
127.0.0.1:6379> scard my_hosts
(integer) 2
127.0.0.1:6379> sismember 'web01' my_hosts
(integer) 0
127.0.0.1:6379> sismember web01 my_hosts
(integer) 0
127.0.0.1:6379> sadd my_hosts web01
(integer) 1
127.0.0.1:6379> smembers my_hosts
1) "web01"
2) "db01"
3) "db02"
127.0.0.1:6379> sadd my_hosts db01
(integer) 0
127.0.0.1:6379> srem my_hosts web01
(integer) 1
127.0.0.1:6379> smembers my_hosts
1) "db01"
2) "db02"
127.0.0.1:6379>
5) Redis有序集合(sorted set)
Redis有序集合是string类型元素的集合,且不允许重复的成员。有序集合的每个元素关联一个double类型分数,通过分数对集合中的成员进行从小到大的排序。有序集合的成员是唯一的,但分数(score)可以重复。
oliver@oliver:~$ redis-cli
127.0.0.1:6379>
127.0.0.1:6379> zadd zhangsan 66 math
(integer) 1
127.0.0.1:6379> zadd zhangsan 90 engilsh
(integer) 1
127.0.0.1:6379> zadd zhangsan 86 chinese
(integer) 1
127.0.0.1:6379> zcard zhangsan
(integer) 3
127.0.0.1:6379> ZREVRANK key member
(nil)
127.0.0.1:6379> ZREVRANK zhangsan
(error) ERR wrong number of arguments for 'zrevrank' command
127.0.0.1:6379> ZREVRANK zhangsan math
(integer) 2
127.0.0.1:6379> ZREVRANK zhangsan chinese
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379> ZREVRANK zhangsan chinese
(integer) 1
127.0.0.1:6379> ZREVRANK zhangsan english
(nil)
127.0.0.1:6379> ZREVRANK zhangsan engilsh
(integer) 0
127.0.0.1:6379>