Detailed explanation of Redis database operation (Python description)

Table of contents

Redis overview

Redis data types

Persistence of Redis

Redis publish subscribe

Redis transactions

Redis performance optimization

Python operation string (String)

Python operation hash table (Hash)

Python operation list (List) 

Python operation set (Set)

Python operation ordered collection (ZSet)

pipeline object

Redis database connection pool

Appendix C++ hiredis library (Life is too short)


Redis overview

Edis is an open source in-memory database that supports functions such as key-value pair storage, publish-subscribe, and persistence. Here are some basics about Redis:

Redis data types

Redis supports five basic data types, namely string (String), hash (Hash), list (List), set (Set) and ordered set (ZSet). These data types can be used in different scenarios, such as strings can be used for caches, counters, etc., hashes can be used to store objects, lists can be used for queues, etc.

Persistence of Redis

Redis supports two persistence methods, RDB and AOF. RDB is a snapshot persistence method, which saves Redis data to the hard disk as a snapshot, and AOF saves Redis operations to a file in an appended manner. These two methods have their own advantages and disadvantages, and you can choose to use them according to the actual situation.

Redis publish subscribe

Redis supports publish-subscribe mode, which can receive messages in a channel by subscribing to a channel. This function can be used to implement real-time message push, event notification and other scenarios.

Redis transactions

Redis supports transactions, which can package multiple operations into an atomic operation for execution. If one of the operations fails, all operations will be rolled back. This function can be used to ensure the consistency of multiple operations and avoid data inconsistency caused by unexpected situations in the middle.

Redis performance optimization

Redis is a high-performance database that can further improve performance through some methods, such as using pipelines to perform operations in batches, using connection pools to reduce connection establishment and closing, setting appropriate memory limits, and so on.

Python operation string (String)

The following are some commonly used Redis string type APIs:

  • set(name, value, ex=None, px=None, nx=False, xx=False): Set the value of the specified key.
  • get(name): Get the value of the specified key.
  • mset(mapping): Set multiple key-value pairs in batches.
  • mget(keys, *args): Get multiple key-value pairs in batches.
  • incr(name, amount=1): Increment the value of the specified key by the specified amount.
  • decr(name, amount=1): Decrements the value of the specified key by the specified amount.
  • append(name, value): Appends the value to the value of the specified key.
  • strlen(name): Get the length of the value of the specified key.

The following is an example that demonstrates how to use the Redis string type API to store, read, and modify strings:

import redis

# 创建 Redis 客户端
r = redis.Redis(host='localhost', port=6379, db=0)

# 设置键值对
r.set('mykey', 'Hello World')

# 获取键值对
value = r.get('mykey')
print(value)  # 输出 b'Hello World'

# 批量设置多个键值对
r.mset({'key1': 'value1', 'key2': 'value2', 'key3': 'value3'})

# 批量获取多个键值对
values = r.mget('key1', 'key2', 'key3')
print(values)  # 输出 [b'value1', b'value2', b'value3']

# 自增键的值
r.incr('counter', amount=2)
value = r.get('counter')
print(value)  # 输出 b'2'

# 自减键的值
r.decr('counter', amount=1)
value = r.get('counter')
print(value)  # 输出 b'1'

# 追加字符串到键的值后面
r.append('mykey', ' Redis')
value = r.get('mykey')
print(value)  # 输出 b'Hello World Redis'

# 获取键的值的长度
length = r.strlen('mykey')
print(length)  # 输出 18

Python operation hash table (Hash)

The following are some commonly used Redis hash type APIs:

  • hset(name, key, value): Set the value of the specified key in the hash table.
  • hget(name, key): Get the value of the specified key in the hash table.
  • hmset(name, mapping): Set multiple key-value pairs in the hash table in batches.
  • hmget(name, keys, *args): Get multiple key-value pairs in the hash table in batches.
  • hgetall(name): Get all key-value pairs in the hash table.
  • hincrby(name, key, amount=1): Increment the value of the specified key in the hash table by the specified amount.
  • hdel(name, *keys): Delete one or more specified keys in the hash table.
import redis

# 创建 Redis 客户端
r = redis.Redis(host='localhost', port=6379, db=0)

# 设置哈希表中的一个键值对
r.hset('myhash', 'field1', 'value1')

# 获取哈希表中的一个键值对
value = r.hget('myhash', 'field1')
print(value)  # 输出 b'value1'

# 批量设置哈希表中的多个键值对
r.hmset('myhash', {'field2': 'value2', 'field3': 'value3'})

# 批量获取哈希表中的多个键值对
values = r.hmget('myhash', 'field1', 'field2', 'field3')
print(values)  # 输出 [b'value1', b'value2', b'value3']

# 获取哈希表中所有的键值对
all_values = r.hgetall('myhash')
print(all_values)  # 输出 {b'field1': b'value1', b'field2': b'value2', b'field3': b'value3'}

