redis 基礎學習總結

背景:由於項目用到了redis,加上以前一直據說了redis,但一直沒有用過,正好項目如今要用到了,抽時間簡單學習了下,作個記錄總結下。前端

一 、Redis簡介java

介紹Redis以前,先了解下NoSQL (Not noly SQL)不只僅是SQLmysql

屬於非關係型數據庫;Redis就屬於非關係型數據庫c++

傳統的Mysql ,oracle ,sql server 等 都是關係型數據庫git

 

爲何須要NoSQL,主要應對如下問題,傳統關係型數據庫力不從心github

High performance -高併發讀寫redis

Huge Storage-海量數據的高效率存儲和訪問sql

High Scalablility && High Availability 高可擴展性和高可用性shell

 

NoSQL 產品 MongoDB Redis等等  Redis目前主流數據庫

 

NoSQL的特色

易擴展

靈活的數據模型

大數據量,高性能

高可用

 

高性能鍵值對數據庫,支持的鍵值數據類型

字符串類型

列表類型

有序集合類型

散列類型

集合類型

 

Redis的應用場景

緩存

任務隊列

網站訪問統計

數據過時處理

應用排行榜

分佈式集羣架構中的session分離

 

2、Redis安裝

 

Redis從一開始就只支持Linux,後面雖然有團隊搞出Window版本,可是我仍是建議大夥安裝到Linux中。

 

 

 

準備工做 VMware 以及Xshell 

 

https://redis.io/

 

 

 

redis官方安裝教程,可是比較簡單,不適合生產環境的安裝部署;

 

咱們這邊詳細點,

 

 

 

第一步:安裝gcc

 

gcc編譯c的,由於redis是c編寫的,因此咱們先安裝下gcc

 

yum install gcc-c++

 

 

 

第二步:wget方式 下載redis壓縮包,並解壓,以及編譯

 

下載

 

wget http://download.redis.io/releases/redis-3.2.9.tar.gz

 

解壓

 

tar -zxvf redis-3.2.9.tar.gz

 

 

 

[root@localhost ~]# ll

 

總用量 1548

 

-rw-------. 1 root root    1261 6月  27 19:54 anaconda-ks.cfg

 

-rw-r--r--. 1 root root   25680 4月  27 18:45 mysql57-community-release-el7-11.noarch.rpm

 

drwxrwxr-x. 6 root root    4096 5月  17 23:39 redis-3.2.9

 

-rw-r--r--. 1 root root 1547695 5月  17 23:40 redis-3.2.9.tar.gz

 

[root@localhost ~]# 

 

這樣/root/目錄下就有redis了

 

 

 

編譯:

 

cd redis-3.2.9

 

進入目錄,

 

make

 

編譯

 

 

 

 

 

第三步:安裝redis

 

make PREFIX=/usr/local/redis install

 

安裝到/usr/local/redis/

 

QQ鎴浘20170702164458.jpg

 

 

 

已經有redis 

 

[root@localhost local]# cd redis/

 

[root@localhost redis]# ll

 

總用量 0

 

drwxr-xr-x. 2 root root 134 7月   2 16:44 bin

 

[root@localhost redis]# 

 

 

 

redis裏有個bin

 

[root@localhost redis]# cd bin/

 

[root@localhost bin]# ll

 

總用量 15060

 

-rwxr-xr-x. 1 root root 2431832 7月   2 16:44 redis-benchmark

 

-rwxr-xr-x. 1 root root   25168 7月   2 16:44 redis-check-aof

 

-rwxr-xr-x. 1 root root 5181840 7月   2 16:44 redis-check-rdb

 

-rwxr-xr-x. 1 root root 2584848 7月   2 16:44 redis-cli

 

lrwxrwxrwx. 1 root root      12 7月   2 16:44 redis-sentinel -> redis-server

 

-rwxr-xr-x. 1 root root 5181840 7月   2 16:44 redis-server

 

[root@localhost bin]# 

 

bin裏是一些工具

 

 

 

cd回到root,咱們須要把一個配置文件 複製到 redis下 後臺啓動用到

 

 

 

QQ鎴浘20170702164811.jpg

 

 

 

[root@localhost redis-3.2.9]# cp redis.conf /usr/local/redis/

 

[root@localhost redis-3.2.9]# ll /usr/local/redis/

 

總用量 48

 

drwxr-xr-x. 2 root root   134 7月   2 16:44 bin

 

-rw-r--r--. 1 root root 46695 7月   2 16:49 redis.conf

 

[root@localhost redis-3.2.9]# 

 

 

 

