Redis與Python的互動

來源:互聯網
上載者:User

標籤:互動   redis   服務   pytho   響應   als   col   delete   資料   

# 1.通過python對Redis資料進行增刪改查,擷取鍵1.增    from redis import *    if __name__ == "__main__":        try:            # 建立StrictRedis對象,與redis伺服器建立連結            sr = StrictRedis()            # 添加鍵py1,值為gj            result = sr.set(‘py1‘, ‘gj‘)            # 輸出響應結果,如果添加成功則返回True,否則返回false            print(result)        except Exception as e:            print(e)2.刪    from redis import *    if __name__ == "__main__":        try:            # 建立StrictRedis對象,與redis伺服器建立連結            sr = StrictRedis()            # 設定鍵py1的值,如果鍵已經存在則進行修改,如果鍵不存在則進行添加            result = sr.delete(‘py1‘)            # 輸出相應結果,如果刪除成功則返回受影響的鍵數,否則則返回0            print(result)        except Exception as e:            print(e)3.改    from redis import *    if __name__ == "__main__":        try:            # 建立StrictRedis對象,與redis伺服器建立連結            sr = StrictRedis()            # 設定py1的值,如果鍵已經存在則進行修改,如果鍵不存在則進行添加            result = sr.set(‘py1‘, ‘hr‘)            # 輸出相應結果,如果操作成功則返回True,否則返回False            print(result)        except Exception as e:            print(e)4.查    from redis import *    if __name__ == "__main__":        try:            # 建立StrictRedis對象,與redis伺服器建立連結            sr = StrictRedis()            # 擷取鍵py1的值            result = sr.get(‘py1‘)            # 輸出鍵的值,如果鍵不存在則返回None            print(result)        except Exception as e:            print(e)5.擷取鍵    from redis import *    if __name__ == "__main__":        try:            # 建立StrictRedis對象,與redis伺服器建立連結            sr = StrictRedis()            # 擷取所有的鍵            result = sr.keys()            # 輸出響應結果,所有的鍵構成一個列表,如果沒有鍵分返回空列表            print(result)        except Exception as e:            print(e)# 2.不同類型對應的執行個體方法    --string:set setex mset append get mget    --key:keys exists type delete expire getrange ttl    --hash:hset hmset hkeys hget hmget hvals hdel    --list:lpush rpush linsert lrange lset lrem    --set:sadd smembers srem    --zset:zadd zrange zrangebyscore zscore zrem zremrangebyscore

 

Redis與Python的互動

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.