# 将哈希表中指定键的值自增指定量
r.hincrby('myhash', 'field4', amount=3)
value = r.hget('myhash', 'field4')
print(value)  # 输出 b'3'

# 删除哈希表中的一个或多个指定键
r.hdel('myhash', 'field1', 'field2')

Python operation list (List) 

  • rpush(name, values): Add one or more elements to the right of the list.
  • lpush(name, values): Add one or more elements to the left of the list.
  • lrange(name, start, end): Returns the elements within the specified range in the list.
  • lpop(name): Remove and return the leftmost element of the list.
  • rpop(name): Remove and return the rightmost element of the list.
  • llen(name): Returns the length of the list.
  • lset(name, index, value): Sets the value of the element at the specified index in the list.
  • lrem(name, count, value): Removes the element with the specified value from the list.

The following is an example that demonstrates how to use the Redis list type API to store, read, and modify lists:

import redis

# 创建 Redis 客户端
r = redis.Redis(host='localhost', port=6379, db=0)

# 在列表的右侧添加一个元素
r.rpush('mylist', 'a')

# 在列表的左侧添加一个元素
r.lpush('mylist', 'b')

# 在列表的右侧添加多个元素
r.rpush('mylist', 'c', 'd', 'e')

# 返回列表中指定范围内的元素
values = r.lrange('mylist', 0, -1)
print(values)  # 输出 [b'b', b'a', b'c', b'd', b'e']

# 删除并返回列表最左侧的元素
value = r.lpop('mylist')
print(value)  # 输出 b'b'

# 删除并返回列表最右侧的元素
value = r.rpop('mylist')
print(value)  # 输出 b'e'

# 返回列表的长度
length = r.llen('mylist')
print(length)  # 输出 3

# 设置列表中指定索引的元素的值
r.lset('mylist', 1, 'new_value')

# 删除列表中所有指定值的元素
r.lrem('mylist', 0, 'c')

Python operation set (Set)

  • sadd(name, values): Add one or more elements to the collection.
  • srem(name, values): Remove one or more elements from the collection.
  • sismember(name, value): Checks if the specified element exists in the collection.
  • smembers(name): Returns all elements in the collection.
  • srandmember(name, count=None): Returns one or more random elements in the collection.
  • scard(name): Returns the number of elements in the collection.
  • sinter(keys, *args): Returns the intersection of multiple collections.
  • sunion(keys, *args): Returns the union of multiple collections.
  • sdiff(keys, *args): Returns the difference of multiple sets.
import redis

# 创建 Redis 客户端
r = redis.Redis(host='localhost', port=6379, db=0)

# 往集合中添加元素
r.sadd('myset', 'a', 'b', 'c')

# 从集合中删除元素
r.srem('myset', 'c')

# 检查指定元素是否存在于集合中
exists = r.sismember('myset', 'a')
print(exists)  # 输出 True

# 返回集合中的所有元素
values = r.smembers('myset')
print(values)  # 输出 {b'a', b'b'}

# 返回集合中一个或多个随机元素
random_values = r.srandmember('myset', 2)
print(random_values)  # 输出 [b'b', b'a']

# 返回集合的元素个数
count = r.scard('myset')
print(count)  # 输出 2

# 创建另一个集合
r.sadd('otherset', 'a', 'd')

# 返回多个集合的交集
inter = r.sinter(['myset', 'otherset'])
print(inter)  # 输出 {b'a'}

# 返回多个集合的并集
union = r.sunion(['myset', 'otherset'])
print(union)  # 输出 {b'a', b'b', b'd'}

# 返回多个集合的差集
diff = r.sdiff(['myset', 'otherset'])
print(diff)  # 输出 {b'b'}

Python operation ordered collection (ZSet)

  • zadd(name, mapping): Add one or more elements to the sorted collection.
  • zrem(name, values): Remove one or more elements from the sorted collection.
  • zscore(name, value): Returns the score of the specified element.
  • zrange(name, start, end, withscores=False): Returns the elements ranked between start and end in the sorted set.
  • zrevrange(name, start, end, withscores=False): Returns the elements whose score is between start and end in the sorted set, sorted by score from large to small.
  • zcard(name): Returns the number of elements in the sorted set.
  • zcount(name, min, max): Returns the number of elements in the sorted set whose score is between min and max.
  • zrank(name, value): Returns the rank of the specified element in the sorted set.
  • zrevrank(name, value): Returns the rank of the specified element in the sorted set, sorted by score from largest to smallest.
  • zincrby(name, amount, value): Increase the score of the specified element by amount.
  • zremrangebyrank(name, start, end): Deletes elements ranked between start and end in the sorted set.
  • zremrangebyscore(name, min, max): Delete the elements whose score is between min and max in the sorted set.
import redis

# 创建 Redis 客户端
r = redis.Redis(host='localhost', port=6379, db=0)

# 往有序集合中添加元素
r.zadd('myzset', {'a': 1, 'b': 2, 'c': 3})