第四步:啓動和關閉redis服務

 

啓動redis就是執行redis裏的bin裏的redis-server命令

 

進入redis目錄 執行

 

[root@localhost redis-3.2.9]# cd /usr/local/redis/

 

[root@localhost redis]# bin/redis-server

 

 

 

QQ鎴浘20170702180602.jpg

 

 

 

出現這種圖標,說明啓動成功;

 

 

 

可是 ,這種啓動是前端或者前臺啓動,假如退出 程序就終止或者退出了。

 

因此這種服務程序,必須後端運行;

 

 

 

咱們經過修改配置文件redis.conf 

 

 

 

操做,

 

咱們ctrl+c 退出當前程序;

 

vi打開redis.conf  vi /usr/local/redis/redis.conf 

 

找到

 

QQ鎴浘20170702202245.jpg

 

 

 

把no改爲yes

 

esc退出 !wq保存;

 

 

 

而後進入redis目錄,而後加載配置文件運行;

 

[root@localhost ~]# cd /usr/local/redis/

 

[root@localhost redis]# ./bin/redis-server ./redis.conf 

 

 

 

咱們經過ps -ef | grep -i redis命令來搜索redis服務

 

[root@localhost redis]# ps -ef | grep -i redis

 

root       8692      1  0 16:52 ?        00:00:19 bin/redis-server *:6379

 

root       8954   8930  0 20:30 pts/0    00:00:00 grep --color=auto -i redi

 

[root@localhost redis]# 

 

 

 

咱們經過shutdown命令來中止redis服務的運行

 

[root@localhost redis]# ./bin/redis-cli shutdown

 

[root@localhost redis]# ps -ef | grep -i redis

 

root       8959   8930  0 20:35 pts/0    00:00:00 grep --color=auto -i redi

 

[root@localhost redis]# 

 

 

 

 

 

第五步:redis基本使用

 

[root@localhost redis]# ./bin/redis-cli

 

127.0.0.1:6379> 

 

進入客戶端

 

咱們存儲 name:java1234

 

key:value格式

 

127.0.0.1:6379> set name java1234

 

OK

 

127.0.0.1:6379> get name

 

"java1234"

 

經過set設置,經過get獲取

 

 

 

127.0.0.1:6379> keys *

 

1) "name"

 

127.0.0.1:6379> del name

 

(integer) 1

 

127.0.0.1:6379> keys *

 

(empty list or set)

 

127.0.0.1:6379> 

 

 

 

經過del刪除key,keys  * 顯示全部keys

 

3、Jedis簡介

 

實際開發中,咱們須要用Redis的鏈接工具鏈接Redis而後操做Redis,

 

對於主流語言,Redis都提供了對應的客戶端;

 

 

 

https://redis.io/clients

 

 

 

QQ鎴浘20170708104553.jpg

 

 

 

https://redis.io/clients#java

 

 

 

QQ鎴浘20170708104732.jpg

 

 

 

提供了不少客戶端 官方推薦的是Jedis  託管地址:https://github.com/xetorthio/jedis

 

4、Jedis鏈接 HelloWorld實現

建一個Maven項目,

pom里加下jedis依賴,

1
2
3
4
5
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.java1234.redis;
 
import redis.clients.jedis.Jedis;
 
/**
 * 測試類
 * @author user
 *
 */
public class JedisTest {
 
    public static void main(String[] args) {
        Jedis jedis=new Jedis("192.168.1.107",6379); // 建立客戶端 設置IP和端口
        jedis.set("name""java知識分享網"); // 設置值
        String value=jedis.get("name"); // 獲取值
        System.out.println(value);
        jedis.close(); // 釋放鏈接資源
    }
}

測試代碼,

運行 報錯了

QQ鎴浘20170708163732.jpg

鏈接超時,

 

咱們配置下防火牆 開一個6379端口權限

firewall-cmd --zone=public --add-port=6379/tcp --permanent

firewall-cmd --reload

 

繼續運行 仍是報錯 鏈接超時 錯誤;

 

咱們配置下 redis配置文件

[root@localhost redis]# vi /usr/local/redis/redis.conf

 

QQ鎴浘20170708164341.jpg

 

這裏綁定了本機,咱們把這個備註掉;

# bind 127.0.0.1

 

配置完後 

[root@localhost redis]# ./bin/redis-cli shutdown

[root@localhost redis]# ./bin/redis-server ./redis.conf

要重啓下redis服務;

 

繼續運行 又報錯了

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

