Redis是完全开源免费的,遵守BSD协议,先进的key -value持久化产品。它通常被称为数据结构服务器,因为值(value)可以是字符串(String), 哈希(Map),列表(list),集合(sets)和 有序集合(sorted sets)等类型。
本地测试下载目录:
cd /
lamp:所有下载文件目录
$ wgethttp://download.redis.io/releases/redis-2.8.9.tar.gz
$ tar xzf redis-2.8.9.tar.gz
$ cd redis-2.8.9
使用make命令安装:
cd src目录下:
使用make install目录安装
安装完毕之后整理下redis的命令和配置文件
cd /usr/local/redis/
分别创建bin和etc两个目录:
bin:用来存放redis的一些启动命令
etc:用来存放redis.conf配置文件
然后到redis-2.8.9文件夹中移动
(1)redis.conf到cd/usr/local/redis/etc下
(2) mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-dump redis-cli redis-sentinel redis-server 移动到 /usr/local/redis/etc下
(3)修改redis.conf配置文件中的daemonize no其中no 变为yes表示后台启动
[root@localhost bin]# ./redis-server /usr/local/redis/etc/redis.conf
[root@localhost bin]# ps -ef|grep redis
root 2472 1 0 00:42 ? 00:00:01 ./redis-server *:6379
root 2605 2438 0 01:01 pts/0 00:00:00 grep redis
[root@localhost bin]# netstat -tunpl|grep6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 2472/./redis-server
tcp 0 0 :::6379 :::* LISTEN 2472/./redis-server
[root@localhost bin]# ./redis-cli
127.0.0.1:6379>
redis-cli.exe -h 192.168.2.57 -p6379
String List Set Hash ZSet五种
127.0.0.1:6379> set name yangluan
OK
127.0.0.1:6379> get name
"yangluan"
127.0.0.1:6379> set name haha
OK
127.0.0.1:6379> get name
"haha"
上面中可以看出已经覆盖了原来的值
127.0.0.1:6379> setnx name yangluan
(integer) 0
127.0.0.1:6379> setnx age 20
(integer) 1
127.0.0.1:6379> get age
"20"
127.0.0.1:6379> setnx age 40
(integer) 0
127.0.0.1:6379> get age
"20"
127.0.0.1:6379>
127.0.0.1:6379> setex mycolor 10 red //表示mycolor的存在时间为10秒
OK
127.0.0.1:6379> get mycolor
"red"
127.0.0.1:6379> get mycolor
"red"
127.0.0.1:6379> get mycolor
"red"
127.0.0.1:6379> get mycolor
"red"
127.0.0.1:6379> get mycolor
"red"
127.0.0.1:6379> get mycolor
"red"
127.0.0.1:6379> get mycolor
"red"
127.0.0.1:6379> get mycolor
(nil)
127.0.0.1:6379>
举例:替换某126.com 为qq.com
127.0.0.1:6379> set name [email protected]
OK
127.0.0.1:6379> get name
127.0.0.1:6379> setrange name 5 126.com
(integer) 12
127.0.0.1:6379> get name
127.0.0.1:6379>
127.0.0.1:6379> mset key1 hello key2world
OK
127.0.0.1:6379> get key1
"hello"
127.0.0.1:6379> get key2
"world"
127.0.0.1:6379>
127.0.0.1:6379> msetnx key3 ceshi1 key4ceshi2 key5 ceshi3 key6 ceshi
(integer) 0
127.0.0.1:6379> get key3
"yang"
127.0.0.1:6379>
127.0.0.1:6379> getset key3 yangluan
"yang"
127.0.0.1:6379>
127.0.0.1:6379> get name
127.0.0.1:6379> getrange name 0 3
"yang"
127.0.0.1:6379> getrange name 0 2
"yan"
127.0.0.1:6379>
127.0.0.1:6379> mget key1 key2 key3 key7
1) "hello"
2) "world"
3) "yangluan"
4) (nil)
127.0.0.1:6379>
127.0.0.1:6379> set key7 10
OK
127.0.0.1:6379> incr key7
(integer) 11
127.0.0.1:6379> incr key7
(integer) 12
127.0.0.1:6379> incr key7
(integer) 13
127.0.0.1:6379> incr key7
(integer) 14
127.0.0.1:6379> incr key7
(integer) 15
127.0.0.1:6379> get key7
"15"
127.0.0.1:6379>
127.0.0.1:6379> incrby key 7 5
(error) ERR wrong number of arguments for'incrby' command
127.0.0.1:6379> incrby key7 5
(integer) 20
127.0.0.1:6379> incrby key7 5
(integer) 25
127.0.0.1:6379> get key7
"25"
127.0.0.1:6379> incrby key7 -5
(integer) 20
127.0.0.1:6379> incrby key7 -5
(integer) 15
127.0.0.1:6379> get key7
"15"
127.0.0.1:6379>
127.0.0.1:6379> set key yang
OK
127.0.0.1:6379> append key luan
(integer) 8
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> get key
"yangluan"
127.0.0.1:6379>
127.0.0.1:6379> get key
"yangluan"
127.0.0.1:6379> strlen key
(integer) 8
127.0.0.1:6379>
127.0.0.1:6379> hset map key yangluan
(integer) 1
127.0.0.1:6379> hget map key
"yangluan"
127.0.0.1:6379>
127.0.0.1:6379> hsetnx map key1 yangluan
(integer) 1
127.0.0.1:6379> hget map key1
"yangluan"
127.0.0.1:6379> hsetnx map key1xiugaiyangluan
(integer) 0
127.0.0.1:6379> hget map key1
"yangluan"
127.0.0.1:6379>
127.0.0.1:6379> hmset usertable nameyangluan age 26 job pachong
OK
127.0.0.1:6379> hget usertable name
"yangluan"
127.0.0.1:6379> hget usertable age
"26"
127.0.0.1:6379> hget usertable job
"pachong"
127.0.0.1:6379>
127.0.0.1:6379> hmget usertable name agejob
1) "yangluan"
2) "26"
3) "pachong"
127.0.0.1:6379>
127.0.0.1:6379> hincrby usertable age 5
(integer) 31
127.0.0.1:6379> hmget usertable age
1) "31"
127.0.0.1:6379>
127.0.0.1:6379> hexists usertable age
(integer) 1
127.0.0.1:6379> hexists usertable haha
(integer) 0
127.0.0.1:6379>
· hlen命令:获取hash里所有key的数量
127.0.0.1:6379> hmset usertable nameyangluan age 25 job java
OK
127.0.0.1:6379> hlen usertable
(integer) 3
127.0.0.1:6379>
127.0.0.1:6379> hmset usertable nameyangluan age 25 job java
OK
127.0.0.1:6379> hlen usertable
(integer) 3
127.0.0.1:6379> hget usertable age
"25"
127.0.0.1:6379> hdel usertable age
(integer) 1
127.0.0.1:6379> hget usertable age
(nil)
127.0.0.1:6379>
127.0.0.1:6379> hkeys usertable
1) "name"
2) "job"
127.0.0.1:6379>
127.0.0.1:6379> hvals usertable
1) "yangluan"
2) "java"
127.0.0.1:6379>
127.0.0.1:6379> hgetall usertable
1) "name"
2) "yangluan"
3) "job"
4) "java"
127.0.0.1:6379>
127.0.0.1:6379> lpush mylist an lu yang
(integer) 3
127.0.0.1:6379> lrange mylist 0 -1
1) "yang"
2) "lu"
3) "an"
127.0.0.1:6379>
127.0.0.1:6379> rpush mylist1 an lu yang
(integer) 3
127.0.0.1:6379> lrange mylist1 0 -1
1) "an"
2) "lu"
3) "yang"
127.0.0.1:6379>
127.0.0.1:6379> lpush mylist2 1
(integer) 1
127.0.0.1:6379> lpush mylist2 3
(integer) 2
127.0.0.1:6379> lrange mylist2 0 -1
1) "3"
2) "1"
127.0.0.1:6379> linsert mylist2 1 3
(error) ERR wrong number of arguments for'linsert' command
127.0.0.1:6379> linsert mylist2 before 12
(integer) 3
127.0.0.1:6379> lrange mylist2 0 -1
1) "3"
2) "2"
3) "1"
127.0.0.1:6379>
127.0.0.1:6379> lpush list1 yang
(integer) 1
127.0.0.1:6379> lset list1 0 luan
OK
127.0.0.1:6379> lrange list1 0 -1
1) "luan"
127.0.0.1:6379>
127.0.0.1:6379> lpush list1 one
(integer) 1
127.0.0.1:6379> lpush list1 two
(integer) 2
127.0.0.1:6379> lpush list1 three
(integer) 3
127.0.0.1:6379> lpush list1 one
(integer) 4
127.0.0.1:6379> lpush list1 one
(integer) 5
127.0.0.1:6379> lpush list1 one
(integer) 6
127.0.0.1:6379> lrange list1 0 -1
1) "one"
2) "one"
3) "one"
4) "three"
5) "two"
6) "one"
127.0.0.1:6379> lrem list1 5 one
(integer) 4
127.0.0.1:6379> lrange list1 0 -1
1) "three"
2) "two"
127.0.0.1:6379>
127.0.0.1:6379> rpush list1 one
(integer) 1
127.0.0.1:6379> rpush list1 two
(integer) 2
127.0.0.1:6379> rpush list1 three
(integer) 3
127.0.0.1:6379> rpush list1 four
(integer) 4
127.0.0.1:6379> lrange list1 0 -1
1) "one"
2) "two"
3) "three"
4) "four"
127.0.0.1:6379> ltrim list1 1 2
OK
127.0.0.1:6379> lrange list1 0 -1
1) "two"
2) "three"
127.0.0.1:6379>
127.0.0.1:6379> lrange list1 0 -1
1) "hello"
2) "word"
127.0.0.1:6379> lopo list1
(error) ERR unknown command 'lopo'
127.0.0.1:6379> lpop list1
"hello"
127.0.0.1:6379> lrange list1 0 -1
1) "word"
127.0.0.1:6379>
127.0.0.1:6379> rpush list1 yang
(integer) 1
127.0.0.1:6379> rpush list1 lu
(integer) 2
127.0.0.1:6379> rpush list1 an
(integer) 3
127.0.0.1:6379> lrange list1 0 -1
1) "yang"
2) "lu"
3) "an"
127.0.0.1:6379> rpop list1
"an"
127.0.0.1:6379> lrange list1 0 -1
1) "yang"
2) "lu"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> rpush list1 three
(integer) 1
127.0.0.1:6379> rpush list1 hello
(integer) 2
127.0.0.1:6379> rpush list2 hello
(integer) 1
127.0.0.1:6379> rpush list2 world
(integer) 2
127.0.0.1:6379> lrange list1 0 -1
1) "three"
2) "hello"
127.0.0.1:6379> lrange list2 0 -1
1) "hello"
2) "world"
127.0.0.1:6379> brpoplpush list1 list2
(error) ERR wrong number of arguments for'brpoplpush' command
127.0.0.1:6379> rpoplpush list1 list2
"hello"
127.0.0.1:6379> lrange list1 0 -1
1) "three"
127.0.0.1:6379> lrange list2 0 -1
1) "hello"
2) "hello"
3) "world"
127.0.0.1:6379>
127.0.0.1:6379> lindex list1 0
"three"
127.0.0.1:6379>
127.0.0.1:6379> llen list1
(integer) 1
127.0.0.1:6379> llen list2
(integer) 3
127.0.0.1:6379>
127.0.0.1:6379> sadd set1 yangluan
(integer) 1
127.0.0.1:6379> sadd set1 hello
(integer) 1
127.0.0.1:6379> sadd set1 hello
(integer) 0
127.0.0.1:6379>
127.0.0.1:6379> smembers set1
1) "hello"
2) "yangluan"
127.0.0.1:6379>
127.0.0.1:6379> sadd set1 yangluan
(integer) 1
127.0.0.1:6379> sadd set1 pachong
(integer) 1
127.0.0.1:6379> smembers set1
1) "pachong"
2) "yangluan"
127.0.0.1:6379> srem set1 pachong
(integer) 1
127.0.0.1:6379> smembers set1
1) "yangluan"
127.0.0.1:6379>
127.0.0.1:6379> spop set1
"three"
127.0.0.1:6379>
127.0.0.1:6379> smembers set1
1) "one"
2) "two"
127.0.0.1:6379> smembers set2
1) "three"
2) "two"
127.0.0.1:6379> sdiff set1 set2
1) "one"
127.0.0.1:6379> sdiff set2 set3
1) "two"
2) "three"
127.0.0.1:6379> sdiff set2 set1
1) "three"
127.0.0.1:6379>
127.0.0.1:6379> sadd set1 yangluan
(integer) 1
127.0.0.1:6379> sadd set1 zhangnan
(integer) 1
127.0.0.1:6379> sadd set2 zhangnan
(integer) 1
127.0.0.1:6379> sadd set2 lidapeng
(integer) 1
127.0.0.1:6379> sdiffstore set3 set1set2
(integer) 1
127.0.0.1:6379> smembers set3
1) "yangluan"
127.0.0.1:6379>
127.0.0.1:6379> sadd set1 yangluan
(integer) 1
127.0.0.1:6379> sadd set1 zhangnan
(integer) 1
127.0.0.1:6379> sadd set2 zhangnan
(integer) 1
127.0.0.1:6379> sadd set2 lidapeng
(integer) 1
127.0.0.1:6379> sinter set1 set2
1) "zhangnan"
127.0.0.1:6379>
127.0.0.1:6379> sadd set1 yangluan
(integer) 1
127.0.0.1:6379> sadd set1 zhangnan
(integer) 1
127.0.0.1:6379> sadd set2 zhangnan
(integer) 1
127.0.0.1:6379> sadd set2 lidapeng
(integer) 1
127.0.0.1:6379> sinterstore set3 set1set2
(integer) 1
127.0.0.1:6379> smembers set3
1) "zhangnan"
127.0.0.1:6379>
127.0.0.1:6379> sadd set1 yangluan
(integer) 1
127.0.0.1:6379> sadd set1 zhangnan
(integer) 1
127.0.0.1:6379> sadd set2 zhangnan
(integer) 1
127.0.0.1:6379> sadd set2 lidapeng
(integer) 1
127.0.0.1:6379> sonion set1 set2
(error) ERR unknown command 'sonion'
127.0.0.1:6379> sunion set1 set2
1) "lidapeng"
2) "zhangnan"
3) "yangluan"
127.0.0.1:6379>
127.0.0.1:6379> smembers set1
1) "zhangnan"
2) "yangluan"
127.0.0.1:6379> smembers set2
1) "lidapeng"
2) "zhangnan"
127.0.0.1:6379> smembers set3
(empty list or set)
127.0.0.1:6379> smove set1 set3 yangluan
(integer) 1
127.0.0.1:6379> smembers set3
1) "yangluan"
127.0.0.1:6379> smembers set1
1) "zhangnan"
127.0.0.1:6379>
127.0.0.1:6379> sadd set1 yangluan
(integer) 1
127.0.0.1:6379> sadd set1 zhangnan
(integer) 1
127.0.0.1:6379> scsrd set1
(error) ERR unknown command 'scsrd'
127.0.0.1:6379> scard set1
(integer) 2
127.0.0.1:6379>
127.0.0.1:6379> sadd set1 yangluan
(integer) 1
127.0.0.1:6379> sadd set1 zhangnan
(integer) 1
127.0.0.1:6379> sismember set1 yangluan
(integer) 1
127.0.0.1:6379> sismember set1 lidapneng
(integer) 0
127.0.0.1:6379>
127.0.0.1:6379> sadd set1 yangluan
(integer) 1
127.0.0.1:6379> sadd set1 zhangnan
(integer) 1
127.0.0.1:6379> sadd set1 liuqingzhao
(integer) 1
127.0.0.1:6379> sadd set1 lidapeng
(integer) 1
127.0.0.1:6379> srandmember set1
"yangluan"
127.0.0.1:6379> srandmember set1
"zhangnan"
127.0.0.1:6379> srandmember set1
"yangluan"
127.0.0.1:6379> smembers set1
1) "lidapeng"
2) "liuqingzhao"
3) "zhangnan"
4) "yangluan"
127.0.0.1:6379>
127.0.0.1:6379> zadd zset1 1 yangluan
(integer) 1
127.0.0.1:6379> zadd zset1 2 zhangnan
(integer) 1
127.0.0.1:6379> zadd zset1 3 zhangnan
(integer) 0
127.0.0.1:6379> zrange zset1 0 -1
1) "yangluan"
2) "zhangnan"
127.0.0.1:6379> zrange zset1 0 -1withscores
1) "yangluan"
2) "1"
3) "zhangnan"
4) "3"
127.0.0.1:6379>
127.0.0.1:6379> zadd zset1 1 yangluan
(integer) 1
127.0.0.1:6379> zadd zset1 2 zhangnan
(integer) 1
127.0.0.1:6379> zadd zset1 3 zhangnan
(integer) 0
127.0.0.1:6379> zrange zset1 0 -1withscores
1) "yangluan"
2) "1"
3) "zhangnan"
4) "3"
127.0.0.1:6379> zrem zset1 zhangnan
(integer) 1
127.0.0.1:6379> zrange zset1 0 -1withscores
1) "yangluan"
2) "1"
127.0.0.1:6379>
127.0.0.1:6379> zrange zset1 0 -1withscores
1) "yangluan"
2) "1"
127.0.0.1:6379> zincrby zset1 2 yangluan
"3"
127.0.0.1:6379> zrange zset1 0 -1withscores
1) "yangluan"
2) "3"
127.0.0.1:6379>
127.0.0.1:6379> zrange zset1 0 -1withscores
1) "yangluan"
2) "1"
3) "zhangnan"
4) "2"
5) "lidapeng"
6) "5"
127.0.0.1:6379> zrank zset1 yangluan
(integer) 0
127.0.0.1:6379> zrank zset1 lidapeng
(integer) 2
127.0.0.1:6379>
127.0.0.1:6379> zrange zset1 0 -1withscores
1) "yangluan"
2) "1"
3) "zhangnan"
4) "3"
5) "lidapeng"
6) "5"
127.0.0.1:6379> zrangebyscore zset1 2 6withscores
1) "zhangnan"
2) "3"
3) "lidapeng"
4) "5"
127.0.0.1:6379>
127.0.0.1:6379> zrangebyscore zset1 2 6withscores
1) "zhangnan"
2) "3"
3) "lidapeng"
4) "5"
127.0.0.1:6379> zcount zset1 2 6
(integer) 2
127.0.0.1:6379>
127.0.0.1:6379> zcard zset1
(integer) 3
127.0.0.1:6379>
127.0.0.1:6379> zrange zset1 0 -1withscores
1) "yangluan"
2) "1"
3) "zhangnan"
4) "2"
127.0.0.1:6379> zremrangebyrank zset1 11
(integer) 1
127.0.0.1:6379> zrange zset1 0 -1withscores
1) "yangluan"
2) "1"
127.0.0.1:6379>
127.0.0.1:6379> set string1 yangluan
OK
127.0.0.1:6379> lpush list1 yangluan
(integer) 1
127.0.0.1:6379> hset map1 name yangluan
(integer) 1
127.0.0.1:6379> keys *
1) "map1"
2) "string1"
3) "list1"
127.0.0.1:6379>
127.0.0.1:6379> exists name
(integer) 0
127.0.0.1:6379> exists map1
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379> del map1
(integer) 1
127.0.0.1:6379> exists map1
(integer) 0
127.0.0.1:6379>
127.0.0.1:6379> set name yangluan
OK
127.0.0.1:6379> expire name 10
(integer) 1
127.0.0.1:6379> ttl name
(integer) 8
127.0.0.1:6379> ttl name
(integer) 7
127.0.0.1:6379> ttl name
(integer) 6
127.0.0.1:6379> ttl name
(integer) 6
127.0.0.1:6379> ttl name
(integer) 5
127.0.0.1:6379> ttl name
(integer) 3
127.0.0.1:6379> ttl name
(integer) 2
127.0.0.1:6379> ttl name
(integer) 0
127.0.0.1:6379> ttl name
(integer) -2
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379>
127.0.0.1:6379> select 0
OK
127.0.0.1:6379>
127.0.0.1:6379> select 0
OK
127.0.0.1:6379> set name yang
OK
127.0.0.1:6379> get name
"yang"
127.0.0.1:6379> move name 1
(integer) 1
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get name
"yang"
127.0.0.1:6379[1]>
127.0.0.1:6379> set name yangluan
OK
127.0.0.1:6379> expire name 300
(integer) 1
127.0.0.1:6379> ttl age
(integer) -2
127.0.0.1:6379> ttl name
(integer) 291
127.0.0.1:6379> persist name
(integer) 1
127.0.0.1:6379> ttl name
(integer) -1
127.0.0.1:6379> get name
"yangluan"
127.0.0.1:6379>
127.0.0.1:6379> randomkey
"string1"
127.0.0.1:6379> randomkey
"list1"
127.0.0.1:6379>
127.0.0.1:6379> keys *
1) "name"
2) "string1"
3) "list1"
127.0.0.1:6379> rename list1 list2
OK
127.0.0.1:6379> keys *
1) "list2"
2) "name"
3) "string1"
127.0.0.1:6379>
127.0.0.1:6379> type name
string
127.0.0.1:6379> type list2
list
127.0.0.1:6379> type string1
string
127.0.0.1:6379>
127.0.0.1:6379> echo yangluan
"yangluan"
127.0.0.1:6379>
127.0.0.1:6379> dbsize
(integer) 3
127.0.0.1:6379> config get dir
1) "dir"
2) "/usr/local/redis/bin"
127.0.0.1:6379>
设置客户端连接后进行任何其他执行前需要使用的密码:redis速度相当快 1s可以暴力尝试15万次
vi /usr/local/redis/etc/redis.conf
修改配置文件:
#requirepass foobared
变为requirepass yangluan
[root@localhost bin]#/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
[root@localhost bin]# ./redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth yangluan
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>
[root@localhost bin]# ./redis-cli -ayangluan
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379>
配置slave服务器很简单,只需要在slave的配置中加入一些配置文件:
slaveof192.128.182.128 6379 #指定master的ip和端口
matserauth lamp #这是主机的密码
可以使用ifconfigech0 Ip地址修改Ip地址
#slaveof
slaveof192.168.182.128 6379
#masterauth
masterauth yangluan
[root@localhostbin]# /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
[root@localhostbin]# /usr/local/redis/bin/redis-cli
127.0.0.1:6379>keys *
(emptylist or set)
127.0.0.1:6379>
Slave上:info
role:slave
master_link_status:up
master上:info
slave0:ip=192.168.182.129,port=6379,state=online,offset=383,lag=0
Redis对事物的支持目前还比较简单。Redis只能保证一个client发起的事务中的命令可以连续的执行,而中间不会拆入client的命令。当一个client在一个连接中发出multi命令时,这个链接会进入一个事务上下文,该连接就绪的命令不会立即执行,而是先放到一个队列中,当执行exec命令时,redis会顺序的执行队列中的所有命令.
127.0.0.1:6379> get age
"33"
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set age 10
QUEUED
127.0.0.1:6379> set age 20
QUEUED
127.0.0.1:6379> exec
1) OK
name incr
2) OK
127.0.0.1:6379> get age
"20"
127.0.0.1:6379>
127.0.0.1:6379> get age
"20"
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set age 10
QUEUED
127.0.0.1:6379> set age 100
QUEUED
127.0.0.1:6379> discard
OK
127.0.0.1:6379> get age
"20"
127.0.0.1:6379>
同SVN原理:
watch 监视命令: exec和discard和unwatch会取消监视命令
Redis是一个支持持久化的内存数据库,也就是说reidis需要经常将内存中的数据同步到硬盘来保证持久化,目前redis支持两种持久化方式:
1、snapshotting(快照)也就是默认方式
2、Append-only file(缩写aof)的方式
快照是默认的持久化方式。这种方式是将内存中数据以快照的方式写入到二进制文件中,默认的文件名是dump.rdb。可以通过配置设置自动做快照的方式。我们可以配置redis在n秒内如果超过 m个key被修改就自动做快照。
save 900 1 :表示900秒内如果超过1个key被修改,则发起快照保存
save 300 10:表示300秒内如果超过10个key被修改,则发起快照保存
save 60 10000
由于快照方式是在一定间隔时间做一次的,所以如果redis意外down掉的话,就会丢失最后一次快照后所有修改。aof比快照方式有更好的持久化性,是由于使用aof时,redis会将每一个收到的写命令都通过write函数追加到文件中,当reids重启时会通过重新执行文件中保存的写命令来在内存中重建整个数据库的内容。
appendonly no改为yes //启动aof持久化方式
# appendfsync always //收到命令就立即写入磁盘,性能最慢,但是保证完全的持久化
appendfsync everysec //每秒写入磁盘一次,在性能和持久化方面做了很好的折中
# appendfsync no //完全依赖os,性能最好,持久化没保证
发布订阅(pub/sub)是一种消息通信模式,主要目的是解除消息发布者和消息订阅者之间的耦合,Redis作为一个pub/sub的server,在订阅者和发布者之间起到了消息路由的功能。订阅者可以通过subscribe和psubscibe命令向redis server订阅自己感兴趣的消息类型,将redis将信息类型称为通道。当发布者通过publish命令向redis server发送热定类型的信息时,订阅该信息类型的全部client都会收到此消息
Redis的虚拟内存与操作系统的虚拟内存不是一回事,但是思路和牡蛎都是相同的,就是暂时把不经常访问的数据内存交换到磁盘中,从而腾出宝贵的内存空间用于其他需要访问的数据。尤其是对于redis这样的内存数据库,内存总是不够用的。除了可以讲数据分割到多个redis server外。灵盖能够提高数据库容量的办法就是使用虚拟内存把那些不经常访问的数据交换到磁盘上。
下面是VM的配置
vm-enabled yes //开启VM功能
vm-swap-file/tmp/redis.swap //交换出来的value保存的位置
vm-max-memory 100000 //使用redis的最大内存上限
vm-page-size 32 //每个页面的大小32位
vm-pages 134217728 //最多使用多少页面
vm-max-threads 4 //用于执行valued对象换入的工作线程数量