Redis——从入门到放弃
redis简介
Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
http://redisdoc.com/bitmap/index.html #这是redis中文翻译文档的地址
Redis是一个开源(BSD许可),内存数据结构存储,用作数据库,缓存和消息代理。 它支持数据结构,如字符串,哈希表,列表,集合,带有范围查询的排序集,位图,超级日志,具有半径查询和流的地理空间索引。 Redis具有内置复制,Lua脚本,LRU驱逐,事务和不同级别的磁盘持久性,并通过Redis Sentinel提供高可用性并使用Redis Cluster自动分区。
简单来讲redis就是一种基于内存的nosql(也可以叫缓存数据库),支持事务,能够实现定时将数据写入磁盘(数据持久化),支持多种数据结构且能够备份。
由于redis是属于nosql,基于内存存储,实时变化的数据可以直接存储在redis中。它是内存磁盘同步数据库,可以将数据存储至磁盘,也可以从磁盘之中恢复数据,同时也支持订阅与发布和主从同步。
redis日常操作
首先下载源码包,编译安装,将redis-server redis-cli 拷贝到/usr/bin 或者添加到路径。
wget http://download.redis.io/releases/redis-5.0.0.tar.gz
tar -xzvf redis-5.0.0.tar.gz
yum install gcc gcc-c++
make install
cp src/redis-server /usr/bin
cp src/redis-client /usr/bin
如果使用yum安装可能只是3的版本,版本号较低。
redis 的默认端口号6379
将配置文件中:daemonize 改为yes 让redis 在后台运行。
启动redis:redis-server redis.conf
关闭redis:redis-cli shutdown
连接到数据库:redis-cli -h 127.0.0.1 -p 6379 # host可以更改,在配置文件中bind后面接上你要对外提供链接的地址——改为你网卡的真实地址,如192.168.27.128。
切换数据库:select 3 #redis的数据库是通过数字来记录的——1,2,3,4,5,6 非关系型数据库切换数据库select DB-name 关系型数据库use DB-name
redis操作语句
字符串的操作:string
添加:set key value
查看:get key
删除:del key #删除多个使用空格分隔
设置过期时间:set key value EX timeout
查看过期时间:ttl key
查看redis的所有key:key *
127.0.0.1:6379> set username jerry
OK
127.0.0.1:6379> get username
"jerry"
127.0.0.1:6379> set school nanjiang ex 100
OK
127.0.0.1:6379> get school
"nanjiang"
127.0.0.1:6379> ttl school
(integer) 86
127.0.0.1:6379> keys *
1) "name"
2) "jerry"
3) "age"
4) "name1"
5) "school"
6) "lan"
7) "username"
127.0.0.1:6379> del username
(integer) 1
127.0.0.1:6379> keys *
1) "name"
2) "jerry"
3) "age"
4) "name1"
5) "lan"
127.0.0.1:6379> set uuu aaa
OK
127.0.0.1:6379> get uuu
"aaa"
127.0.0.1:6379> set uuu iii
OK
127.0.0.1:6379> get uuu
"iii"
列表的操作:list
在列表左边添加元素:lpush key value
在列表右边添加元素:rpush key value
查看列表中的所有元素:lrange key start stop #列表有下标位从0开始
移除并且返回列表key的头元素:lpop key
移除并且返回列表key的尾元素:rpop key
返回指定的第几个元素:lindex key index
查看列表的长度:llen key
删除列表指定内容:lrem key values
删除整个list:del key
127.0.0.1:6379> lpush lan java perl php python
(integer) 6
127.0.0.1:6379>
127.0.0.1:6379> lrange lan 0 -1
1) "python"
2) "php"
3) "perl"
4) "java"
5) "java"
6) "python"
127.0.0.1:6379> rpush lan ttt aaa
(integer) 8
127.0.0.1:6379> lrange lan 0 -1
1) "python"
2) "php"
3) "perl"
4) "java"
5) "java"
6) "python"
7) "ttt"
8) "aaa"
127.0.0.1:6379> lpop lan
"python"
127.0.0.1:6379> rpop lan
"aaa"
127.0.0.1:6379> lrange lan 0 -1
1) "php"
2) "perl"
3) "java"
4) "java"
5) "python"
6) "ttt"
127.0.0.1:6379> lindex lan 1
"perl"
127.0.0.1:6379> lindex lan 0
"php"
127.0.0.1:6379> lindex lan 2
"java"
127.0.0.1:6379> llen lan
(integer) 6
127.0.0.1:6379> lrem lan 2 java
(integer) 2
127.0.0.1:6379> lrange lan 0 -1
1) "php"
2) "perl"
3) "python"
4) "ttt"
127.0.0.1:6379> del lan
(integer) 1
127.0.0.1:6379> keys *
1) "name"
2) "jerry"
3) "age"
4) "name1"
集合的操作:set
set的特点:自动去重
集合的交集,并集,差集:
sinter key1 key2 (交集) sunion key1 key2 (并集) sdiff key1 key2 (key1-key2) sdiff key2 key1
添加元素到set : sadd key values
查看集合的元素:smembers key
集合的长度:scard key
删除集合的具体内容:srem key values
删除整个集合: del key
127.0.0.1:6379> sadd age 67
(integer) 1
127.0.0.1:6379> smembers age
1) "12"
2) "13"
3) "14"
4) "15"
5) "21"
6) "67"
127.0.0.1:6379> srem age 12 13 14 15
(integer) 4
127.0.0.1:6379> smembers age
1) "21"
2) "67"
127.0.0.1:6379> scard age
(integer) 2
127.0.0.1:6379> del age
(integer) 1
127.0.0.1:6379> keys *
1) "name"
2) "jerry"
3) "name1"
127.0.0.1:6379> sadd age1 22 33 44 55
(integer) 4
127.0.0.1:6379> sadd age2 33 44 66 77
(integer) 4
127.0.0.1:6379> sinter age1 age2
1) "33"
2) "44"
127.0.0.1:6379> sunion age1 age2
1) "22"
2) "33"
3) "44"
4) "55"
5) "66"
6) "77"
127.0.0.1:6379> sdiff age1 age2
1) "22"
2) "55"
127.0.0.1:6379> sdiff age2 age1
1) "66"
2) "77"
哈希表的操作:hash
添加哈希值:hset key filed value
删除哈希值:hdel key filed
查看字段的值:hget key filed
查看所有的字段和值:hgetall key
查看所有字段:hkeys key
查看所有的值:hvals key
查看字段是否存在:hexists key filed
获得哈希总键值对个数:hlen key
127.0.0.1:6379> hset jerry gender male
(integer) 1
127.0.0.1:6379> hget jerry gender
"male"
127.0.0.1:6379> hgetall jerry
1) "high"
2) "170"
3) "age"
4) "22"
5) "weight"
6) "100"
7) "gender"
8) "male"
127.0.0.1:6379> hdel jerry age high weight
(integer) 3
127.0.0.1:6379> hgetall jerry
1) "gender"
2) "male"
127.0.0.1:6379> hkeys jerry
1) "gender"
127.0.0.1:6379> hvals jerry
1) "male"
127.0.0.1:6379> hexists jerry gender
(integer) 1
127.0.0.1:6379> hexists jerry age
(integer) 0
127.0.0.1:6379> hexists jerry high
(integer) 0
127.0.0.1:6379> hlen jerry
(integer) 1
redis——事务
隔离性和原子性
隔离:事务中的命令都会序列化,按顺序执行,不会被其他命令所干扰
原子性:事务中的命令要么全部被执行,要么全没有被执行。
开启一个事务:multi
提交一个事务: exec
回滚事务:discard
127.0.0.1:6379> multi
OK
127.0.0.1:6379> keys *
QUEUED
127.0.0.1:6379> exec
1) 1) "age1"
2) "name"
3) "jerry"
4) "name1"
5) "age2"
127.0.0.1:6379> multi
OK
127.0.0.1:6379> keys *
QUEUED
127.0.0.1:6379> discard
OK
127.0.0.1:6379> discard
(error) ERR DISCARD without MULTI
redis——发布与订阅
发布:
publish channel message
订阅:
subscribe channel
终端一:
127.0.0.1:6379> publish ch1 hello
(integer) 0
127.0.0.1:6379> publish ch1 hello
(integer) 1
127.0.0.1:6379> publish ch2 hello
(integer) 0
127.0.0.1:6379> publish ch2 hello
(integer) 1
127.0.0.1:6379> publish ch1 hello
(integer) 1
终端二:
127.0.0.1:6379> subscribe ch1 ch2
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "ch1"
3) (integer) 1
1) "subscribe"
2) "ch2"
3) (integer) 2
1) "message"
2) "ch2"
3) "hello"
1) "message"
2) "ch1"
3) "hello"