python操做redis集羣

strictRedis對象方法用於鏈接redisnode

指定主機地址,port與服務器鏈接,默認db是0,redis默認數據庫有16個,在配置文件中指定database 16python

上代碼git

1、對redis的單實例進行鏈接操做
根據不一樣的實例方法,與redis的命令對應
python3
>>>import redis
>>>r = redis.StrictRedis(host='localhost', port=6379, db=0,password='root')
>>>r.set('lufei', 'guojialei')
True
>>>r.get('lufei')
'bar'

增刪改查

>>> conn=redis.StrictRedis()
>>>
>>>
>>> conn.set("name1","alex1")
True
>>> conn.set("name2","wupeiqi")
True
>>>
>>>
>>> conn.set("name1","alex666")
True
>>> conn.delete("name2","name1")
2github

>>> conn.keys()
[b'name3', b'name2', b'name1']redis

--------------------

2、sentinel集羣鏈接並操做


[root@db01 ~]# redis-server /data/6380/redis.conf
[root@db01 ~]# redis-server /data/6381/redis.conf
[root@db01 ~]# redis-server /data/6382/redis.conf 
[root@db01 ~]# redis-sentinel /data/26380/sentinel.conf &



--------------------------------
## 導入redis sentinel包
>>> from redis.sentinel import Sentinel  
##指定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)  
#讀節點
>>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1)  
###讀寫分離測試   key     
>>> master.set('oldboy', '123')  
>>> slave.get('oldboy')  
'123'


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


3、python鏈接rediscluster集羣測試
使用

python3
>>> from rediscluster import StrictRedisCluster  
>>> startup_nodes = [{"host": "127.0.0.1", "port": "7000"}]  
### Note: decode_responses must be set to True when used with python3  
>>> rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)  
>>> rc.set("foo", "bar")  
True  
>>>   
'bar'
----------------------

 redis存儲session

django-redis-sessions 官方文檔:https://pypi.org/project/django-redis-sessions/數據庫

dango-redis 官方文檔:http://niwinz.github.io/django-redis/latest/#_configure_as_cache_backenddjango

安裝模塊vim

安裝模塊
 pip3 install django-redis-sessions
或者
pip3 install django-redis

本文這裏用的是django 2.0語法,建立django項目服務器

django-admin startproject mydjango

修改settings.py文件session

寫入

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/0",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "PASSWORD": "",
#             "PARSER_CLASS": "redis.connection.HiredisParser",
#             "SOCKET_TIMEOUT": 10,
#             "CONNECTION_POOL_CLASS_KWARGS": {
#                 "max_connections": 2,
#             }
        }
    }
}
  
#SESSION_COOKIE_AGE = 30 * 60 #設置session過時時間爲30分鐘
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'

建立app01

django-admin startapp app01

編寫session視圖函數 app01.views

from django.shortcuts import render,HttpResponse

def set_session(request):
    request.session['username']='chaoge'
    request.session['age']=18
    return HttpResponse("設置sesson成功")

def get_session(request):
    username=request.session['username']
    age = request.session['age']
    return HttpResponse(username+":"+str(age))

添加url路由

vim mydjango/urls.py

from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
    path('set_session/',views.set_session),
    path('get_session/',views.get_session),
    path('admin/', admin.site.urls),
]

確保啓動redis服務端,默認方式啓動

redis-server &

redis-cli 鏈接無誤
發送ping命令獲得pong便可

啓動django

python3 manage.py runserver 0.0.0.0:8000

訪問視圖,設置一條session信息,存入redis數據庫

訪問視圖,獲取session信息

檢查redis數據庫,是否存在一條key

127.0.0.1:6379> keys *
1) ":1:django.contrib.sessions.cachep220moqvxclz2hyjqmbybqs3v8ck2i39"

獲取這個key的值

127.0.0.1:6379> get :1:django.contrib.sessions.cachep220moqvxclz2hyjqmbybqs3v8ck2i39"\x80\x04\x95!\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\busername\x94\x8c\x06chaoge\x94\x8c\x03age\x94K\x12u."

相關文章
相關標籤/搜索