第八章· Redis API 開發

Redis 開發

1.源碼安裝Python環境

Python官網:https://www.python.org/node

#下載Python3.6.4安裝包
[root@db03 ~]# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
#生成Python環境安裝文件
[root@db03 ~]# ./configure --prefix=/usr/local/python3.6.4 --with-ssl
#編譯
[root@db03 ~]# make
#安裝
[root@db03 ~]# make install
#軟連接python3命令
[root@db03 ~]# ln -s /usr/local/python3.6.4/bin/python3 /usr/bin/
#軟連接pip3命令
[root@db03 ~]# ln -s /usr/local/python3.6.4/bin/pip3  /usr/bin/

2.安裝python鏈接redis驅動

Python鏈接redis驅動網站:http://www.redis.cn/clients
python

打開github倉庫,而後能夠下載驅動器的包
linux

也可使用pip安裝redis驅動git

[root@db01 Python-3.6.4]# pip3 install redis
Collecting redis
  Downloading https://files.pythonhosted.org/packages/ac/a7/cff10cc5f1180834a3ed564d148fb4329c989cbb1f2e196fc9a10fa07072/redis-3.2.1-py2.py3-none-any.whl (65kB)
    100% |████████████████████████████████| 71kB 120kB/s
Installing collected packages: redis
Successfully installed redis-3.2.1
You are using pip version 9.0.1, however version 19.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

3.Python鏈接單臺Redis API

安裝redisgithub

#下載
[root@db01 src]# wget http://download.redis.io/releases/redis-3.2.12.tar.gz
#解壓
[root@db01 src]# tar xf redis-3.2.12.tar.gz
#移動到指定目錄
[root@db01 src]# mv redis-3.2.12 /application/
#作軟連接
[root@db01 src]# ln -s /application/redis-3.2.12 /application/redis
#進入redis目錄
[root@db01 src]# cd /application/redis
#編譯
[root@db01 redis]# make
#添加環境變量
[root@db01 redis]# vim /etc/profile.d/redis.sh
export PATH="/application/redis/src:$PATH"
#建立配置文件存放目錄
[root@db01 ~]# mkdir -p /data/6379
#編輯redis配置文件
[root@db01 ~]# vim /data/6379/redis.conf
port 6379
daemonize yes
pidfile /data/6379/redis.pid
logfile "/data/6379/redis.log"
dbfilename dump.rdb
dir /data/6379
protected-mode no
appendonly yes
requirepass  zls
#啓動redis
[root@db01 ~]# redis-server /data/6379/redis.conf
#鏈接redis
[root@db01 ~]# redis-cli -a zls
#設置key
127.0.0.1:6379> set name zls
OK

使用Python鏈接redisredis

#鏈接Python終端
[root@db01 ~]# python3
Python 3.6.4 (default, Apr  8 2019, 17:12:35)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

#導入redis模塊
>>> import redis
#設置redis鏈接變量
>>> r = redis.StrictRedis(host='localhost', port=6379, db=0,password='zls')
#獲取剛纔建立的key
>>> r.get('name')
b'zls'
#建立一個key
>>> r.set('age', '18')
True
#退出Python終端
>>> quit()
#鏈接redis
[root@db01 ~]# redis-cli -a zls
#查看是否有剛纔建立的key
127.0.0.1:6379> KEYS *
#查看age的值
127.0.0.1:6379> get age
"18"

4.Python鏈接Redis Sentinel API

通常在企業中,Redis是不會使用單臺,大部分企業都是以集羣的形式存在的,因此咱們須要知道,Python如何鏈接Redis集羣的API,固然咱們講的集羣,有Sentinel和Redis Cluster。vim

#啓動Redis多實例
[root@db01 ~]# redis-server /data/6380/redis.conf
[root@db01 ~]# redis-server /data/6381/redis.conf
[root@db01 ~]# redis-server /data/6382/redis.conf 
#啓動Redis Sentinel
[root@db01 ~]# redis-sentinel /data/26380/sentinel.conf &
#鏈接python終端
[root@db01 ~]# python3
Python 3.6.4 (default, Apr  8 2019, 17:12:35)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.