at redis.clients.jedis.Protocol.processError(Protocol.java:127)

at redis.clients.jedis.Protocol.process(Protocol.java:161)

at redis.clients.jedis.Protocol.read(Protocol.java:215)

at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340)

at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239)

at redis.clients.jedis.Jedis.set(Jedis.java:121)

at com.java1234.redis.JedisTest.main(JedisTest.java:14)

 

這個是由於遠程鏈接redis redis自我保護 拒絕訪問;

 

有兩種方法 解決

第一種 直接去掉自我保護功能(不推薦)

[root@localhost redis]# vi /usr/local/redis/redis.conf

進入配置

找到 protected-mode yes

QQ鎴浘20170708164718.jpg

 

改爲 no便可

 

編輯後 重啓redis服務,而後運行 ,結果出來了

QQ鎴浘20170708164855.jpg

 

第二種 設置redis鏈接密碼

進入客戶端

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> config set requirepass 123456

設置密碼 123456

 

127.0.0.1:6379> quit

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> auth 123456

OK

 

說明設置成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.java1234.redis;
 
import redis.clients.jedis.Jedis;
 
/**
 * 測試類
 * @author user
 *
 */
public class JedisTest {
 
    public static void main(String[] args) {
        Jedis jedis=new Jedis("192.168.1.107",6379); // 建立客戶端 設置IP和端口
        jedis.auth("123456"); // 設置密碼
        jedis.set("name""java知識分享網"); // 設置值
        String value=jedis.get("name"); // 獲取值
        System.out.println(value);
        jedis.close(); // 釋放鏈接資源
    }
}

QQ鎴浘20170708224324.jpg

 

這樣就OK了

鏈接池:

package com.java1234.redis;
 
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
 
/**
 * 測試類
 * @author user
 *
 */
public class JedisTest {
 
    public static void main(String[] args) {
        JedisPoolConfig config=new JedisPoolConfig(); // 鏈接池的配置對象
        config.setMaxTotal(100); // 設置最大鏈接數
        config.setMaxIdle(10); // 設置最大空閒鏈接數
         
        JedisPool jedisPool=new JedisPool(config,"192.168.1.107",6379);
         
        Jedis jedis=null;
        try{
            jedis=jedisPool.getResource(); // 獲取鏈接
            jedis.auth("123456"); // 設置密碼
            jedis.set("name", "java知識分享網"); // 設置值
            String value=jedis.get("name"); // 獲取值
            System.out.println(value);
             
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(jedis!=null){
                jedis.close();
            }
            if(jedisPool!=null){
                jedisPool.close();
            }
        }
    }
}

 

 

 

5、Redis的數據結構之字符串

五種數據類型:

字符串(String)

字符串列表(list)

有序字符串集合(sorted set)

哈希(hash)

字符串集合(set)

 

Key定義的注意點:

不要過長,

不要太短,

統一的命名規範

 

存儲String

二進制安全的,存入和獲取的數據相同

Value最多能夠容納的數據長度是512M

 

存儲String經常使用命令

賦值

取值

刪除

數值增減

擴展命令

 

[root@localhost redis]# ./bin/redis-cli

賦值

127.0.0.1:6379> set name java1234

OK

 

取值

127.0.0.1:6379> get name

"java1234"

 

獲取並設置值

127.0.0.1:6379> getset name open1111

"java1234"

127.0.0.1:6379> get name

"open1111"

 

刪除

127.0.0.1:6379> del name

(integer) 1

127.0.0.1:6379> get name

(nil)

 

incr 自增 

假如沒有定義 則默認0

假如非數值類型 則報錯

127.0.0.1:6379> incr n

(integer) 1

127.0.0.1:6379> get n

"1"

 

127.0.0.1:6379> set nn 2

OK

127.0.0.1:6379> incr nn

(integer) 3

127.0.0.1:6379> get nn

"3"

127.0.0.1:6379> 

 

127.0.0.1:6379> set n2 java1234

OK

127.0.0.1:6379> incr n2

(error) ERR value is not an integer or out of range

127.0.0.1:6379> 

 

自減 decr 同上

127.0.0.1:6379> decr nn

(integer) 2

127.0.0.1:6379> decr n3

(integer) -1

 

擴展 

incrby 指定增量值

127.0.0.1:6379> incrby n3 8

(integer) 7

 

decrby 指定減量值

127.0.0.1:6379> decrby n3 5

(integer) 2

 

append 追加字符串

假如沒定義 直接賦值

127.0.0.1:6379> append s1 jj

(integer) 2

