日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

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

0.說明

????由于學習開發監控軟件的需要,因此需要使用到Redis,這里簡單介紹。

????注意,使用的環境為: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

????出現上面所示的提示,說明已經正常啟動了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#創建key-value 127.0.0.1:6379>?set?name?xpleaf OK#獲得key對應的value 127.0.0.1:6379>?get?name "xpleaf"#創建有時間的key-value 127.0.0.1:6379>?set?name2?CL?ex?5 OK#創建列表 127.0.0.1:6379>?lpush?stu_list?xpleaf?yonghaoye?CL (integer)?3 127.0.0.1:6379>?lpush?stu_list?CLYYH (integer)?4#獲取列表內容 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或其它數據類型 127.0.0.1:6379>?del?name (integer)?1


3.在Python交互器中使用redis

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

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

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

#查看所有的key >>>?r.keys() ['YourKey',?'stu_list',?'k1',?'k3']#創建key-value >>>?r.set('xpleaf',?'xpleaf') True#獲取key所對應的value >>>?r['xpleaf'] 'xpleaf' 或 >>>?r.get('xpleaf') 'xpleaf'#保存Python中的字典到Redis數據庫中 >>>?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數據庫中的Python字典 >>>?a?=?json.loads(r['Py_myDict']) >>>?a {u'age':?21,?u'name':?u'xpleaf',?u'loving':?u'cl'} >>>?a['name'] u'xpleaf'


轉載于:https://blog.51cto.com/xpleaf/1703387

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。