## 安裝包
* 到[中文官網](http://redis.cn/clients.html)查找客戶端代碼
* 聯網安裝
~~~
sudo pip install redis
~~~
* 使用源碼安裝
~~~
unzip redis-py-master.zip
cd redis-py-master
sudo python setup.py install
~~~
## 交互代碼
* 引入模塊
~~~
import redis
~~~
* 連接
~~~
try:
r=redis.StrictRedis(host='localhost',port=6379)
except Exception,e:
print e.message
~~~
* 方式一:根據數據類型的不同,調用相應的方法,完成讀寫
* 更多方法同前面學的命令
~~~
r.set('name','hello')
r.get('name')
~~~
* 方式二:pipline
* 緩沖多條命令,然后一次性執行,減少服務器-客戶端之間TCP數據庫包,從而提高效率
~~~
pipe = r.pipeline()
pipe.set('name', 'world')
pipe.get('name')
pipe.execute()
~~~
## 封裝
* 連接redis服務器部分是一致的
* 這里將string類型的讀寫進行封裝
~~~
import redis
class RedisHelper():
def __init__(self,host='localhost',port=6379):
self.__redis = redis.StrictRedis(host, port)
def get(self,key):
if self.__redis.exists(key):
return self.__redis.get(key)
else:
return ""
def set(self,key,value):
self.__redis.set(key,value)
~~~
## python3代碼
~~~
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@Time : 5/18/18 7:39 PM
@Author : haibo
@File : MyRedis.py
'''
from redis import *
# r = StrictRedis(host='localhost', port=6379)
# write
# pipe = r.pipeline()
# pipe.set('py10', 'hello1')
# pipe.set('py11', 'world')
# pipe.execute()
# read
# temp = r.get('py10')
# print(temp)
class redisHelper(object):
def __init__(self, host, port):
self.__redis = StrictRedis(host, port)
def set(self, key, value):
self.__redis.set(key, value)
def get(self, key):
return self.__redis.get(key)
~~~
- mysql
- 1.創建庫和表
- 1.1.數據庫簡介
- 1.2.安裝管理
- 1.3.數據完整性
- 1.4.命令腳本操作
- 2.查詢
- 2.1.條件
- 2.2.聚合
- 2.3.分組
- 2.4.排序
- 2.5.分頁
- 3.高級
- 3.1.關系
- 3.2.連接
- 3.3.自關聯
- 3.4.子查詢
- 3.5.內置函數
- 3.6.視圖
- 3.7.事務
- 4.與python交互
- 4.1.交互類型
- 4.2.增改刪
- 4.3.查詢
- 4.4.封裝
- 4.5.用戶登錄
- Nosql簡介
- mongodb
- 1.基本操作
- 1.1.環境安裝
- 1.2.數據庫操作
- 1.3.集合操作
- 1.4.數據類型
- 1.5.數據操作
- 1.6.數據查詢
- 1.6.1.Limit與Skip
- 1.6.2.投影
- 1.6.3.排序
- 1.6.4.統計個數
- 1.6.5.消除重復
- 2.高級操作
- 2.1.聚合aggregate
- 2.1.1.$group
- 2.1.2.$match
- 2.1.3.$project
- 2.1.4.$sort
- 2.1.5.$limit,$skip
- 2.1.6.$unwind
- 2.2.安全
- 2.3.復制(副本集)
- 2.4.備份和恢復
- 2.5.與python交互
- redis
- 1.基本配置
- 2.數據操作
- 2.1.string
- 2.2.鍵命令
- 2.3.hash
- 2.4.list
- 2.5.set
- 2.6.zset
- 4.高級
- 4.1.發布訂閱
- 4.2.主從配置
- 5.與python交互
- 6.login登陸完善