127.0.0.1:6379> get s1

"jj"

127.0.0.1:6379> append s1 bb

(integer) 4

127.0.0.1:6379> get s1

"jjbb"

 

6、Redis的數據結構之哈希

存儲Hash

 String key和String Value的Map容器

 

每個Hash能夠存儲4294967295個鍵值對

 

存儲Hash經常使用命令:

賦值  

取值

刪除

增長數字

判斷字段是否存在

獲取hash屬性個數

獲取hash全部屬性名稱

 

定義h1 以及username和password字段 值分別是java1234 123456

 

hget單個字段設置

 

127.0.0.1:6379> hset h1 username java1234

(integer) 1

127.0.0.1:6379> hset h1 password 123456

(integer) 1

127.0.0.1:6379> hget h1 username

"java1234"

127.0.0.1:6379> hget h1 password

"123456"

 

hmset 多個字段一塊兒設置

127.0.0.1:6379> hmset h2 username open1111 password 23456

OK

127.0.0.1:6379> hmget h2 username

1) "open1111"

127.0.0.1:6379> hmget h2 password

1) "23456"

127.0.0.1:6379> hmget h2 username password

1) "open1111"

2) "23456"

 

127.0.0.1:6379> hgetall h2

1) "username"

2) "open1111"

3) "password"

4) "23456"

 

hdel刪除屬性

能夠一次刪除一個或者多個

127.0.0.1:6379> hdel h2 username password

(integer) 2

127.0.0.1:6379> hgetall h2

(empty list or set)

 

hincrby增長數字

127.0.0.1:6379> hset h1 age 20

(integer) 1

127.0.0.1:6379> hincrby h1 age 5

(integer) 25

 

hexists判斷字段是否存在 1表示存在 0表示不存在

127.0.0.1:6379> hexists h1 age

(integer) 1

127.0.0.1:6379> hexists h1 age2

(integer) 0

 

hlen獲取hash屬性個數

127.0.0.1:6379> hlen h1

(integer) 3

127.0.0.1:6379> hlen h2

(integer) 0

 

hkeys獲取全部屬性名稱

127.0.0.1:6379> hkeys h1

1) "username"

2) "password"

3) "age"

127.0.0.1:6379> hkeys h2

(empty list or set)

 

hvals獲取全部屬性值

127.0.0.1:6379> hvals h1

1) "java1234"

2) "123456"

3) "25"

127.0.0.1:6379> hvals h2

(empty list or set)

127.0.0.1:6379> 

 

7、Redis的數據結構之List

 

存儲list:

 

ArrayList使用數組方式

 

LinkedList使用雙向連接方式

 

 

 

雙向連接表中增長數據

 

雙向連接表中刪除數據

 

 

 

存儲list經常使用命令

 

兩端添加

 

兩端彈出

 

擴展命令

 

 

 

lpush 方式添加

 

從左邊開始添加

 

127.0.0.1:6379> lpush l1 a b c d

 

(integer) 4

 

127.0.0.1:6379> lpush l1 1 2 3 4

 

(integer) 8

 

 

 

lrange 獲取指定方位的集合元素

 

 

 

從第1個開始 到倒數第一個 也就是最後一個 也就是 全部數據

 

127.0.0.1:6379> lrange l1 0 -1

 

1) "4"

 

2) "3"

 

3) "2"

 

4) "1"

 

5) "d"

 

6) "c"

 

7) "b"

 

8) "a"

 

 

 

獲取從第1個到第6個集合元素

 

127.0.0.1:6379> lrange l1 0 6

 

1) "4"

 

2) "3"

 

3) "2"

 

4) "1"

 

5) "d"

 

6) "c"

 

7) "b"

 

 

 

rpush 從右端開始添加(通常人比較習慣這種方式)

 

127.0.0.1:6379> rpush l2 a b c d

 

(integer) 4

 

127.0.0.1:6379> rpush l2 1 2 3 4

 

(integer) 8

 

127.0.0.1:6379> lrange l2 0 -1

 

1) "a"

 

2) "b"

 

3) "c"

 

4) "d"

 

5) "1"

 

6) "2"

 

7) "3"

 

8) "4"

 

127.0.0.1:6379> lrange l2 0 6

 

1) "a"

 

2) "b"

 

3) "c"

 

4) "d"

 

5) "1"

 

6) "2"

 

7) "3"

 

 

 

lpop 左側彈出集合元素

 

rpop 右側彈出集合元素

 

127.0.0.1:6379> lrange l2 0 -1

 

1) "b"

 

2) "c"

 