#導入Redis Sentinel模塊
>>> from redis.sentinel import Sentinel
#設置鏈接信息變量
>>> sentinel = Sentinel([('localhost', 26380)], socket_timeout=0.1)
#獲取主庫,從庫信息
>>> sentinel.discover_master('mymaster')
>>> sentinel.discover_slaves('mymaster')
#配置讀寫分離,寫節點
>>> master = sentinel.master_for('mymaster', socket_timeout=0.1,password="zls")  
#配置讀寫分離,讀節點
>>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1,password="zls")  
#讀寫分離測試key     
>>> master.set('zls', 'handsome')  
>>> slave.get('zls')  
'handsome'

5.Python鏈接Redis Cluster

Redis Cluster的鏈接並操做(python2.7.2以上版本才支持redis cluster,咱們選擇的是3.6.4)
https://github.com/Grokzen/redis-py-clusterapi

#安裝Python鏈接Redis Cluster驅動
[root@db01 ~]# pip3 install redis-py-cluster
Collecting redis-py-cluster
  Downloading https://files.pythonhosted.org/packages/6d/02/b2458f900496d1e573ada7ffd882efe62aeee992eab1222411fe08aa5f75/redis-py-cluster-1.3.6.tar.gz
Collecting redis==2.10.6 (from redis-py-cluster)
  Downloading https://files.pythonhosted.org/packages/3b/f6/7a76333cf0b9251ecf49efff635015171843d9b977e4ffcf59f9c4428052/redis-2.10.6-py2.py3-none-any.whl (64kB)
    100% |████████████████████████████████| 71kB 26kB/s
Installing collected packages: redis, redis-py-cluster
  Found existing installation: redis 3.2.1
    Uninstalling redis-3.2.1:
      Successfully uninstalled redis-3.2.1
  Running setup.py install for redis-py-cluster ... done
Successfully installed redis-2.10.6 redis-py-cluster-1.3.6
You are using pip version 9.0.1, however version 19.0.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
#啓動Redis Cluster集羣
[root@db01 ~]# redis-server /data/7000/redis.conf
[root@db01 ~]# redis-server /data/7001/redis.conf
[root@db01 ~]# redis-server /data/7002/redis.conf
[root@db01 ~]# redis-server /data/7003/redis.conf
[root@db01 ~]# redis-server /data/7004/redis.conf
[root@db01 ~]# redis-server /data/7005/redis.conf
#鏈接Python終端
[root@db01 ~]# python3
Python 3.6.4 (default, Apr  8 2019, 17:12:35)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
#導入Redis Cluster模塊
>>> from rediscluster import StrictRedisCluster
#設置登陸redis集羣變量
>>> startup_nodes = [{"host": "127.0.0.1", "port": "7000"}]
#設置鏈接變量
>>> rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)
#測試設置key
>>> rc.set("foo", "bar")
True
#查詢key
>>> print(rc.get("foo"))
bar

6.Redis緩存故障概念

緩存穿透緩存

概念:
訪問一個不存在的key,緩存不起做用,請求會穿透到DB,流量大時DB會掛掉。app

解決方案:
採用布隆過濾器,使用一個足夠大的bitmap,用於存儲可能訪問的key,不存在的key直接被過濾;
訪問key未在DB查詢到值,也將空值寫進緩存,但能夠設置較短過時時間。

緩存雪崩

概念:
大量的key設置了相同的過時時間,致使在緩存在同一時刻所有失效,形成瞬時DB請求量大、壓力驟增,引發雪崩。

解決方案:
能夠給緩存設置過時時間時加上一個隨機值時間,使得每一個key的過時時間分佈開來,不會集中在同一時刻失效。

緩存擊穿

概念:
一個存在的key,在緩存過時的一刻,同時有大量的請求,這些請求都會擊穿到DB,形成瞬時DB請求量大、壓力驟增。

解決方案: 在訪問key以前,採用SETNX(set if not exists)來設置另外一個短時間key來鎖住當前key的訪問,訪問結束再刪除該短時間key。

相關文章
相關標籤/搜索