Home > Backend Development > PHP Tutorial > PHP and REDIS: How to achieve persistent storage of data

PHP and REDIS: How to achieve persistent storage of data

WBOY
Release: 2023-07-21 12:48:01
Original
1399 people have browsed it

PHP and REDIS: How to achieve persistent storage of data

Introduction:
In the process of web development, we often need to persistently store data. As a high-performance key-value storage system, Redis can meet our needs well. This article will introduce how to use PHP and Redis to achieve persistent storage of data.

1. Redis installation and configuration

  1. Download and install Redis:
    First, you need to download and install Redis from the Redis official website. The specific installation steps will not be described here.
  2. Configuring Redis:
    After the installation is completed, you need to enter the Redis configuration file (redis.conf) to perform some basic configurations.

    a) Open the redis.conf file

    vim /etc/redis/redis.conf
    Copy after login

    b) Configure the Redis password
    Find and modify the "requirepass" parameter and set it to the password you want:

    requirepass your_password
    Copy after login

    c) Save changes and close file.

  3. Start Redis:
    Enter the following command in the terminal to start Redis:

    redis-server
    Copy after login

2. Use PHP to connect to the Redis server
Next, you need to use PHP to connect to the Redis server and read and store data in the code.

  1. Install Redis extension:
    To use Redis in PHP, you need to install the Redis extension first. It can be installed through pecl or source code compilation.
  2. Connecting to the Redis server:
    First, you need to use the Redis class in the code to instantiate the Redis object and connect to the Redis server:

    $redis = new Redis();
    $redis->connect('127.0.0.1', 6379);
    $redis->auth('your_password'); // 如果设置了Redis密码,需要进行身份验证
    Copy after login
  3. Storing data to Redis:
    In Redis, we can use the set method to store data into the specified key. The following is a sample code:

    $redis->set('my_key', 'my_value');
    Copy after login
  4. Reading data from Redis:
    You can use the get method to read the value of the specified key from Redis. The following is a sample code:

    $value = $redis->get('my_key');
    echo $value; // 输出:my_value
    Copy after login
  5. Storing complex data types:
    Redis can not only store simple string values, but also complex data types, such as arrays, objects, etc. . We can use the serialize function to serialize complex data types into strings and then store them in Redis. The sample code is as follows:

    $data = ['name' => 'John', 'age' => 25];
    $redis->set('my_data', serialize($data));
    Copy after login

    When you need to retrieve data, you can use the unserialize function to deserialize the stored string into the original data type. The sample code is as follows:

    $data = unserialize($redis->get('my_data'));
    print_r($data); // 输出:Array ( [name] => John [age] => 25 )
    Copy after login

3. Implement persistent storage of data
The above description is to store data in Redis and read it out in the code. But when the Redis server is shut down or restarted, the data will be lost. In order to achieve persistent storage of data, we can use Redis's RDB and AOF mechanisms.

  1. RDB persistence (snapshot persistence):
    RDB persistence is the default persistence mechanism of Redis. It generates snapshot files based on certain time intervals and saves the data to disk in binary format. On a Redis restart, data can be restored from the snapshot file.

    a) Configure RDB persistence:
    Open the redis.conf file and find the following configuration:

    save 900 1
    save 300 10
    save 60 10000
    Copy after login
    Copy after login

    These configurations mean: after 900 seconds, if at least 1 key occurs After 300 seconds, if at least 10 keys have changed, an RDB file will be generated; after 60 seconds, if at least 10,000 keys have changed, an RDB file will be generated.

    b) Enable RDB persistence:
    Remove the comment symbols from the following configuration items to enable RDB persistence:

    save 900 1
    save 300 10
    save 60 10000
    Copy after login
    Copy after login

    c) Save and close the configuration file.

    d) Restart the Redis server.

    RDB persistence can save Redis' memory data to disk, but after Redis is restarted, it can only be restored to the state of the last generated RDB file.

  2. AOF persistence (append file persistence):
    AOF persistence will append each write command received by the Redis server to the end of the AOF file. When the Redis server restarts, it re-executes all write commands in the AOF file, thereby restoring the data.

    a) Configure AOF persistence:
    Open the redis.conf file and find the following configuration:

    appendonly no
    appendfilename "appendonly.aof"
    Copy after login

    Modify the value of the appendonly configuration item to yes, you can enable AOF persistence.

    b) Save and close the configuration file.

    c) Restart the Redis server.

Conclusion:
This article introduces how to use PHP and Redis to achieve persistent storage of data. By configuring Redis's RDB and AOF persistence mechanisms, you can ensure that the data is still available after the Redis server is restarted. At the same time, through the explanation of sample code, readers can have a clearer understanding of how to use PHP and Redis for data storage in actual projects.

References:

  • Redis official website: https://redis.io/
  • Redis source code: https://github.com/redis/redis

The above is the detailed content of PHP and REDIS: How to achieve persistent storage of data. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template