Python連接Redis連線配置
系統環境:
OS:Oracle Linux Enterprise 5.6
redis:redis-2.6.8
python:Python-2.7.3
redis的python包版本:redis-2.7.前提條件:
1.確保Redis已成功安裝並且正確配置,參考文檔
包:
使用easy-install安裝,關於easy-install的配置,參考以上Python環境的搭建。 [root@njdyw bin]# easy_install2.7.3 redisSearching for redisReading http://pypi.python.org/simple/redis/Re.com pyBest match: redis 2.7.2Downloading http://pypi.python.org/packages/source/r/redis/redis-2.7.2.tar.gz#md5=17ac60dcf1333f82cc .2.tar.gzRunning redis-2.7.2/setup.py -q bdist_egg --dist-dir /tmp/easy_install-8FAlft/redis-2.7.2/egg-dist-tmp-JzQJJfe flagi not set; analyzing archive contents...Adding redis 2.7.2 to easy-install.pth fileInstalled /usr/local/python2.7.2/python2.7. 2.7.2-py2.7.egg
Processing dependencies for redis
Finished processing dependencies for redis
--安裝Parser包(可選)
說明:Parser可以控制如何解析redis響應的內容。 redis-py包含兩個Parser類,PythonParser和HiredisParser。默認,如果已經安裝了hiredis模組,redis-py會使用HiredisParser,否則會使用PythonParser。
HiredisParser是C編寫的,由redis核心團隊維護,效能要比PythonParser提高10倍以上,所以建議使用。安裝方法,使用easy_install:
[root@njdyw ~]# easy_install2.7.3 hiredis
總計408
-rw---r -- 1 root root 239 03-21 10:45 easy-install.pth
-rw-r--r-- 1 root root 119 03-21 10:07 README-- 1 root root 60401 03-21 10:45redis-2.7.2-py2.7.egg
-rw-r--r-- 1 root root 332125 03-21 10:12 setuptools-0.py6c1. egg
-rw-r--r-- 1 root root 30 03-21 10:12 setuptools.pth
可以看到redis-2.7.2-py2.7.7.2-py2.7.7.2-py2.7.2-py2.7.7.2-py2.7.7.2-py2
3.測試連接[root@njdyw site-packages]#python2.7.3Python 2.7.3 (default, Mar 21 2013, 10:06:48)GC Hat 4.1.2-50)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>>import redisient>>卷host='127.0.0.1',port=6379,db=0)>>> redisClient.set('test_redis','Hello Python')True>>> value=testTrue
>>> value=redis ')
>>> print value
Hello Python
['RESPONSE_CALLBACKS', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__format__', '__hash__' '__reduce_ex__'、'__new__'、'__reduce__'、'__reduce_ex__'、'__repr__'、'__setattr__'、'__setitem__'、'__sizeof__'、'__str__'、'__subclasshook__'、'__weakref__'、'_zaggregate、app__' ', 'bgrewriteaof', 'bgsave', 'bitcount', 'bitop', 'blpop', 'brpop', 'brpoplpush', 'client_kill', 'client_list', 'config_get', 'config_set', 'connection_pool', 'dbsize'、'debug_object'、'decr'、'刪除'、'echo'、'eval'、'evalsha'、'execute_command'、'exists'、'expire'、'expireat'、'flushall'、'flushdb ', 'from_url', 'get', 'getbit', 'getrange', 'getset', 'hdel', 'hexists', 'hget', 'hgetall', 'hincrby', 'hincrbyfloat', 'hkeys', 'hlen'、'hmget'、'hmset'、'hset'、'hsetnx'、'hvals'、'incr'、'incrbyfloat'、'info'、'keys'、'lastsave'、'lindex'、'linsert ', 'llen', '鎖定', 'lpop', 'lpush', 'lpushx', 'lrange', 'lrem', 'lset', 'ltrim', 'mget', '移動', 'mset', 'msetnx'、'物件'、'parse_response'、'持久'、'pexpire'、'pexpireat'、'ping'、'管道'、'pttl'、'發布'、'pubsub'、'randomkey'、'register_script ', '重新命名', 'renamenx', 'response_callbacks', 'rpop', 'rpoplpush', 'rpush', 'rpushx', 'sadd', '保存', 'scard', 'script_exists', 'script_flush' , 'script_kill'、'script_load'、'sdiff'、'sdiffstore'、'set'、'set_response_callback'、'setbit'、'setex'、'setnx'、'setrange'、'關閉'、'sinter'、' sinterstore '、'sismember'、'slaveof'、'smembers'、'smove'、'排序'、'spop'、'srandmember'、'srem'、'strlen'、'substr'、'sunion'、'sunionstore' , '時間', '交易', 'ttl', '類型', '取消觀看', '觀看', 'zadd', 'zcard', 'zcount', 'zincrby', 'zinterstore', 'zrange', 'zrangebyscore', 'zrank', 'zrem', 'zremrangebyrank', 'zremrangebyscore', 'zrevrange', 'zrevrangebyscore', 'zrevrank', 'zscore', 'zunionstore']
>revrank', 'zscore', 'zunionstore']
> 4.實例測試:
wocns # wolysopen111 # wowolys@21.com
珊瑚珊珊#601601601#zss1984@126.compengfeihuchao#woaidami#294522652@qq.comsimulategirl#@#$9608125#.com ushigang_ 123@16 3.comsirenxing424 # tfiloveyou #sirenxing424 @126.com
raininglxy #1901061139#lixinyu23@qq.com
leochenlei#leichenleiii3@qq.com
leochenlei#leichenleiii3@qq.com
leochenlei#leichenleiiii3@qq.com
leochenlei#leichenleiiii3@qq.com
leochenlei#leichenleiiialelei120138.com 433835@qq .com
--創建腳本腳本
[ root@njdyw ~]#cat imp_red.py
導入redis
導入re
pool = redis.ConnectionPool(host='127.0.0.1', port=6379)Pool(host='127.0.0.1', port=6379) port=6379)
. (connection_pool =pool) pipe = r.pipeline()p=re.compile(r'(.*)s#s(.*)s#s(.*)');pipe = r .pipeline ()f = open("data.txt")matchs=p.findall(f.read())對於配對中的使用者: key='users_%s' %user[%s' %user[ 0] .strip() pipeline.hset(key,'pwd',user[1].strip()).hset(key,'email',user[2].strip())pipe.execute () f。 close()
[root@njdyw ~]# python2.7.3 imp_red.py
[root@njdyw ~]# python2.7.3 imp_red.py
1) "users_xiaochuan2018"
2) "201%
4) “用戶_daisypp」5)「users_boiny」6)「users_raininglxy」7)「users_fennal」8)「users_abc654468252」068252」468252」468252」468252」468252」468252」468252」468252」468252」468252」468252」468252」468252! xl2008 11"11) 「users_baby19881018」12)「users_darksoul0929」13)「users_pengcfwxh」14)「users_alex126126」15)「users_jiongjiongmao」
16)「users_sirenxing424」
17)「users_mengjie007」
18)「us」_cers_mengjie007」
18)「us使用者_licaijun007"
21) "users_ai3Min2"
22)“users_bokil”
23)“users_z370433835”
24)“users_yiling1007”
25)“users_simulate” s_baoautumn”
28)「user_黃大橋」
29)「users_q1718334567」
30)「users_xldq_l」
31)「users_beibeilong012」
31)「users_beibeilong012」32)「3個」34)“user_jacksbalu”
35)“ user_wolys ”
36)“users_kangte1”
37)“users_demonhaodh”
38)“users_ysdz8”
39)“users_leochenlei” 1)「users_peng飛虎潮」
redis 127.0.0.1: 6379>
redis 127.0.0.1:6379>hget users_pengfeihuchao 郵箱
"294522652@qq.com"
"294522652@qq.com"
"linlixian200606 @126.com”
熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Redis集群模式通過分片將Redis實例部署到多個服務器,提高可擴展性和可用性。搭建步驟如下:創建奇數個Redis實例,端口不同;創建3個sentinel實例,監控Redis實例並進行故障轉移;配置sentinel配置文件,添加監控Redis實例信息和故障轉移設置;配置Redis實例配置文件,啟用集群模式並指定集群信息文件路徑;創建nodes.conf文件,包含各Redis實例的信息;啟動集群,執行create命令創建集群並指定副本數量;登錄集群執行CLUSTER INFO命令驗證集群狀態;使

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

