搜尋
首頁後端開發Python教學詳解python安裝與使用redis的方法

這篇文章主要介紹了python安裝與使用redis的方法,分析了安裝與配置的具體步驟,並結合實例詳細分析了redis資料庫的具體使用技巧,需要的朋友可以參考下

本文實例講述了python安裝與使用redis的方法。分享給大家供大家參考,具體如下:

1、安裝

#好吧,我承認我只會最簡單的安裝:

#
sudo apt-get install redis-server

python 支援包: (其實就一個文件,搞過來就能用)

sudo apt-get install python-redis

2、設定

設定一下吧,預設設定檔在: "/etc/redis/redis.conf"
綁定ip:

"bind 127.0.0.1″ -> "bind 10.0.1.7″

將磁碟同步改為不同步或每秒同步,一直同步的話太慢了:

#
"appendfsync always" -> "appendfsync no"

##檢查一下後台執行是否打開:

"daemonize yes"

或其他你想設定的,例如:

連接超時時間: "timeout 300″

#運行級別: "loglevel notice" (個人認為預設的這個就挺好,非出現大異常,不用改為debug )

3、使用

#! /usr/bin/env python
#coding=utf-8
import redis
print redis.__file__
# 连接,可选不同数据库
r = redis.Redis(host='10.0.1.7', port=6379, db=1)
# -------------------------------------------
# 看信息
info = r.info()
for key in info:
 print "%s: %s" % (key, info[key])
# 查数据库大小
print '\ndbsize: %s' % r.dbsize()
# 看连接
print "ping %s" % r.ping()
# 选数据库
#r.select(2)
# 移动数据去2数据库
#r.move('a',2)
# 其他
#r.save('a') # 存数据
#r.lastsave('a') # 取最后一次save时间
#r.flush() #刷新
#r.shutdown() #关闭所有客户端,停掉所有服务,退出服务器
#
#--------------------------------------------
# 它有四种类型: string(key,value)、list(序列)、set(集合)、zset(有序集合,多了一个顺序属性)
# 不知道你用的哪种类型?
# print r.get_type('a') #可以告诉你
# -------------------------------------------
# string操作
print '-'*20
# 塞数据
r['c1'] = 'bar'
#或者
r.set('c2','bar')
#这里有个 getset属性,如果为True 可以在存新数据时将上次存储内容同时搞出来
print 'getset:',r.getset('c2','jj')
#如果你想设置一个递增的整数 每执行一次它自加1:
print 'incr:',r.incr('a')
#如果你想设置一个递减的整数 please:
print 'decr:',r.decr('a')
# 取数据
print 'r['']:',r['c1']
#或者
print 'get:',r.get('a')
#或者 同时取一批
print 'mget:',r.mget('c1','c2')
#或者 同时取一批 它们的名字(key)很像 而恰好你又不想输全部
print 'keys:',r.keys('c*')
#又或者 你只想随机取一个:
print 'randomkey:',r.randomkey()
# 查看一个数据有没有 有 1 无0
print 'existes:',r.exists('a')
# 删数据 1是删除成功 0和None是没这个东西
print 'delete:',r.delete('cc')
# 哦对了 它是支持批量操作的
print 'delete:',r.delete('c1','c2')
# 其他
r.rename('a','c3') #呃.改名
r.expire('c3',10) #让数据10秒后过期 说实话我不太明白么意思
r.ttl('c3') #看剩余过期时间 不存在返回-1
#--------------------------------
# 序列(list)操作
print '-'*20
# 它是两头通的
# 塞入
r.push('b','gg')
r.push('b','hh')
# head 属性控制是不是从另一头塞
r.push('b','ee',head=True)
# 看长度
print 'list len:',r.llen('b')
# 列出一批出来
print 'list lrange:',r.lrange('b',start=0,end=-1)
# 取出一位
print 'list index 0:',r.lindex('b',0)
# 修剪列表
#若start 大于end,则将这个list清空
print 'list ltrim :',r.ltrim('b',start=0,end=3) #只留 从0到3四位
# 排序
# 这可是个大工程
#--------------------------------
# 集合(set)操作
# 塞数据
r.sadd('s', 'a')
# 判断一个set长度为多少 不存在为0
r.scard('s')
# 判断set中一个对象是否存在
r.sismember('s','a')
# 求交集
r.sadd('s2','a')
r.sinter('s1','s2')
#求交集并将结果赋值
r.sinterstore('s3','s1','s2')
# 看一个set对象
r.smembers('s3')
# 求并集
r.sunion('s1','s2')
# 阿 我想聪明的你已经猜到了
#求并集 并将结果返回
r.sunionstore('ss','s1','s2','s3')
# 求不同
# 在s1中有,但在s2和s3中都没有的数
r.sdiff('s1','s2','s3')
r.sdiffstore('s4','s1','s2')# 这个你懂的
# 取个随机数
r.srandmember('s1')
#-------------------------------------
#zset 有序set
#'zadd', 'zcard', 'zincr', 'zrange', 'zrangebyscore', 'zrem', 'zscore'
# 分别对应
#添加, 数量, 自加1,取数据,按照积分(范围)取数据,删除,取积分
# 我靠 你玩死我了 redis!
# 今天在实验中,我尝试插入一条zset类型数据:
r1.zset(u'www.liyi99.com','liwu',3)
# 插入成功
# 我继续插入
r1.zset(u'www.liyi99,com',u'\u9001\u793c',5)
#报错:
#UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
#这次插入的是礼物的中文词 unicode编码
#为什么会失败那,这条数据是我从redis里面取出来然后没做任何修改再插入的阿
#redis返回和接受的数据类型都是unicode编码的阿
#好吧,我们再次插入试试
#再次插入
r1.zset('www.liyi99.com',u'\u9001\u793c',5)
#成功了
#插入
r1.zset('www.liyi99.com','礼物',5)
#依然成功,跟入redis.py 1024行
return self.send_command('ZADD %s %s %s\r\n%s\r\n' % (
  key, score, len(member), member))
# 哦 万恶的编码转换!
#不过取的时候,不论第一个是何种类型的数据都无关系

更多詳解python安裝與使用redis的方法相關文章請關注PHP中文網!

#

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?May 03, 2025 am 12:11 AM

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

說明如何將內存分配給Python中的列表與數組。說明如何將內存分配給Python中的列表與數組。May 03, 2025 am 12:10 AM

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

您如何在Python數組中指定元素的數據類型?您如何在Python數組中指定元素的數據類型?May 03, 2025 am 12:06 AM

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

什麼是Numpy,為什麼對於Python中的數值計算很重要?什麼是Numpy,為什麼對於Python中的數值計算很重要?May 03, 2025 am 12:03 AM

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

討論'連續內存分配”的概念及其對數組的重要性。討論'連續內存分配”的概念及其對數組的重要性。May 03, 2025 am 12:01 AM

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

您如何切成python列表?您如何切成python列表?May 02, 2025 am 12:14 AM

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

在Numpy陣列上可以執行哪些常見操作?在Numpy陣列上可以執行哪些常見操作?May 02, 2025 am 12:09 AM

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,減法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Python的數據分析中如何使用陣列?Python的數據分析中如何使用陣列?May 02, 2025 am 12:09 AM

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具