当前位置: 首页 > 工具软件 > ReJSON > 使用案例 >

python使用rejson+redis存储json

袁枫涟
2023-12-01

python使用rejson+redis存储json

python安装rejson

pip3 install rejson

python代码

#!/bin/python3
# -*- coding=utf-8 -*-
import rejson

# 连接redis
rj = rejson.Client(host='localhost', port=6379, decode_responses=True)

def run():
    obj={}
    obj['a']="1"
    obj['b']="2"
    # 写入。返回值a为True时写成功
    a = rj.jsonset('obj', '.', obj)
        
    # 读取。b非None时读取到内容
    b = rj.jsonget('obj', '.')
    print(b)
    return 

if __name__ == '__main__':
    run()

参数

class Client(encoder=None, decoder=None, host: Text=..., port: int=..., 
db: int=..., password: Optional[Text]=..., socket_timeout: Optional[float]=..., 
socket_connect_timeout: Optional[float]=..., socket_keepalive: Optional[bool]=..., 
socket_keepalive_options: Optional[Mapping[str, Union[int, str]]]=..., 
connection_pool: Optional[ConnectionPool]=..., unix_socket_path: Optional[Text]=..., 
encoding: Text=..., encoding_errors: Text=..., charset: Optional[Text]=..., 
errors: Optional[Text]=..., decode_responses: bool=..., retry_on_timeout: bool=...,
 ssl: bool=..., ssl_keyfile: Optional[Text]=..., ssl_certfile: Optional[Text]=..., 
 ssl_cert_reqs: Optional[Union[str, int]]=..., ssl_ca_certs: Optional[Text]=..., 
 max_connections: Optional[int]=...,)

常用参数简单说明:

host redis地址
port redis端口
decode_responses=True 返回字符串

参考:
https://pypi.org/project/rejson/

 类似资料: