企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
使用 Python 的`redis`库操作 Redis 1. 字符串(String): 存储简单的键值对 ``` import redis r = redis.Redis(host='localhost', port=6379, db=0) r.set('key', 'value') print(r.get('key')) ``` 2. 哈希表(Hash): 存储键值对集合 ~~~python import redis r = redis.Redis(host='localhost', port=6379, db=0) r.hset('hash_key', 'field1', 'value1') print(r.hget('hash_key', 'field1')) ~~~ 3. 列表(List): 存储有序的字符串列表 ~~~python import redis r = redis.Redis(host='localhost', port=6379, db=0) r.lpush('list_key', 'value1') print(r.lrange('list_key', 0, -1)) ~~~ 4. 集合(Set): 存储唯一字符串元素 ~~~python import redis r = redis.Redis(host='localhost', port=6379, db=0) r.sadd('set_key', 'member1') print(r.smembers('set_key')) ~~~ 5. 有序集合(Sorted Set): 存储带分数的唯一字符串 ~~~python import redis r = redis.Redis(host='localhost', port=6379, db=0) r.zadd('zset_key', {'member1': 1}) print(r.zrange('zset_key', 0, -1, withscores=True)) ~~~