3) "d"

 

4) "1"

 

5) "2"

 

6) "3"

 

7) "4"

 

127.0.0.1:6379> rpop l2

 

"4"

 

127.0.0.1:6379> lrange l2 0 -1

 

1) "b"

 

2) "c"

 

3) "d"

 

4) "1"

 

5) "2"

 

6) "3"

 

 

 

llen查看元素個數

 

127.0.0.1:6379> llen l2

 

(integer) 6

 

 

 

lpushx 集合頭部插入元素

 

127.0.0.1:6379> lpushx l2 xx

 

(integer) 7

 

127.0.0.1:6379> lrange l2 0 -1

 

1) "xx"

 

2) "b"

 

3) "c"

 

4) "d"

 

5) "1"

 

6) "2"

 

7) "3"

 

 

 

 

 

 

 

rpushx 集合尾部插入元素

 

127.0.0.1:6379> rpushx l2 yy

 

(integer) 8

 

127.0.0.1:6379> lrange l2 0 -1

 

1) "xx"

 

2) "b"

 

3) "c"

 

4) "d"

 

5) "1"

 

6) "2"

 

7) "3"

 

8) "yy"

 

 

 

lpush集合頭部插入多個元素

 

127.0.0.1:6379> lpush l2 a1 a2 

 

(integer) 10

 

127.0.0.1:6379> lrange l2 0 -1

 

 1) "a2"

 

 2) "a1"

 

 3) "xx"

 

 4) "b"

 

 5) "c"

 

 6) "d"

 

 7) "1"

 

 8) "2"

 

 9) "3"

 

10) "yy"

 

 

 

127.0.0.1:6379> rpush l2 a3 a4

 

(integer) 12

 

127.0.0.1:6379> lrange l2 0 -1

 

 1) "a2"

 

 2) "a1"

 

 3) "xx"

 

 4) "b"

 

 5) "c"

 

 6) "d"

 

 7) "1"

 

 8) "2"

 

 9) "3"

 

10) "yy"

 

11) "a3"

 

12) "a4"

 

 

 

lrem 從指定方向刪除指定個數的指定元素

 

先加點數據搞個新集合l3

 

127.0.0.1:6379> lpush l3 1 3 2 3 2 1 2 1 3

 

(integer) 9

 

127.0.0.1:6379> lrange l3 0 -1

 

1) "3"

 

2) "1"

 

3) "2"

 

4) "1"

 

5) "2"

 

6) "3"

 

7) "2"

 

8) "3"

 

9) "1"

 

 

 

從左邊開始刪除2個1

 

127.0.0.1:6379> lrem l3 2 1

 

(integer) 2

 

127.0.0.1:6379> lrange l3 0 -1

 

1) "3"

 

2) "2"

 

3) "2"

 

4) "3"

 

5) "2"

 

6) "3"

 

7) "1"

 

 

 

從右邊開始刪除2個3

 

127.0.0.1:6379> lrem l3 -2 3

 

(integer) 2

 

127.0.0.1:6379> lrange l3 0 -1

 

1) "3"

 

2) "2"

 

3) "2"

 

4) "2"

 

5) "1"

 

 

 

刪除全部2

 

127.0.0.1:6379> lrem l3 0 2

 

(integer) 3

 

127.0.0.1:6379> lrange l3 0 -1

 

1) "3"

 

2) "1"

 

 

 

lset 設置集合指定索引的值

 

127.0.0.1:6379> lrange l1 0 -1

 

1) "4"

 

2) "3"

 

3) "2"

 

4) "1"

 

5) "d"

 

6) "c"

 

7) "b"

 

8) "a"

 

 

 

索引從0開始

 

127.0.0.1:6379> lset l1 3 xxxx

 

OK

 

127.0.0.1:6379> lrange l1 0 -1

 

1) "4"

 

2) "3"

 

3) "2"

 

4) "xxxx"

 

5) "d"

 

6) "c"

 

7) "b"

 

8) "a"

 

 

 

linsert 在集合裏插入指定元素

 

在xxxx元素以前插入aa

 

127.0.0.1:6379> linsert l1 before xxxx aa

 

(integer) 9

 

127.0.0.1:6379> lrange l1 0 -1

 

1) "4"

 

2) "3"

 

3) "2"

 

4) "aa"

 

5) "xxxx"

 

6) "d"

 

7) "c"

 

8) "b"

 

9) "a"

 

 

 

在xxxx元素以後插入bb

 

127.0.0.1:6379> linsert l1 after xxxx bb

 

