標籤:互動 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的互動