Redis_weixin_30501857的博客-程序员宅基地

技术标签: 数据结构与算法  数据库  ruby  

Redis 是内存数据库,是nosql数据库。
基于key value存储的数据,key只能是string类型, v 可以使多种类型。

 

关系型数据库的索引机制; btree

 

 

  

https://visualgo.net/zh

麻省理工:算法导论:网易公开课
清华大学:数据结构:学堂在线

<<db2  教材 官方>>  join

《深入理解计算机系统》

《MR设计模式》
《nosql精粹?》

  

 

Redis

开源的(BSD协议),使用ANSI  C 编写,基于内存的且支持持久化,高性能的Key-Value的NoSQL数据库
支持数据结构类型丰富,有如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。
丰富的支持主流语言的客户端,C、C++、Python、Erlang、R、C#、Java、PHP、Objective-C、Perl、Ruby、Scala、Go、JavaScript

用途:缓存(StackOverFlow)、数据库(微博)、消息中间件(微博)

官方网站: http://www.redis.io

a=1 a=k a=(1,3,98)a={x=11,w=88}    
客户端完成业务

 

 

 

 

行业经验和项目报价和评议比较重要。

  

版本:
分布式  3.18;  伪分布式3.0一下,2.8.18 

单节点部署
安装gcc和tcl准备编译环境
yum install -y gcc tcl 
/root/software
tar -zxvf redis-2.8.18.tar.gz 
 cd redis-2.8.18
 make && make PREFIX=/opt/sxt/redis install  ## 编译复制到opt下
 cd /opt/sxt/redis/
vi /etc/profile  
export REDIS_HOME=/opt/sxt/redis
export PATH=$JAVA_HOME/bin:$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$HBASE_HOME/bin:$REDIS_HOME/bin
source /etc/profile

 cd ~/software/redis-2.8.18
 cd utils/
[[email protected] utils]# ./install_server.sh   ## 安装redis 服务端 实例。
Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/opt/sxt/redis/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /opt/sxt/redis/bin/redis-server
Cli Executable : /opt/sxt/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

## 一个redis服务可以再一个节点上有多个实例,(选择不同的通讯端口)
## 一个服务实例最多有16个数据库。  从0开始。

## 查看帮助
[[email protected] utils]# redis-server -h
[[email protected] utils]# redis-cli  -h
[[email protected] ~]# redis-cli 
127.0.0.1:6379> help set

  SET key value [EX seconds] [PX milliseconds] [NX|XX]
  summary: Set the string value of a key
  since: 1.0.0
  group: string

127.0.0.1:6379> set k1 abc
OK
127.0.0.1:6379> get k1
"abc"
127.0.0.1:6379> set k2 5
OK
127.0.0.1:6379> get k2
"5"
127.0.0.1:6379> keys *
1) "k1"
2) "k2"
127.0.0.1:6379> SETNX k1 dww  ## nx 不存在时才增加(只能添加)
(integer) 0
127.0.0.1:6379> get k1
"abc"
127.0.0.1:6379> set k1 dww xx
OK
127.0.0.1:6379> get k1
"dww"
127.0.0.1:6379> set k2 sdlwlw xx  ## 存在时才修改(只能修改)
OK
127.0.0.1:6379> get k2
"sdlwlw"
127.0.0.1:6379> set k3 sdfdfs xx
(nil)
127.0.0.1:6379> get k3
(nil)
127.0.0.1:6379> keys *
1) "k1"
2) "k2"
127.0.0.1:6379> type k1
string
127.0.0.1:6379> get k1
"dww"
127.0.0.1:6379> type k2
string
127.0.0.1:6379> get k2
"sdlwlw"
127.0.0.1:6379> set k3 99
OK
127.0.0.1:6379> type k3
string
127.0.0.1:6379> OBJECT encoding k2
"raw"
127.0.0.1:6379> OBJECT encoding k3   ## 属性,可以看到string是否适合位运算。
"int"
127.0.0.1:6379> get k3
"99"

  

127.0.0.1:6379> help MGET MGET key [key ...] summary: Get the values of all the given keys since: 1.0.0 group: string 127.0.0.1:6379> keys * 1) "k3" 2) "k1" 3) "k2" 127.0.0.1:6379> MSET k4 5 k5 abc k6 werw OK 127.0.0.1:6379> keys * 1) "k5" 2) "k6" 3) "k1" 4) "k4" 5) "k3" 6) "k2" 127.0.0.1:6379> MSETNX k6 dfsd k7 dkfd (integer) 0 127.0.0.1:6379> keys * 1) "k5" 2) "k6" 3) "k1" 4) "k4" 5) "k3" 6) "k2" 127.0.0.1:6379> MSETNX k7 dfsd k8 dkfd (integer) 1 127.0.0.1:6379> keys * 1) "k7" 2) "k5" 3) "k6" 4) "k1" 5) "k4" 6) "k8" 7) "k3" 8) "k2" 127.0.0.1:6379> MSETNX k9 eerw k8 ewrwe (integer) 0 127.0.0.1:6379> keys * 1) "k7" 2) "k5" 3) "k6" 4) "k1" 5) "k4" 6) "k8" 7) "k3" 8) "k2" 127.0.0.1:6379> set kab www OK 127.0.0.1:6379> keys * 1) "kab" 2) "k5" 3) "k4" 4) "k1" 5) "k3" 6) "k8" 7) "k2" 8) "k7" 9) "k6" 127.0.0.1:6379> keys ? ## 正则匹配一个字符 (empty list or set) 127.0.0.1:6379> keys ?? 1) "k5" 2) "k4" 3) "k1" 4) "k3" 5) "k8" 6) "k2" 7) "k7" 8) "k6" 127.0.0.1:6379> keys ??? ## 匹配三个字符 1) "kab" 127.0.0.1:6379> type k1 string 127.0.0.1:6379> STRLEN ke (integer) 0 127.0.0.1:6379> STRLEN k1 (integer) 3 127.0.0.1:6379> STRLEN k3 (integer) 2 127.0.0.1:6379> FLUSHALL ## 清空所有数据 OK 127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> set k21 werwe OK 127.0.0.1:6379> FLUSHDB ## 清空数据库 OK 127.0.0.1:6379> keys * (empty list or set) [[email protected] ~]# redis-cli -n 1 127.0.0.1:6379[1]> get k1 (nil) 127.0.0.1:6379[1]> keys * (empty list or set) 127.0.0.1:6379[1]> quit [[email protected] ~]# redis-cli 127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> get k1 (nil) 127.0.0.1:6379> set k1 a OK 127.0.0.1:6379> get ki (nil) 127.0.0.1:6379> get k1 "a" 127.0.0.1:6379> exit [[email protected] ~]# redis-cli -n 1 ##链接数据库1 127.0.0.1:6379[1]> get k1 (nil) 127.0.0.1:6379[1]> keys * (empty list or set) 127.0.0.1:6379[1]> quit [[email protected] ~]# redis-cli -n 0 127.0.0.1:6379> get k1 "a" 127.0.0.1:6379> set k2 99 OK 127.0.0.1:6379> STRLEN k2 (integer) 2 127.0.0.1:6379> set k3 999 OK 127.0.0.1:6379> STRLEN k3 (integer) 3 127.0.0.1:6379> set k4 中 ## xshell是utf-8 OK 127.0.0.1:6379> STRLEN k4 (integer) 3 127.0.0.1:6379> set k5 中 OK 127.0.0.1:6379> STRLEN k5 (integer) 3
[[email protected] ~]# redis-cli           ## xhsell选择gbk编码链接时。
127.0.0.1:6379> set k5 中
OK
127.0.0.1:6379> STRLEN k5
(integer) 2

  


127.0.0.1:6379> [[email protected] ~]# redis-cli -n 0 127.0.0.1:6379> get k4 ##展示的是字节码 "\xe4\xb8\xad" 127.0.0.1:6379> [[email protected] ~]# redis-cli --raw ##原始数据类型显示 127.0.0.1:6379> get k4 中 127.0.0.1:6379> get k5 א 127.0.0.1:6379> get k4 中 127.0.0.1:6379> [[email protected] ~]# redis-cli 127.0.0.1:6379> EXISTS k5 (integer) 1 127.0.0.1:6379> get k1 "a" 127.0.0.1:6379> GETSET k1 b "a" 127.0.0.1:6379> get k1 "b" 127.0.0.1:6379> APPEND k1 bd (integer) 3 127.0.0.1:6379> get k1 "bbd" 127.0.0.1:6379> get k1 "bbd" 127.0.0.1:6379> append k1 werw (integer) 7 127.0.0.1:6379> get k1 "bbdwerw" 127.0.0.1:6379> GETRANGE (error) ERR wrong number of arguments for 'getrange' command 127.0.0.1:6379> help GETRANGE GETRANGE key start end summary: Get a substring of the string stored at a key since: 2.4.0 group: string 127.0.0.1:6379> GETRANGE k1 3 (error) ERR wrong number of arguments for 'getrange' command 127.0.0.1:6379> GETRANGE k1 3 -1 "werw" 127.0.0.1:6379> GETRANGE k1 -2 -2 "r" 127.0.0.1:6379> get k1 "bbdwerw" 127.0.0.1:6379> INCR k1 (error) ERR value is not an integer or out of range 127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> set k1 99 OK 127.0.0.1:6379> INCR k1 ## 数值类型自增长 (integer) 100 127.0.0.1:6379> INCR k1 (integer) 101 127.0.0.1:6379> INCR k1 (integer) 102 127.0.0.1:6379> DECR k1 (integer) 101 127.0.0.1:6379> DECR k1 (integer) 100 127.0.0.1:6379> INCRBY k1 5 (integer) 105 127.0.0.1:6379> INCRBY k1 5 ## 数值类型自增长步长 (integer) 110 127.0.0.1:6379> INCRBYFLOAT 5.5 (error) ERR wrong number of arguments for 'incrbyfloat' command 127.0.0.1:6379> INCRBYFLOAT k1 5.5 "115.5" 127.0.0.1:6379> SETBIT k1 1 1 (integer) 0 127.0.0.1:6379> get k1 "q15.5" 127.0.0.1:6379> get k1 "q15.5" 127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> SETBIT k1 1 1 ## 位图数据 1byte 01000000 sacii 表的64 = @ (integer) 0 127.0.0.1:6379> get k1 "@" 127.0.0.1:6379> help SETBIT SETBIT key offset value summary: Sets or clears the bit at offset in the string value stored at key since: 2.2.0 group: string 127.0.0.1:6379> SETBIT k1 7 1 1byte 01000001 (integer) 0 127.0.0.1:6379> get k1 "A" 127.0.0.1:6379> SETBIT k1 9 1 2byte 01000001 01000000 [email protected] (integer) 0 127.0.0.1:6379> get k1 "[email protected]" 127.0.0.1:6379>

  

  

 

 

 

 

 

 

