Skip to content

客户端

python 客户端 redis-py

https://github.com/andymccurdy/redis-py

1
In [1]: from redis import Redis
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import json
from redis import StrictRedis

# for db in range(2):
#     redis = StrictRedis(host="0.0.0.0", port=6381, db=db)
#     redis.flushdb()

redis = StrictRedis(host="0.0.0.0", port=6380, db=1)

def du(data):
    return json.dumps(data, ensure_ascii=False).encode()

for i in itr:
    redis.hset("lalala", du(i['code']), du(i['vol']))

aioredis

https://github.com/aio-libs/aioredis-py

redis-rs

https://docs.rs/redis/0.15.1/redis/

https://github.com/mitsuhiko/redis-rs

简单使用

1
2
3
serde = { version = "1.0.118", features = ["derive"] }
serde_json = "1.0.60"
redis = "0.18.0"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use serde::{Deserialize, Serialize};
use redis::{self, Commands};

#[derive(Clone, Debug, Serialize, Deserialize)]
struct People {
    pub name: String,
    pub age: u32,
}

fn main() {
    let client = redis::Client::open("redis://127.0.0.1:6380").unwrap();
    let mut con = client.get_connection().unwrap();

    let _: () =  con.set("the_key", 42).unwrap();
    let people = People {
        name: "zyz".to_string(),
        age: 18,
    };
    let _: () = con.set(
        "key1",
        serde_json::to_string(&people).unwrap(),
    ).unwrap();
}

使用 python 读取

1
2
3
4
5
6
import json
from redis import StrictRedis

redis = StrictRedis(host="0.0.0.0", port=6380, db=0)
res = json.loads(redis.get("key1").decode())
print(type(res), res)

输出结果:

1
<class 'dict'> {'name': 'zyz', 'age': 18}