標籤:ebs get for exe pubsub member site members import
安裝python-redis
pip install redis
python操作redis
#從redis包中匯入Redis類from redis import Redis#初始化redis執行個體cache = Redis(host=‘10.2.2.120‘, port=‘6379‘)#操作字串cache.set(‘username‘, ‘abc‘)cache.delete(‘username‘)#列表操作cache.lpush(‘books‘, ‘java‘)cache.lpush(‘books‘, ‘python‘)cache.lpush(‘books‘, ‘php‘)print(cache.lrange(‘books‘, 0, -1))#集合的操作cache.sadd(‘team‘, ‘blue‘)cache.sadd(‘team‘, ‘yellow‘)cache.sadd(‘team‘, ‘red‘)print(cache.smembers(‘team‘))#雜湊的操作cache.hset(‘website‘, ‘baidu‘, ‘www.baidu.com‘)cache.hset(‘website‘, ‘google‘, ‘www.google.com‘)print(cache.hgetall(‘website‘))#事務的操作pip = cache.pipeline()pip.set(‘usernmae‘, ‘heboan‘)pip.set(‘password‘, ‘123456‘)pip.execute()#發布與訂閱(發布訂閱要在不同的檔案)#訂閱訊息ps = cache.pubsub()ps.subscribe(‘email‘)while True: for item in ps.listen(): print(item) #發布訊息for x in range(3): cache.publish(‘email‘, ‘[email protected]‘)
這裡只是列出了一些基本的操作,其實和命令列是一樣的
Python操作redis