127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> SETBIT k1 1 1
(integer) 0
127.0.0.1:6379> get k1
"@"
127.0.0.1:6379> SETBIT k1 7 1
(integer) 0
127.0.0.1:6379> get k1
"A"
127.0.0.1:6379> SETBIT k1 9 1
(integer) 0
127.0.0.1:6379> get k1
"[email protected]"
127.0.0.1:6379> GETBIT k1 9
(integer) 1
127.0.0.1:6379> GETBIT k1 1
(integer) 1
127.0.0.1:6379> GETBIT k1 7
(integer) 1
127.0.0.1:6379> GETBIT k1 6
(integer) 0
127.0.0.1:6379> BITPOS k1 1 
(integer) 1
127.0.0.1:6379> BITPOS k1 1 0 0
(integer) 1
127.0.0.1:6379> BITPOS k1 1 0 1
(integer) 1
127.0.0.1:6379> SETBIT k1 1 0
(integer) 1
127.0.0.1:6379> BITPOS k1 1 0 1
(integer) 7
127.0.0.1:6379> SETBIT k1 7 0
(integer) 1
127.0.0.1:6379> BITPOS k1 1 0 1
(integer) 9
### 解释
0000000  01000000
127.0.0.1:6379> BITPOS k1 1 0 1
查找1在0~1的字节索引中的二进制位索引(偏移量)

  

 

 

 

 

127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> SETBIT k1 1 1
(integer) 0
127.0.0.1:6379> SETBIT k2 7 1
(integer) 0
127.0.0.1:6379> BITOP and k3 k1 k2
(integer) 1
127.0.0.1:6379> get k3
"\x00"
127.0.0.1:6379> BITOP or k4 k1 k2
(integer) 1
127.0.0.1:6379> get k4
"A"

  

127.0.0.1:6379> help BITCOUNT

  BITCOUNT key [start] [end]
  summary: Count set bits in a string
  since: 2.6.0
  group: string

127.0.0.1:6379> SETBIT k1 7 1
(integer) 0
127.0.0.1:6379> SETBIT k1 9 1
(integer) 0
127.0.0.1:6379> BITCOUNT k1 0 0
(integer) 2
127.0.0.1:6379> BITCOUNT k1 0 1
(integer) 3
## 统计字节区间范围内的二进制总数
127.0.0.1:6379> BITCOUNT k1 0 -1
(integer) 3
127.0.0.1:6379> BITCOUNT k1 -2 -1

  

 

 

一年内某用户上线次数: 365为bit, 每天对应一个位,0,1表示上线。

 

 

 

 

 

 

 

 

 

 

 

127.0.0.1:6379> LPUSH l1 a
(integer) 1
127.0.0.1:6379> LPUSH l1 b c d e f
(integer) 6
127.0.0.1:6379> LRANGE l1
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379> LRANGE l1 0 -1
1) "f"
2) "e"
3) "d"
4) "c"
5) "b"
6) "a"
127.0.0.1:6379> LPOP l1
"f"
127.0.0.1:6379> LPOP l1
"e"
127.0.0.1:6379> LPOP l1
"d"
127.0.0.1:6379> LPOP l1
"c"
127.0.0.1:6379> LPOP l1
"b"
127.0.0.1:6379> LPOP l1
"a"
127.0.0.1:6379> LPOP l1
(nil)
127.0.0.1:6379> LRANGE l1 0 -1
(empty list or set)
127.0.0.1:6379> LPUSH l1 b c d e f
(integer) 5
127.0.0.1:6379> RPOP l1
"b"
127.0.0.1:6379> RPOP l1
"c"
127.0.0.1:6379> RPOP l1
"d"
127.0.0.1:6379> RPOP l1
"e"
127.0.0.1:6379> RPOP l1
"f"
## 从左到右,从上到下  [f e d c b] 
127.0.0.1:6379> LPUSH l1 b c d e f
(integer) 5
127.0.0.1:6379> LRANGE l1 1 1
1) "e"
127.0.0.1:6379> LRANGE l1 0 -1
1) "f"
2) "e"
3) "d"
4) "c"
5) "b"
127.0.0.1:6379> LRANGE l1 -2 -1
1) "c"
2) "b"
127.0.0.1:6379> LRANGE l1 -1 -2
(empty list or set)
127.0.0.1:6379> LPUSH l1 b c d e f
(integer) 5
127.0.0.1:6379> RPOPLPUSH l1 l2
"b"
127.0.0.1:6379> LRANGE l2 0 -1
1) "b"
127.0.0.1:6379> LRANGE l1 0 -1
1) "f"
2) "e"
3) "d"
4) "c"

  

  

 

 

 

 