(integer) 10

 

127.0.0.1:6379> lrange l1 0 -1

 

 1) "4"

 

 2) "3"

 

 3) "2"

 

 4) "aa"

 

 5) "xxxx"

 

 6) "bb"

 

 7) "d"

 

 8) "c"

 

 9) "b"

 

10) "a"

 

 

 

rpoplpush 把A集合尾部元素彈出並插入到B集合頭部

 

127.0.0.1:6379> rpush l4 a b c

 

(integer) 3

 

127.0.0.1:6379> rpush l5 1 2 3

 

(integer) 3

 

127.0.0.1:6379> lrange l4 0 -1

 

1) "a"

 

2) "b"

 

3) "c"

 

127.0.0.1:6379> lrange l5 0 -1

 

1) "1"

 

2) "2"

 

3) "3"

 

127.0.0.1:6379> rpoplpush l4 l5

 

"c"

 

127.0.0.1:6379> lrange l4 0 -1

 

1) "a"

 

2) "b"

 

127.0.0.1:6379> lrange l5 0 -1

 

1) "c"

 

2) "1"

 

3) "2"

 

4) "3"

 

8、Redis的數據結構之Set

 

存儲Set

 

 和List類型不一樣的是,Set集合中不容許出現重複的元素

 

 Set可包含的最大元素數量是4294967295

 

存儲set經常使用命令:

 

添加/刪除元素

 

獲取集合中的元素

 

集合中的差集運算

 

集合中的交集運算

 

集合中的並集元算

 

擴展命令

 

 

 

sadd key member [member ...]

 

添加set元素

 

127.0.0.1:6379> sadd set1  a b c

 

(integer) 3

 

添加三個元素

 

smembers key

 

查看指定key集合元素

 

127.0.0.1:6379> smembers set1

 

1) "c"

 

2) "b"

 

3) "a"

 

 

 

127.0.0.1:6379> sadd set1 a d e

 

(integer) 2

 

127.0.0.1:6379> smembers set1

 

1) "c"

 

2) "d"

 

3) "b"

 

4) "a"

 

5) "e"

 

繼續添加元素 發現 重複元素再也不添加

 

 

 

srem key member [member ...]

 

刪除元素

 

127.0.0.1:6379> srem set1 a d

 

(integer) 2

 

127.0.0.1:6379> smembers set1

 

1) "b"

 

2) "c"

 

3) "e"

 

刪除兩個元素

 

 

 

sismember key member

 

判斷某個元素是否存在 返回1 表示存在 返回0表示不存在

 

127.0.0.1:6379> sismember set1 a

 

(integer) 0

 

127.0.0.1:6379> sismember set1 b

 

(integer) 1

 

 

 

sdiff計算差集

 

127.0.0.1:6379> sadd set2 a b c

 

(integer) 3

 

127.0.0.1:6379> sadd set3 b c d e

 

(integer) 4

 

127.0.0.1:6379> sdiff set2 set3

 

1) "a"

 

127.0.0.1:6379> sdiff set3 set2

 

1) "d"

 

2) "e"

 

咱們發現 集合的順序不一樣 結果不同 根據前者參考

 

 

 

sinter計算交集

 

127.0.0.1:6379> sinter set2 set3

 

1) "c"

 

2) "b"

 

 

 

sunion計算並集

 

127.0.0.1:6379> sunion set2 set3

 

1) "e"

 

2) "a"

 

3) "b"

 

4) "c"

 

5) "d"

 

 

 

scard計算元素總數

 

127.0.0.1:6379> smembers set1

 

1) "b"

 

2) "c"

 

3) "e"

 

127.0.0.1:6379> scard set1

 

(integer) 3

 

 

 

srandmember 隨機取一個元素

 

127.0.0.1:6379> srandmember set1

 

"c"

 

127.0.0.1:6379> srandmember set1

 

"e"

 

 

 

sdiffstore 把差集結果存儲到新集合中

 

127.0.0.1:6379> smembers set2

 

1) "c"

 

2) "b"

 

3) "a"

 

127.0.0.1:6379> smembers set3

 

1) "c"

 

2) "e"

 

3) "d"

 

4) "b"

 

127.0.0.1:6379> sdiffstore r1 set2 set3

 

(integer) 1

 

127.0.0.1:6379> smembers r1

 

1) "a"

 

 

 

sinterstore 把交集結果存儲到新集合中

 

127.0.0.1:6379> sinterstore r2 set2 set3

 

(integer) 2

 

127.0.0.1:6379> smembers r2

 

