日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

【Python之旅】第七篇(二):Redis使用基础

發(fā)布時間:2025/7/14 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python之旅】第七篇(二):Redis使用基础 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

0.說明

????由于學(xué)習(xí)開發(fā)監(jiān)控軟件的需要,因此需要使用到Redis,這里簡單介紹。

????注意,使用的環(huán)境為:Ubuntu 15.10


1.安裝

? ? 可以采用源碼安裝,也可以采用apt-get來安裝,都比較簡單。


2.啟動

????由于采用的是源碼安裝的方式,所以直接進入src目錄,啟動redis-server:

xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/redis-2.8.9/src$?./redis-server? [12681]?16?Oct?00:06:52.964?#?Warning:?no?config?file?specified,?using?the?default?config.?In?order?to?specify?a?config?file?use?./redis-server?/path/to/redis.conf [12681]?16?Oct?00:06:52.967?#?You?requested?maxclients?of?10000?requiring?at?least?10032?max?file?descriptors. [12681]?16?Oct?00:06:52.968?#?Redis?can't?set?maximum?open?files?to?10032?because?of?OS?error:?Operation?not?permitted. [12681]?16?Oct?00:06:52.968?#?Current?maximum?open?files?is?1024.?maxclients?has?been?reduced?to?4064?to?compensate?for?low?ulimit.?If?you?need?higher?maxclients?increase?'ulimit?-n'._._??????????????????????????????????????????????????_.-``__?''-._?????????????????????????????????????????????_.-``????`.??`_.??''-._???????????Redis?2.8.9?(00000000/0)?64?bit.-``?.-```.??```\/????_.,_?''-._???????????????????????????????????(????'??????,???????.-`??|?`,????)?????Running?in?stand?alone?mode|`-._`-...-`?__...-.``-._|'`?_.-'|?????Port:?6379|????`-._???`._????/?????_.-'????|?????PID:?12681`-._????`-._??`-./??_.-'????_.-'???????????????????????????????????|`-._`-._????`-.__.-'????_.-'_.-'|??????????????????????????????????|????`-._`-._????????_.-'_.-'????|???????????http://redis.io????????`-._????`-._`-.__.-'_.-'????_.-'???????????????????????????????????|`-._`-._????`-.__.-'????_.-'_.-'|??????????????????????????????????|????`-._`-._????????_.-'_.-'????|??????????????????????????????????`-._????`-._`-.__.-'_.-'????_.-'???????????????????????????????????`-._????`-.__.-'????_.-'???????????????????????????????????????`-._????????_.-'???????????????????????????????????????????`-.__.-'???????????????????????????????????????????????[12681]?16?Oct?00:06:52.974?#?Server?started,?Redis?version?2.8.9 [12681]?16?Oct?00:06:52.974?#?WARNING?overcommit_memory?is?set?to?0!?Background?save?may?fail?under?low?memory?condition.?To?fix?this?issue?add?'vm.overcommit_memory?=?1'?to?/etc/sysctl.conf?and?then?reboot?or?run?the?command?'sysctl?vm.overcommit_memory=1'?for?this?to?take?effect. [12681]?16?Oct?00:06:52.976?*?DB?loaded?from?disk:?0.002?seconds [12681]?16?Oct?00:06:52.977?*?The?server?is?now?ready?to?accept?connections?on?port?6379

????出現(xiàn)上面所示的提示,說明已經(jīng)正常啟動了redis。


3.交互式操作

????進入src目錄,運行redis-cli即可進入redis交互界面:

xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/redis-2.8.9/src$?./redis-cli 127.0.0.1:6379>

????基本操作:

#查看幫助 127.0.0.1:6379>?help?setSET?key?value?[EX?seconds]?[PX?milliseconds]?[NX|XX]summary:?Set?the?string?value?of?a?keysince:?1.0.0group:?string#創(chuàng)建key-value 127.0.0.1:6379>?set?name?xpleaf OK#獲得key對應(yīng)的value 127.0.0.1:6379>?get?name "xpleaf"#創(chuàng)建有時間的key-value 127.0.0.1:6379>?set?name2?CL?ex?5 OK#創(chuàng)建列表 127.0.0.1:6379>?lpush?stu_list?xpleaf?yonghaoye?CL (integer)?3 127.0.0.1:6379>?lpush?stu_list?CLYYH (integer)?4#獲取列表內(nèi)容 127.0.0.1:6379>?lrange?stu_list?1?4 1)?"CL" 2)?"yonghaoye" 3)?"xpleaf" 127.0.0.1:6379>?lrange?stu_list?0?4 1)?"CLYYH" 2)?"CL" 3)?"yonghaoye" 4)?"xpleaf"#刪除key-value或其它數(shù)據(jù)類型 127.0.0.1:6379>?del?name (integer)?1


3.在Python交互器中使用redis

  • 要使用Python來操作Redistribute,則需要安裝Python與Redis通信的接口:

apt-get?install?python-redis
  • 在交互器中連接Redis數(shù)據(jù)庫:

>>>?import?redis >>>?r?=?redis.Redis('127.0.0.1',?port=6379,?db=0)
  • 基本操作

#查看所有的key >>>?r.keys() ['YourKey',?'stu_list',?'k1',?'k3']#創(chuàng)建key-value >>>?r.set('xpleaf',?'xpleaf') True#獲取key所對應(yīng)的value >>>?r['xpleaf'] 'xpleaf' 或 >>>?r.get('xpleaf') 'xpleaf'#保存Python中的字典到Redis數(shù)據(jù)庫中 >>>?import?json >>>?myDict?=?{'name':?'xpleaf',?'age':?21,?'loving':?'cl'} >>>?r['Py_myDict']?=?json.dumps(myDict) >>>? >>>?r['Py_myDict'] '{"age":?21,?"name":?"xpleaf",?"loving":?"cl"}'#取出保存在Redis數(shù)據(jù)庫中的Python字典 >>>?a?=?json.loads(r['Py_myDict']) >>>?a {u'age':?21,?u'name':?u'xpleaf',?u'loving':?u'cl'} >>>?a['name'] u'xpleaf'


轉(zhuǎn)載于:https://blog.51cto.com/xpleaf/1703387

總結(jié)

以上是生活随笔為你收集整理的【Python之旅】第七篇(二):Redis使用基础的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。