127.0.0.1:6379> LINDEX l1 2
"d"
127.0.0.1:6379> LLEN l1
(integer) 4
127.0.0.1:6379> RPUSH listkey c abc c ab 123 ab bj ab redis list
(integer) 10
127.0.0.1:6379> LRANGE listkey 0 -1
 1) "c"
 2) "abc"
 3) "c"
 4) "ab"
 5) "123"
 6) "ab"
 7) "bj"
 8) "ab"
 9) "redis"
10) "list"
127.0.0.1:6379> LREM listkey -1 ab
(integer) 1
127.0.0.1:6379> LRANGE listkey 0 -1
1) "c"
2) "abc"
3) "c"
4) "ab"
5) "123"
6) "ab"
7) "bj"
8) "redis"
9) "list"
127.0.0.1:6379> LREM listkey 0 ab
(integer) 2
127.0.0.1:6379> LRANGE listkey 0 -1
1) "c"
2) "abc"
3) "c"
4) "123"
5) "bj"
6) "redis"
7) "list"
127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> RPUSH listkey c abc c ab 123 ab bj ab redis list
(integer) 10
127.0.0.1:6379> help LTRIM

  LTRIM key start stop
  summary: Trim a list to the specified range
  since: 1.0.0
  group: list

127.0.0.1:6379> LTRIM listkey 0 -1
OK
127.0.0.1:6379> LRANGE listkey 0 -1
 1) "c"
 2) "abc"
 3) "c"
 4) "ab"
 5) "123"
 6) "ab"
 7) "bj"
 8) "ab"
 9) "redis"
10) "list"
127.0.0.1:6379> LTRIM listkey 0 0
OK
127.0.0.1:6379> LRANGE listkey 0 -1   ## 去除指定范围外的元素
1) "c"
127.0.0.1:6379> LPUSH l3 a b d
(integer) 3
127.0.0.1:6379> LRANGE l3 0 -1
1) "d"
2) "b"
3) "a"
127.0.0.1:6379> LINSERT l3 before b x
(integer) 4
127.0.0.1:6379> LRANGE l3 0 -1
1) "d"
2) "x"
3) "b"
4) "a"

  

 

 

 

 

windowA
127.0.0.1:6379> BLPOP l1 0
windowB
127.0.0.1:6379> BLPOP l1 0
windowC
127.0.0.1:6379> BLPOP l1 0
windowD
127.0.0.1:6379> LPUSH l1 1
windowA
127.0.0.1:6379> BLPOP l1 0  ## 阻塞中断
1) "l1"
2) "1"
(92.45s)
windowD
127.0.0.1:6379> LPUSH l1 1
windowB
127.0.0.1:6379> BLPOP l1 0  ## 阻塞中断
1) "l1"
2) "1"
127.0.0.1:6379> BLPOP l1 5  ## 5秒自动中断
(nil)
(5.93s)

  

 

 

 

 

127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> HSET mike name michael
(integer) 1
127.0.0.1:6379> HSET mike age 30
(integer) 1
127.0.0.1:6379> HMSET mike gender m phone 13552594619
OK
127.0.0.1:6379> HVALS mike
1) "michael"
2) "30"
3) "m"
4) "13552594619"
127.0.0.1:6379> HKEYS mike
1) "name"
2) "age"
3) "gender"
4) "phone"
127.0.0.1:6379> set mike:name michael
OK
127.0.0.1:6379> set mike:age 30
OK
127.0.0.1:6379> set mike:gender m
OK
127.0.0.1:6379> keys *
1) "mike:age"
2) "mike:name"
3) "mike"
4) "mike:gender"
127.0.0.1:6379> HGET mike age
"30"
127.0.0.1:6379> HGET mike name
"michael"
127.0.0.1:6379> HLEN mike
(integer) 4
127.0.0.1:6379> HGETALL mike
1) "name"
2) "michael"
3) "age"
4) "30"
5) "gender"
6) "m"
7) "phone"
8) "13552594619"
127.0.0.1:6379> HINCRBY mike age 10
(integer) 40
127.0.0.1:6379> HINCRBY mike age 10.5
(error) ERR value is not an integer or out of range
127.0.0.1:6379> HINCRBYFLOAT mike age 10.5
"50.5"

  

 

 

redis key分时点存储,到时点时去检查时间片下的数据是否过期。被动
当用户查询数据时,检查是否过期。 被动

 

 

 

 

 

 

  

127.0.0.1:6379> SADD s1 mike marie peter marry
(integer) 4
127.0.0.1:6379> SMEMBERS s1
1) "peter"
2) "marie"
3) "mike"
4) "marry"
127.0.0.1:6379> SREM s1 peter
(integer) 1
127.0.0.1:6379> SMEMBERS s1
1) "marie"
2) "mike"
3) "marry"
127.0.0.1:6379> SISMEMBER s1 peter
(integer) 0
127.0.0.1:6379> SISMEMBER s1 marry
(integer) 1

 