1) "c"

 

2) "b"

 

 

 

sunionstore把並集結果存儲到新集合中

 

127.0.0.1:6379> sunionstore r3 set2 set3

 

(integer) 5

 

127.0.0.1:6379> smembers r3

 

1) "e"

 

2) "a"

 

3) "b"

 

4) "c"

 

5) "d"

 

 

 

存儲Set使用場景

 

 跟蹤一些惟一性數據

 

 用於維護數據對象之間的關聯關係

 

 

 

9、Redis持久化

全部的數據都存在內存中,從內存當中同步到硬盤上,這個過程叫作持久化過程

持久化操做,兩種方式:rdb方式、aof方式,能夠單獨使用或者結合使用。

使用方法:

 rdb持久化方法:在指定的時間間隔寫入硬盤

aof方式:將以日誌,記錄每個操做,服務器啓動後就構建數據庫。

配置能夠禁用 持久化功能。

也能夠同時使用兩種方式。

Redis的持久化之RDB方式:

RDB方式 Redis是默認支持的

優點:只有一個文件,時間間隔的數據,能夠歸檔爲一個文件,方便壓縮轉移(就一個文件)

劣勢:若是宕機,數據損失比較大,由於它是沒一個時間段進行持久化操做的。也就是積攢的數據比較多,一旦懵逼,就完全懵逼了

配置:

[root@localhost redis]# vi redis.conf 

編輯redis.conf

往下拉:

QQ鎴浘20170718091223.jpg

 

這裏save 900 1 表示 每900秒內至少有1個kery發生變化,就持久化

save 300 10表示 每300秒內至少有10個key發生變化,就持久化

save 60 10000表示 每60秒內至少有10000個key發生變化,就持久化

 

再往下拉:

QQ鎴浘20170718091658.jpg

 

這裏有個dbfilename配置 是 保存的持久化的文件名 默認是dump.rdb

 

再往下:

QQ鎴浘20170718091821.jpg

 

dir ./ 表示文件存儲路徑是當前路徑;

 

 

咱們退出

QQ鎴浘20170718092622.jpg

 

當前路徑裏確實是有這個文件的。

 

 

RDB北風和恢復數據  

假如遇到斷電或者宕機或者天然災害, 咱們須要恢復數據 咱們模擬下。

咱們先重置下數據

shutdown關閉下redis

127.0.0.1:6379> shutdown

not connected> exit

[root@localhost redis]# ll

總用量 52

drwxr-xr-x. 2 root root   134 7月   6 09:18 bin

-rw-r--r--. 1 root root    99 7月  18 10:41 dump.rdb

-rw-r--r--. 1 root root 46697 7月  18 10:41 redis.conf

而後刪除掉rdb文件,再啓動redis

[root@localhost redis]# rm -rf dump.rdb 

[root@localhost redis]# ll

總用量 48

drwxr-xr-x. 2 root root   134 7月   6 09:18 bin

-rw-r--r--. 1 root root 46697 7月  18 10:41 redis.conf

[root@localhost redis]# ./bin/redis-server ./redis.conf 

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> keys *

(empty list or set)

這時候是沒有數據的

 

這時候啓動 是沒數據的

[root@localhost redis]# ./bin/redis-server ./redis.conf 

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> keys *

(empty list or set)

 

咱們搞幾個key,而後shutdown save 保存退出

127.0.0.1:6379> set n1 1

OK

127.0.0.1:6379> set n2 2

OK

127.0.0.1:6379> set n3 3

OK

127.0.0.1:6379> shutdown save

not connected> exit

 

假如這時候 咱們再重啓redis 這時候啓動過程會進程rdb check驗證 而後加載redis目錄下rdb文件;加載數據;

咱們驗證下:

 

咱們再次啓動

[root@localhost redis]# ./bin/redis-server ./redis.conf 

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> keys *

1) "n1"

2) "n3"

3) "n2"

說明是數據加載進來了;

 

這裏咱們把redis下的rdb文件剪切到其餘地方去 而後再啓動試下

[root@localhost redis]# ll

總用量 52

drwxr-xr-x. 2 root root   134 7月  18 11:05 bin

-rw-r--r--. 1 root root    99 7月  18 11:35 dump.rdb

-rw-r--r--. 1 root root 46697 7月  18 10:41 redis.conf

[root@localhost redis]# mv dump.rdb /root/

[root@localhost redis]# ll

總用量 48

drwxr-xr-x. 2 root root   134 7月  18 11:05 bin

-rw-r--r--. 1 root root 46697 7月  18 10:41 redis.conf