要從 Redis 讀取隊列,需要獲取隊列名稱、使用 LPOP 命令讀取元素,並處理空隊列。具體步驟如下:獲取隊列名稱:以 "queue:" 前綴命名,如 "queue:my-queue"。使用 LPOP 命令:從隊列頭部彈出元素並返回其值,如 LPOP queue:my-queue。處理空隊列:如果隊列為空,LPOP 返回 nil,可先檢查隊列是否存在再讀取元素。

如何清空 Redis 數據:使用 FLUSHALL 命令清除所有鍵值。使用 FLUSHDB 命令清除當前選定數據庫的鍵值。使用 SELECT 切換數據庫,再使用 FLUSHDB 清除多個數據庫。使用 DEL 命令刪除特定鍵。使用 redis-cli 工具清空數據。

Redis計數器是一種使用Redis鍵值對存儲來實現計數操作的機制,包含以下步驟:創建計數器鍵、增加計數、減少計數、重置計數和獲取計數。 Redis計數器的優勢包括速度快、高並發、持久性和簡單易用。它可用於用戶訪問計數、實時指標跟踪、遊戲分數和排名以及訂單處理計數等場景。

使用 Redis 命令行工具 (redis-cli) 可通過以下步驟管理和操作 Redis:連接到服務器,指定地址和端口。使用命令名稱和參數向服務器發送命令。使用 HELP 命令查看特定命令的幫助信息。使用 QUIT 命令退出命令行工具。

Redis 集群中使用 zset:zset 是一種有序集合,將元素與評分關聯。分片策略: a. 哈希分片:根據 zset 鍵的哈希值分佈。 b. 範圍分片:根據元素評分劃分為範圍,並將每個範圍分配給不同的節點。讀寫操作: a. 讀操作:如果 zset 鍵屬於當前節點的分片,則在本地處理;否則,路由到相應的分片。 b. 寫入操作:始終路由到持有 zset 鍵的分片。

Redis數據過期策略有兩種:定期刪除:定期掃描刪除過期鍵,可通過 expired-time-cap-remove-count、expired-time-cap-remove-delay 參數設置。惰性刪除:僅在讀取或寫入鍵時檢查刪除過期鍵,可通過 lazyfree-lazy-eviction、lazyfree-lazy-expire、lazyfree-lazy-user-del 參數設置。