127.0.0.1:6379> SRANDMEMBER s1 
"marry"
127.0.0.1:6379> SRANDMEMBER s1 
"marie"
127.0.0.1:6379> SRANDMEMBER s1 1
1) "marry"
127.0.0.1:6379> SRANDMEMBER s1 2
1) "mike"
2) "marry"
127.0.0.1:6379> SRANDMEMBER s1 10
1) "mike"
2) "marie"
3) "marry"
127.0.0.1:6379> SRANDMEMBER s1 -100  ## 超过集合数量时,返回数量个。
  1) "mike"
  2) "marry"
  3) "marry"
  4) "marie"
  5) "mike"
  6) "marry"
  7) "marry"
...100

127.0.0.1:6379> SRANDMEMBER s1 -2
1) "mike"
2) "marry"
127.0.0.1:6379> SPOP s1
"mike"
127.0.0.1:6379> SPOP s1
"marie"
127.0.0.1:6379> SCARD s1
(integer) 1

  

 

  

 

 

127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> SADD number1 123 456 789
(integer) 3
127.0.0.1:6379> SADD number2 123 456 999
(integer) 3
127.0.0.1:6379> SDIFFSTORE number3 number1 number2  ## number1 去除1与2的交集
(integer) 1
127.0.0.1:6379> SMEMBERS number3
1) "789"
127.0.0.1:6379> SDIFFSTORE number4 number2 number1
(integer) 1
127.0.0.1:6379> SMEMBERS number4
1) "999"
127.0.0.1:6379> SUNION number5 number1 number2
1) "123"
2) "456"
3) "789"
4) "999"
127.0.0.1:6379> SINTER number1 number2
1) "123"
2) "456"

  

 

 

 

 

 

 

 

 

127.0.0.1:6379> ZADD fruits 3.2 香蕉
(integer) 1
127.0.0.1:6379> ZADD fruits 2.0 西瓜
(integer) 1
127.0.0.1:6379> ZADD fruits 4.0 番石榴 7.0 梨 6.8 芒果
(integer) 3
127.0.0.1:6379> ZRANGE fruits
(error) ERR wrong number of arguments for 'zrange' command
127.0.0.1:6379> ZRANGE fruits
(error) ERR wrong number of arguments for 'zrange' command
127.0.0.1:6379> ZRANGE fruits 0 -1
1) "\xe8\xa5\xbf\xe7\x93\x9c"
2) "\xe9\xa6\x99\xe8\x95\x89"
3) "\xe7\x95\xaa\xe7\x9f\xb3\xe6\xa6\xb4"
4) "\xe8\x8a\x92\xe6\x9e\x9c"
5) "\xe6\xa2\xa8"
127.0.0.1:6379> quit
[[email protected] ~]# redis-cli --raw
127.0.0.1:6379> ZRANGE fruits 0 -1
西瓜
香蕉
番石榴
芒果
梨
127.0.0.1:6379> ZRANGE fruits 0 -1 withscores
西瓜
2
香蕉
3.2000000000000002
番石榴
4
芒果
6.7999999999999998
梨
7
127.0.0.1:6379> ZREVRANGE fruits 0 -1 withscores
梨
7
芒果
6.7999999999999998
番石榴
4
香蕉
3.2000000000000002
西瓜
2
127.0.0.1:6379> ZRANGE fruits -3 -1
番石榴
芒果
梨
127.0.0.1:6379> ZRANGE fruits -3 -1 withscores
番石榴
4
芒果
6.7999999999999998
梨
7

  