# 返回有序集合中排名在 0 和 1 之间的元素
range_values = r.zrange('myzset', 0, 1, withscores=True)
print(range_values)  # 输出 [(b'a', 1.0), (b'b', 2.0)]

# 返回有序集合中分值在 1 和 2 之间的元素,按分值从大到小排序
revrange_values = r.zrevrange('myzset', 1, 2, withscores=True)
print(revrange_values)  # 输出 [(b'b', 2.0), (b'c', 3.0)]

# 返回指定元素的分值
score = r.zscore('myzset', 'a')
print(score)

pipeline object

In Redis, in order to reduce the number of network round trips and improve performance, we can use the Redis pipeline to package multiple Redis operations into one request and send it to the Redis server. The Redis server will return the results of all operations at one time.

In Python, you can use Redis's pipeline to perform multiple Redis operations, such as insert, delete, modify, etc., without sending each operation to the Redis server. This can greatly reduce the number of network round trips and improve the performance of Redis.

The pipeline is usually used to perform a large number of similar Redis operations, such as insertion, deletion, modification, etc. These operations can be packaged into a pipeline and sent to the Redis server for execution at one time without waiting for the result of each operation.

In Python, you can use pipeline()the method to create a pipeline object, and then use pipelinethe object to perform multiple Redis operations. For example:

import redis

# 创建 Redis 客户端
r = redis.Redis(host='localhost', port=6379, db=0)

# 创建 pipeline 对象
pipe = r.pipeline()

# 将多个 Redis 操作添加到 pipeline 中
pipe.set('mykey', 'value1')
pipe.set('mykey2', 'value2')
pipe.get('mykey')
pipe.get('mykey2')

# 执行 pipeline 中的操作
result = pipe.execute()

# 输出操作结果
print(result)  # 输出 [True, True, b'value1', b'value2']

In the above example, we first created a pipeline object using pipeline()the method , and then added multiple Redis operations to the pipeline. Finally, use execute()the method to execute all operations in the pipeline and store the results of the operations in a list.

Redis database connection pool

In Python, when using a Redis client to connect to a Redis server, each connection requires steps such as establishing a TCP connection, authentication, and selecting a database. These steps will increase the load on the Redis server and reduce the performance of the client.

In order to reduce the overhead of each Redis operation, Python's Redis client provides a Connection Pool (connection pool) mechanism. Through the connection pool, the connection multiplexing of the Redis client can be realized, the overhead of connection establishment can be reduced, and the performance of the Redis client can be improved.

In Python, you can use redis.ConnectionPool()the method to create a connection pool, and then use redis.Redis(connection_pool=pool)the method to create a Redis client, passing the connection pool as a parameter.

import redis

# 创建一个连接池
pool = redis.ConnectionPool(host='localhost', port=6379, db=0)

# 创建一个 Redis 客户端,并将连接池作为参数传递
r = redis.Redis(connection_pool=pool)

# 使用 Redis 客户端执行操作
r.set('mykey', 'myvalue')
print(r.get('mykey'))

Appendix C++ hiredis library (Life is too short)

In C++, you can use the hiredis library to connect to the Redis server, which provides a C language API that corresponds to Redis commands one-to-one.

The steps to operate Redis using the hiredis library are roughly as follows:

  1. Create a Redis connection object
  2. Construct Redis command and send to Redis server
  3. Parse the response returned by the Redis server
  4. Release the Redis connection object
#include <iostream>
#include <hiredis/hiredis.h>

int main() {
    // 1. 创建 Redis 连接对象
    redisContext* redis_conn = redisConnect("localhost", 6379);
    if (redis_conn == nullptr || redis_conn->err) {
        if (redis_conn) {
            std::cout << "Error: " << redis_conn->errstr << std::endl;
            redisFree(redis_conn);
        } else {
            std::cout << "Error: Failed to allocate redis context" << std::endl;
        }
        return -1;
    }

    // 2. 构造 Redis 命令并发送给 Redis 服务器
    redisReply* redis_reply = nullptr;

    // 使用 RPUSH 命令向列表中添加元素
    redis_reply = static_cast<redisReply*>(redisCommand(redis_conn, "RPUSH mylist a b c"));
    if (redis_reply == nullptr) {
        std::cout << "Error: Failed to execute Redis command" << std::endl;
        redisFree(redis_conn);
        return -1;
    }

    // 使用 LLEN 命令获取列表长度
    redis_reply = static_cast<redisReply*>(redisCommand(redis_conn, "LLEN mylist"));
    if (redis_reply == nullptr) {
        std::cout << "Error: Failed to execute Redis command" << std::endl;
        redisFree(redis_conn);
        return -1;
    }
    std::cout << "List length: " << redis_reply->integer << std::endl;

    // 3. 解析 Redis 服务器返回的响应
    freeReplyObject(redis_reply);

    // 4. 释放 Redis 连接对象
    redisFree(redis_conn);

    return 0;
}

Life is too short and I use Python!!

Guess you like

Origin blog.csdn.net/weixin_40582034/article/details/129158645