[root@localhost redis]# ll /root/

總用量 8

-rw-------. 1 root root 1261 7月   6 05:42 anaconda-ks.cfg

-rw-r--r--. 1 root root   99 7月  18 11:35 dump.rdb

-rw-r--r--. 1 root root    0 7月  13 22:00 java牛逼

drwxr-xr-x. 2 root root    6 7月  13 22:07 java書籍

 

剪切到了root下

 

這時候再啓動下:

[root@localhost redis]# ./bin/redis-server ./redis.conf 

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> keys *

(empty list or set)

數據沒了

 

恢復數據的話 咱們只須要把備份文件搞到redis下便可

再複製過來便可:

[root@localhost redis]# cp /root/dump.rdb /usr/local/redis/

cp:是否覆蓋"/usr/local/redis/dump.rdb"? y

[root@localhost redis]# ./bin/redis-server ./redis.conf 

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> keys *

1) "n2"

2) "n1"

3) "n3"

 

這時候 數據就有了 這就是恢復過程;

 

繞了一大圈 總結下

平時咱們能夠按期把rdb文件備份到指定地方 須要恢復的時候 直接把rdb搞到redis下便可;

 Redis的持久化之AOF方式

AOF方式:將以日誌,記錄每個操做

優點:安全性相對RDB方式高不少;

劣勢:效率相對RDB方式低不少;

配置:

[root@localhost redis]# vi redis.conf 

 

編輯redis.conf

 

往下拉 找到:

QQ鎴浘20170718094504.jpg

 

appendonly no默認關閉aof方式 咱們修改爲yes 就開啓

下面那個是默認的aof文件名

 

再往下拉:

QQ鎴浘20170718094518.jpg

 

這裏是三種同步策略:

always 是 只要發生修改,當即同步 (推薦實用 安全性最高)

everysec 是 每秒同步一次

no是不一樣步 

 

咱們修改爲always

 

而後保存 退出;

 

咱們從新啓動redis,而後隨便加幾個key

 

QQ鎴浘20170718103620.jpg

 

這裏就有一個appendonly.aof文件;

 

aof方式恢復數據

 

咱們先重置數據

[root@localhost redis]# rm -rf dump.rdb 

[root@localhost redis]# ll

總用量 48

drwxr-xr-x. 2 root root   134 7月  18 11:05 bin

-rw-r--r--. 1 root root 46698 7月  18 12:14 redis.conf

 

啓動redis

[root@localhost redis]# ./bin/redis-server ./redis.conf 

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> keys *

(empty list or set)

目前數據庫是空

 

添加數據

127.0.0.1:6379> set n1 1

OK

127.0.0.1:6379> set n2 2

OK

127.0.0.1:6379> set n3 3

OK

127.0.0.1:6379> shutdown nosave

not connected> exit

[root@localhost redis]# ll

總用量 52

-rw-r--r--. 1 root root   107 7月  18 12:17 appendonly.aof

drwxr-xr-x. 2 root root   134 7月  18 11:05 bin

-rw-r--r--. 1 root root 46698 7月  18 12:14 redis.conf

[root@localhost redis]# 

 

咱們把aof文件剪切到其餘地方去 而後啓動試下

[root@localhost redis]# mv appendonly.aof /root/

[root@localhost redis]# ll

總用量 48

drwxr-xr-x. 2 root root   134 7月  18 11:05 bin

-rw-r--r--. 1 root root 46698 7月  18 12:14 redis.conf

[root@localhost redis]# ./bin/redis-server ./redis.conf 

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> keys *

(empty list or set)

沒數據;

 

咱們再把aof文件複製回來;

[root@localhost redis]# cp /root/appendonly.aof /usr/local/redis/

cp:是否覆蓋"/usr/local/redis/appendonly.aof"? y

[root@localhost redis]# ll

總用量 52

-rw-r--r--. 1 root root   107 7月  18 12:22 appendonly.aof

drwxr-xr-x. 2 root root   134 7月  18 11:05 bin

-rw-r--r--. 1 root root 46698 7月  18 12:14 redis.conf

[root@localhost redis]# ./bin/redis-server ./redis.conf 

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> keys *

1) "n1"

2) "n3"

3) "n2"

咱們發現 以及有數據了

小結: 咱們平時能夠把aof文件按期備份 而後須要的時候 拷貝到redis下 重啓便可;

 

學習資料參考:《一頭扎進Redis》 和  http://blog.java1234.com  感謝:小鋒

相關文章
相關標籤/搜索