127.0.0.1:6379> ZSCORE fruits 芒果
6.7999999999999998
127.0.0.1:6379> ZINCRBY fruits 1.5 芒果
8.3000000000000007
127.0.0.1:6379> ZRANGE fruits 0 -1 
西瓜
香蕉
番石榴
梨
芒果
127.0.0.1:6379> ZRANK fruits 西瓜
0
127.0.0.1:6379> ZRANK fruits 西瓜
0
127.0.0.1:6379> ZREVRANK fruits 番石榴
2
127.0.0.1:6379> ZREVRANK fruits 芒果
0
127.0.0.1:6379> ZRANGE fruits 3 4 withscores
梨
7
芒果
8.3000000000000007
127.0.0.1:6379> ZRANGEBYSCORE fruits 3 3.5 withscores
香蕉
3.2000000000000002
127.0.0.1:6379> ZRANGEBYSCORE fruits (3.2 3.5 withscores

127.0.0.1:6379> ZRANGEBYSCORE fruits -inf +inf withscores
西瓜
2
香蕉
3.2000000000000002
番石榴
4
梨
7
芒果
8.3000000000000007
127.0.0.1:6379> ZREMRANGEBYRANK fruits 0 0
1
127.0.0.1:6379> ZRANGEBYSCORE fruits -inf +inf withscores
香蕉
3.2000000000000002
番石榴
4
梨
7
芒果
8.3000000000000007
127.0.0.1:6379> ZCARD fruits ## 返回元素个数
4
127.0.0.1:6379> ZCOUNT fruits 4 7   ## 分值范围内元素的个数
2
127.0.0.1:6379> ZCOUNT fruits 5 7
1

  

 

 

  

举例
ZADD scores1 70 tom 80 peter 60 john
ZADD scores2 90 peter 60 ben
ZUNIONSTORE scores-all 2 scores1 scores2
ZUNIONSTORE scores-all1 2 scores1 scores2 AGGREGATE SUM
ZUNIONSTORE scores-all2 2 scores1 scores2 WEIGHTS 1 0.5 

 

127.0.0.1:6379> ZADD scores1 70 tom 80 peter 60 john
3
127.0.0.1:6379> ZADD scores2 90 peter 60 ben
2
127.0.0.1:6379> ZUNIONSTORE scores3 2 scores1 scores2
4
127.0.0.1:6379> ZRANGE scores3 0 -1 withscores
ben
60
john
60
tom
70
peter
170
127.0.0.1:6379> ZUNIONSTORE scores-all1 2 scores1 scores2 AGGREGATE SUM
4
127.0.0.1:6379> ZRANGE scores-all1 0 -1 withscores
ben
60
john
60
tom
70
peter
170
127.0.0.1:6379> ZUNIONSTORE scores-all2 2 scores1 scores2 WEIGHTS 1 0.5 AGGREGATE SUM
4
127.0.0.1:6379> ZRANGE scores-all2 0 -1 withscores
ben
30
john
60
tom
70
peter
125

  

 

 

 

 

 

 

 

 

 

 

 

  

 

mysql索引 btree https://www.jianshu.com/p/0371c9569736
跳跃表
https://blog.csdn.net/qpzkobe/article/details/80056807

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

AOF开启之后,RDB就不会被采纳。

 

 

 

 

 

 

数据承载的方式:
集群  分片   3台4G内存,最多承载12G数据,每一台4G,只是缺少备份
lvs    镜像全量   3台4G内存,  一模一样的机器和数据,最多承载4G

  

 

 

  

 

 

CAP原则
https://baike.baidu.com/item/CAP%E5%8E%9F%E5%88%99/5712863?fr=aladdin
无主模型

主从模型

 

sentinel
英 ['sentɪn(ə)l]  美 ['sɛntɪnl] 
n. 哨兵
vt. 守卫,放哨

 

https://www.jianshu.com/p/d9d067a8a086 paxos 论文: 分布式系统一致性的基本理论

  

 

 

 

 

官网,和java client源码
https://redis.io/clients

https://github.com/xetorthio/jedis

  

 

 

伪分布式搭建
node1上
[[email protected] ~]# mkdir redis
[[email protected] redis]# mkdir 6380
[[email protected] redis]# mkdir 6381
[[email protected] redis]# mkdir 6382
[[email protected] redis]# cd 6380/
[[email protected] 6380]# redis-server -h
[[email protected] 6380]# redis-server -port 6380   ## 默认异master身份启动
[[email protected] 6381]# pwd
/root/redis/6381
[[email protected] 6381]# redis-server --port 6381 --slaveof 127.0.0.1 6380  ## 启动slave
[[email protected] 6382]# redis-server --port 6382 --slaveof 127.0.0.1 6380

[[email protected] ~]# redis-cli -p 6380
127.0.0.1:6380> set k1 5
OK
127.0.0.1:6380> get k1
"5"
[[email protected] ~]# redis-cli -p 6381
127.0.0.1:6381> get k1
"5"
[[email protected] ~]# redis-cli -p 6382
127.0.0.1:6382> get k1
"5"
127.0.0.1:6382> set k3 dsfsd       ## slave 只能读,不能写,master才能够写入
(error) READONLY You can't write against a read only slave. 

## 当6380 ctrl+c挂掉之后。

  

127.0.0.1:6381> SLAVEOF no one ## 6381手动成为master 
127.0.0.1:6381> set k3 dfsd
OK
[[email protected] 6380]# redis-server --port 6380 --slaveof 127.0.0.1 6381  ## 启动6380称为81的slave

not connected> get k3
"dfsd"
127.0.0.1:6380> set k4 werwe
(error) READONLY You can't write against a read only slave.

## 如上手动切换比较麻烦。

  

 

哨兵,单独存在的。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

配置和启动
[[email protected] src]# cp redis-sentinel /opt/sxt/redis/bin
[[email protected] src]# ls /opt/sxt/redis/bin/
redis-benchmark  redis-check-aof  redis-check-dump  redis-cli  redis-sentinel  redis-server
[[email protected] src]# cd
[[email protected] ~]# mkdir sent
[[email protected] ~]# cd sent/
[[email protected] sent]# vi s1.conf
[[email protected] sent]# cp s1.conf s2.conf
[[email protected] sent]# cp s1.conf s3.conf
[[email protected] sent]# vi s2.conf 
[[email protected] sent]# vi s3.conf 
[[email protected] sent]# cat s*.conf
port 23680
sentinel monitor sxt 127.0.0.1 6380 2
port 23681
sentinel monitor sxt 127.0.0.1 6380 2
port 23682
sentinel monitor sxt 127.0.0.1 6380 2


[[email protected] 6380]# redis-server --port 6380
[[email protected] 6381]# redis-server --port 6381 --slaveof 127.0.0.1 6380
[[email protected] 6382]# redis-server --port 6382 --slaveof 127.0.0.1 6380

[[email protected] sent]# redis-sentinel s1.conf   ## ~/sent ## 启动哨兵
[[email protected] sent]# redis-sentinel s2.conf 
[[email protected] sent]# redis-sentinel s3.conf  
## ctrl+c结束6380服务。
## 稍等一会,哨兵自动选举,
s2,s3下的日志
[10204] 14 Sep 14:33:28.946 # +switch-master sxt 127.0.0.1 6380 127.0.0.1 6382
## 此时6382称为主

## 重新启动6380,以master身份。
[[email protected] 6380]# redis-server --port 6380  ## 此时6382还是master,6380是slave.

  

Redis3.0提供了集群解决方案

  

 

 

 

 

 

slot
英 [slɒt]  美 [slɑt] 
n. 位置;狭槽;水沟;硬币投币口
vt. 跟踪;开槽于

  

 

 

 

 

3.0的节点既是节点又是哨兵的功能。

redis集群环境搭建
[[email protected] redis-cluster]# pwd
/root/software/redis-cluster
[[email protected] redis-cluster]# ll
total 1432
-rw-r--r--. 1 root root 1364993 Jun  4  2018 redis-3.0.4.tar.gz
-rw-r--r--. 1 root root   92160 Jun  4  2018 redis-3.3.0.gem   ## 依赖ruby环境
drwxr-xr-x. 8 root root    4096 Sep 14 15:25 redis-test
[[email protected] redis-cluster]# tar xf redis-3.0.4.tar.gz 

[[email protected] redis-cluster]# cd redis-3.0.4
[[email protected] redis-3.0.4]# whereis gcc tcl
[[email protected] redis-3.0.4]# make && make PREFIX=/opt/sxt/redis/  install
[[email protected] redis-3.0.4]# cd /opt/sxt/redis/
[[email protected] redis]# ls
bin
[[email protected] redis]# cd bin/
[[email protected] bin]# ll
total 8520
-rwxr-xr-x. 1 root root 2077192 Sep 14 15:29 redis-benchmark
-rwxr-xr-x. 1 root root   25104 Sep 14 15:29 redis-check-aof
-rwxr-xr-x. 1 root root   55936 Sep 14 15:29 redis-check-dump
-rwxr-xr-x. 1 root root 2201240 Sep 14 15:29 redis-cli
lrwxrwxrwx. 1 root root      12 Sep 14 15:29 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 4352016 Sep 14 15:29 redis-server
[[email protected] bin]# cd ~/software/redis-cluster/
[[email protected] redis-cluster]# ls
redis-3.0.4  redis-3.0.4.tar.gz  redis-3.3.0.gem  redis-test
[[email protected] redis-cluster]# yum -y install ruby rubygems   ## 安装ruby环境
[[email protected] redis-cluster]# ls
redis-3.0.4  redis-3.0.4.tar.gz  redis-3.3.0.gem  redis-test
[[email protected] redis-cluster]# gem install --local redis-3.3.0.gem  ## 本地安装gem(redis依赖ruby组件)
[[email protected] redis-cluster]# cd redis-test/
[[email protected] redis-test]# ls
7000  7001  7002  7003  7004  7005
[[email protected] redis-test]# cd 7000/
-rw-r--r--. 1 root root 30 Jun  4  2018 redis.conf
[[email protected] 7000]# cat redis.conf     ## 每个文件加下的配置对应700x
cluster-enabled yes
port 7000
###通过单节点演示6个实例;node1 打开6个终端 进入各个700x文件夹
[[email protected] 7000]# pwd
/root/software/redis-cluster/redis-test/7000
## 6个终端都启动实例 执行 redis-server r*
[[email protected] 7000]# redis-server r*    ## 相当于6个无主的实例
##指定集群  前三个是主,后三个是备
[[email protected] src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 \
> 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
[[email protected] src]# redis-cli -p 7000    ## 错误 应该-c
127.0.0.1:7000> set k1 5
(error) MOVED 12706 127.0.0.1:7002
127.0.0.1:7000>     
[[email protected] src]# redis-cli -p 7000 -c     ## -c以集群方式进入
127.0.0.1:7000> set k1 5 
-> Redirected to slot [12706] located at 127.0.0.1:7002  ## 槽位和数据
OK
127.0.0.1:7002> set k2 10
-> Redirected to slot [449] located at 127.0.0.1:7000
OK
127.0.0.1:7000> get k2
"10"
127.0.0.1:7000> get k1
-> Redirected to slot [12706] located at 127.0.0.1:7002
"5"
[[email protected] ~]# redis-cli -p 7000 -c
127.0.0.1:7000> get k1
-> Redirected to slot [12706] located at 127.0.0.1:7002
"5"
127.0.0.1:7002> exit
[[email protected] ~]# redis-cli -p 7003 -c
127.0.0.1:7003> get k1
-> Redirected to slot [12706] located at 127.0.0.1:7002
"5"
127.0.0.1:7002> exit
## 将7000 ctrl+c结束, 此时,7003备接替。get k1时,不会重定向
[[email protected] ~]# redis-cli -p 7003 -c
127.0.0.1:7003> get k2
"10"

  

转载于:https://www.cnblogs.com/xhzd/p/11515602.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_30501857/article/details/102244858

智能推荐

java的浅克隆和深克隆,你了解吗_西青程序猿的博客-程序员宅基地

文章目录浅克隆深克隆应用场景克隆这个词最早出现在生物学,在java中引入这个概念是为了在特定的场景避免大量的get,set方法,java的克隆有两种方式: 深克隆和浅克隆,这里分析一下这两种克隆的方式实现方式和使用场景;浅克隆浅克隆的实现方式是实现Cloneable接口并重写Object类中的clone()方法;现在随便写一个类package com.lbh.test;public class Person implements Cloneable{ private String name

NVM安装和使用_m0_46815188的博客-程序员宅基地

1、nvm是什么 nvm(node.js version management)是一个nodejs的版本管理工具。2、nvm的下载下载地址GitHubnvm-noinstall.zip:绿色免安装版,但使用时需进行配置。 nvm-setup.zip:安装版,推荐使用。 Source code(zip):zip压缩的源码。 Sourc code(tar.gz):tar.gz的源码,一般用于*nix系统。3、安装卸载现有node请注意,在安装适用于Windows的N...

MFC制作Windows画图程序(一)_理理我的博客-程序员宅基地_mfc绘图程序

文章目录一、开发环境二、程序功能三、功能实现1 铅笔2 线段3 特殊形状4 放大镜5 橡皮擦6 添加文本一、开发环境Visual Studio,MFC,单文档二、程序功能1.实现设置画笔属性,选择画笔粗细的功能2.实现颜色选择功能3.实现油漆桶填充功能4.实现画直线和铅笔画功能5.实现特殊形状绘制(矩形、三角形、圆、椭圆)功能6.实现局部放大功能7.实现插入位图功能8.实现保存所绘制图形的功能9.实现橡皮擦功能10.实现添加文本功能三、功能实现我们首先创建了单文档的应用程序M.

linux apr文件解压失败,编译httpd2.4.10踩坑记_一笑奈何666的博客-程序员宅基地

作者【Jev Tse】【版权所有】 系统环境:centos6.8【本文概览】零、httpd2.4.10编译总结1、基础环境2、依赖包组1、第一个坑:APR not found (ARP-Ulit not fount)2、第二个坑:pcre-config for libpcre not found.3、第三个坑:g++: command not found4、第四个坑:make[1]: *** [...

qt 导入mysql数据源_如何用Qt连接数据库并导入文件?_带虾条酱的博客-程序员宅基地

可以使用QT自带的数据库完成操作,以下是参考代码:QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); //添加数据库db.setDatabaseName("note.db"); //创建一个note.db的文件存储数据db.open(); //开启数据库query = QSqlQuery(db); //获得访问数据库的...

关于Qt5.7生成应用程序图标的操作_LVsler的博客-程序员宅基地

1. 新建文件icon.rc,内容为: IDI_ICON1 ICON DISCARDABLE “main.ico” 2. 将icon.rc和main.icon 文件放到程序目录下,和.pro文件放在一起。 3. 在 pro工程文件最后添加一句: RC_FILE = icon.rc 4.qmake项目,再重新构建项目(将ico和rc文件拷贝到release目录中)5.重新

随便推点

MySQL:事务:以转账为例_爱学习的梵高先生的博客-程序员宅基地_mysql转账事务

1.创建数据表account2.插入数据3.模拟转账操作-- 模拟转账SET autocommit=0; -- 关闭自动提交START TRANSACTION -- 开启一个事务UPDATE account SET money=money-500 WHERE NAME='张三'UPDATE account SET money=money+500 WHERE NAME='李四'COMMIT; -- 提交事务,被持久化ROLLBACK; -- 回滚SET autocommit=1;

haproxy_ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ0的博客-程序员宅基地

haproxyHAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制

pika安装_怎么就重名了的博客-程序员宅基地_pika 安装

pika是360开源的一个使用硬盘作为存储的redis。安装$ wget https://github.com/OpenAtomFoundation/pika/releases/download/v3.3.6/pika-linux-x86_64-v3.3.6.tar.bz2$ tar -xvf pika-linux-x86_64-v3.3.6.tar.bz2$ mv output /usr/local/pika #做软连接$ ln -s /usr/local/pika/bin/pika /u

MySQL和hive建表区别,Hive中創建表(hive的使用和MySQL的使用很相似)_雅儿zZ的博客-程序员宅基地

CREATE TABLE語句(不區分大小寫)Create Table是用於在Hive中創建表的語句,語法和示例如下:語法:CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.] table_name[(col_name data_type [COMMENT col_comment], ...)][COMMENT table_co...

iOS App 上线发布流程_OneCrazyDog的博客-程序员宅基地

先登陆自己的开发者账号(自己提前注册好 iOS 开发者账号,这里假设你已经拥有了一个 iOS 开发者账号),进入这个页面:https://developer.apple.com/account/overview.action由于我提交上架的是iOS Apps,因此进入最左边这一栏:一开始,看着这几个“Certificates、Identifiers、Devices、Prov

【CTF】异或运算符的利用_吃_早餐的博客-程序员宅基地_ctf 异或

异或运算符简介mysql里异或运算符:^ 或者 xor;^xor位异或 运算逻辑 运算如 1^1=0、 0^0=0、 0^1=1、1^1^1=0 、1^1^0=0、 1^2=3、 1^2=3数字n xor 0会输出n;其他情况输出其他所有数据异或运算规则:如果两个相应bit位相同,则结果为0,否则为1。注释方法:‘ xxxx #、’ xxxx--+异或运算:可以取每个字符的ASCII,转成二进制再做运算,为了方便,copy一个脚本,通过查ASCII表的符合和

推荐文章

热门文章

相关标签