redis安装及数据类型简介(string、list、set、sorted_set、hash)
一:简介:
redis国内最大的案例---》新浪微博
memcache:是key-value数据库
数据类型:只支持key value数据
过期策略:支持
持久化:不支持(可以通过三方程序)
主从复制:不支持
虚拟内存:不支持
使用场景:可以保存session,存放单一的数据,更加轻量级,效率更高
redis:是key-value数据库
数据类型:支持五种数据类型
过期策略:支持
持久化:支持
主从复制:支持
虚拟内存:不支持
支持五种数据类型,可以保存购物车的商品
二:编译安装redis 3.0.7
本机编译安装redis 3.0.7:
tar xvf redis-3.0.7.tar.gz
cd redis-3.0.7
make PREFIX=/usr/local/redis install
制作启动脚本:
cp utils/redis_init_script /etc/init.d/redisd
chmod a+x /etc/init.d/redisd
[root@node5 redis-3.0.7]# vim /etc/init.d/redisd
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
REDISPORT=6379
EXEC=/usr/local/redis/bin/redis-server
CLIEXEC=/usr/local/redis/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF="/etc/redis/${REDISPORT}.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
配置文件:
[root@node5 redis-3.0.7]# mkdir /etc/redis
[root@node5 redis-3.0.7]# cp redis.conf /etc/redis/6379.conf #对应配置文件/etc/redis/6379.conf
启动测试:
/etc/init.d/redisd start
[root@node5 redis-3.0.7]# /etc/init.d/redisd start
Starting Redis server...
65171:M 21 Mar 07:14:15.712 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.7 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 65171
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
这是在前台启动的,如果要想在后台启动,需要改一下配置文件:
daemonize no 改为 daemonize yes
然后在重新启动服务即可
通过salt-master批量安装:
1、编辑redis.sls文件:
/etc/salt/states/init
redis-install:
file.managed: #调用file模块的managed方法
- name: /usr/local/src/redis-3.0.7.tar.gz #客户端的文件路径
- source: salt://init/files/redis-3.0.7.tar.gz #在服务器的路径
- user: root
- group: root
- mode: 755
cmd.run: #执行远程命令,使用cmd的run方法
- name: cd /usr/local/src/ && tar xvf redis-3.0.7.tar.gz && cd redis-3.0.7 && make PREFIX=/usr/local/redis install #编译安装
- unless: test -d /usr/local/redis #如果目录存在就不安装了
- require: #依赖
- file: redis-install #编译之前需要依赖redis-install执行成功
redis-config:
file.managed:
- name: /etc/redis/6379.conf #客户端的配置文件路径
- source: salt://init/files/6379.conf #服务器的配置文件路径
- user: root
- group: root
- mode: 644
redis-service:
file.managed:
- name: /etc/init.d/redis
- source: salt://init/files/redis
- user: root
- group: root
- mode: 755
cmd.run:
- name: chkconfig --add redis && chkconfig redis on #设置服务开机启动
- unless: chkconfig --list | grep redis #如果已经在chkconfig --list列表就不执行上一步骤
service.running: #这是服务启动
- name: redis
- enable: True
- watch: #监控的文件
- file: redis-config
- require: 服务启动依赖 redis-install和redis-service
- cmd: redis-install
- cmd: redis-service
2、准备文件:
[root@node5 init]# ls files/redis #启动脚本
files/redis
[root@node5 init]# ls files/6379.conf #配置文件
files/6379.conf
[root@node5 init]# ls files/redis-3.0.7.tar.gz #源码包
files/redis-3.0.7.tar.gz
3、执行命令:
[root@node5 init]# salt "node6.a.com" state.sls init.redis
4、客户端验证:
root@node6 ~]# /etc/init.d/redis start
Starting Redis server...
[root@node6 ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:6379 *:*
LISTEN 0 128 :::6379 :::*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 *:111 *:*
LISTEN 0 128 :::41586 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:46715 *:*
LISTEN 0 128 :::10050 :::*
LISTEN 0 128 *:10050 *:*
salt-master执行命令后后如果报错就根据返回的红色错误信息进行排错
三:redis数据类型格式
3.1、字符串数据格式
SET 设置key,设置的值都是字符串格式string:
192.168.10.205:6379> set key1 value1
OK
192.168.10.205:6379> set key3 value3
OK
192.168.10.205:6379> set key2 value2
OK
GET 获取key的值:
192.168.10.205:6379> get key1
"value1"
192.168.10.205:6379> get key3 #获取指定的key的值
"value3"
不能一次多个key的值:
192.168.10.205:6379> get key1 key2
(error) ERR wrong number of arguments for 'get' command
KEYS 显示所有的key
192.168.10.205:6379> KEYS * 1) "ss" 2) "key2" 3) "list1" 4) "key1" 5) "key4"
EXISTS 判断key是否存在:
192.168.10.205:6379> EXISTS key1
(integer) 1
192.168.10.205:6379> EXISTS key9
(integer) 0
返回1表示存在,0表示不存在
DEL 删除指定的key:
192.168.10.205:6379> del key1 key2 key9
(integer) 2
返回0表示没有指定的key可以删除,返回其他整数表示成功删除的key的数量,没有的key不报错
TYPE 获取key的类型
[root@node5 init]# redis-cli -h 192.168.10.205 -p 6379
192.168.10.205:6379> set key1 value1 #set key
OK
192.168.10.205:6379> get key1 #获取key的值
"value1"
INFO: 当前服务器的状态
SELECT: 更换数据库,redis默认支持16个数据库,默认在数据库0,可以使用sekect 进行更换数据库
192.168.10.205:6379> SELECT 1
OK
192.168.10.205:6379[1]> set key1 value1
OK
192.168.10.205:6379[1]> KEYS *
1) "key1"
清空整个数据库:FLUSHALL
192.168.10.205:6379[1]> FLUSHALL
APPEND:附加值给value
127.0.0.1:6379> get key1
"value1"
127.0.0.1:6379> APPEND key1 cc
(integer) 8
127.0.0.1:6379> get key1
"value1cc"
INCR:如果key不存在就创建并设置value默认为1:
127.0.0.1:6379> INCR num #如果key不存在就创建并设置默认值为1
(integer) 1
127.0.0.1:6379> get num
"1"
127.0.0.1:6379> INCR num #再次执行的值会自增1
(integer) 2
127.0.0.1:6379> get num
"2"
127.0.0.1:6379> get key1
"value1cc"
127.0.0.1:6379> INCR key1 #如果key已经存则报错
(error) ERR value is not an integer or out of range
DECR:value自减1:
127.0.0.1:6379> get num
"5"
127.0.0.1:6379> DECR num
(integer) 4
127.0.0.1:6379> get num
"4"
127.0.0.1:6379> DECR num
(integer) 3
127.0.0.1:6379> get num
"3"
INCRBY :指定自增value的整数值:
127.0.0.1:6379> get num
"13"
127.0.0.1:6379> INCRBY num 7
(integer) 20
127.0.0.1:6379> get num
"20"
127.0.0.1:6379> INCRBY num 10
(integer) 30
127.0.0.1:6379> get num
"30"
DECRBY:指定自检value的值:
127.0.0.1:6379> get num
"41"
127.0.0.1:6379> DECRBY num 10
(integer) 31
127.0.0.1:6379> get num
"31"
127.0.0.1:6379> DECRBY num 10
(integer) 21
127.0.0.1:6379> get num
"21"
INCRBYFLOAT :设置浮点数的value
127.0.0.1:6379> INCRBYFLOAT num1 0.1
"0.1"
127.0.0.1:6379> INCRBYFLOAT num1 0.1
"0.2"
127.0.0.1:6379> INCRBYFLOAT num1 0.1
"0.3"
127.0.0.1:6379> get num1
"0.3"
MSET 和 MGET:批量创建和获取key
127.0.0.1:6379> MSET k1 v2 k2 v2 k3 v3
OK
127.0.0.1:6379> MGET k1 k2 k3
1) "v2"
2) "v2"
3) "v3"
STRLEN:获取key字符串的长度
127.0.0.1:6379> STRLEN key1
(integer) 8
127.0.0.1:6379> STRLEN k1
(integer) 2
127.0.0.1:6379> get key1
"value1cc"
127.0.0.1:6379> get k1
"v2"
3.2、散列数据类型:
HSET:命令用于为哈希表中的字段赋值,如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作,如果字段已经存在于哈希表中,旧值将被覆盖
HGET:获取key:
127.0.0.1:6379> HSET shouji name iphone (integer) 1 127.0.0.1:6379> HSET shouji color red (integer) 1 127.0.0.1:6379> HSET shouji price 4888 (integer) 1 127.0.0.1:6379> HGET shouji name "iphone" 127.0.0.1:6379> HGET shouji color "red" 127.0.0.1:6379> HGET shouji price "4888"
HGETALL:获取key的所有值
127.0.0.1:6379> HGETALL shouji 1) "name" 2) "iphone" 3) "color" 4) "red" 5) "price" 6) "4888"
HMSET、HMGET、HMGETALL:批量创建、批量获取并获取所有的key:
127.0.0.1:6379> HMSET shouji name xiaoji color baise storge 16G #批量创建 OK 127.0.0.1:6379> HMGET shouji name storge #批量获取 1) "xiaoji" 2) "16G" 127.0.0.1:6379> HGETALL shouji #获取所有的key 1) "name" 2) "xiaoji" 3) "color" 4) "baise" 5) "price" 6) "4888" 7) "name1" 8) "xiaomio" 9) "storge" 10) "16G"
HDEL:删除指定的key
127.0.0.1:6379> HDEL shouji name #删除值的key (integer) 1 127.0.0.1:6379> HGETALL shouji #再次查看 1) "color" 2) "baise" 3) "price" 4) "4888" 5) "name1" 6) "xiaomio" 7) "storge" 8) "16G"
3.3 列表数据类型:不同的数据类型的命令是不通用的,如list与string的命令是不能通用的。出了set命令之外
LPUSH与RPUSH:
127.0.0.1:6379> LPUSH list1 a #从左侧添加 (integer) 1 127.0.0.1:6379> LPUSH list1 b (integer) 2 127.0.0.1:6379> LPUSH list1 c (integer) 3 127.0.0.1:6379> RPUSH list 1 #从右侧添加 (integer) 1 127.0.0.1:6379> TYPE list1 #查看类型 list
LLEN:获取列表的长度:
127.0.0.1:6379> LLEN list1
(integer) 3
LPOP 与 RPOP:从左侧和右侧弹出列表中的值:
127.0.0.1:6379> LPOP list1 "c" 127.0.0.1:6379> RPOP list1 "a"
LINDEX:获取最后一个元素:
LRANGE :获取指定范围的元素
127.0.0.1:6379> LINDEX list1 -1 "b" 127.0.0.1:6379> LINDEX list1 3 "a"
127.0.0.1:6379> LRANGE list1 0 -1 1) "d" 2) "c" 3) "b" 4) "a" 5) "b"
3.4、集合数据类型的操作:集合默认是无序的,列表是有序的,有序是只按照添加的循序保持位置
SADD:创建并给集合赋值
192.168.10.205:6379> SADD set1 0 99 1 (integer) 3 192.168.10.205:6379> SADD set1 a b c (integer) 3
SMEMBERS:获取集合中的所有值
192.168.10.205:6379> SMEMBERS set1 1) "1" 2) "99" 3) "0" 4) "c" 5) "b" 6) "a"
SISMEMBER:判断一个值是不是在集合当中,在返回1,否则返回0
192.168.10.205:6379> SISMEMBER set1 a (integer) 1 192.168.10.205:6379> SISMEMBER set1 p (integer) 0
SDIFF:求两个集合的差集:
192.168.10.205:6379> SADD jihe1 1 2 3 a (integer) 4 192.168.10.205:6379> SADD jihe2 1 2 3 b (integer) 4 192.168.10.205:6379> SDIFF jihe1 jihe2 #差集,求集合1有而集合2没有的值 1) "a" 192.168.10.205:6379> SDIFF jihe2 jihe1 1) "b"
SINTER:求并集,即在多个集合当中共同包含的值,可以是多个集合
192.168.10.205:6379> SADD jihe3 2 3 c (integer) 3 192.168.10.205:6379> SINTER jihe1 jihe2 jihe3 1) "2" 2) "3"
SUNION:求并集,即在每个元素都出现的值只统计一次
192.168.10.205:6379> SUNION jihe1 jihe2 jihe3 1) "c" 2) "1" 3) "b" 4) "3" 5) "a" 6) "2"
3.5 有序队列:保持值的位置固定
192.168.10.205:6379> ZADD youxv 1 b 2 a 3 c #通过设置分数创建有序队列,值会安装自己的分数排列位置 (integer) 3 192.168.10.205:6379> ZSCORE youxv a #获取值的分数 "2" 192.168.10.205:6379> ZSCORE youxv b "1"
ZRANGE:根据值的分数排序获取值
192.168.10.205:6379> ZRANGE youxv 0 3 #0和3为取值的下标范围,可以通过LEN统计,超出值的总数不报错 1) "b" 2) "a" 3) "c"