Redis & PHP Simple Memorandum

Keywords: PHP Redis Mac github

The following are the Redis installation, basic operation of Redis client, PHP-Redis extension and basic interface memorandum of Redis in PHP. The local environment is Mac.

Redis installation

First, you need to download Redis, visit the following website, and select the Stable version to download:

https://redis.io/download

Enter the decompressed folder and execute the compilation:

make

Following implementation:

make install

This step will put Redis commands such as redis-server, redis-cli, etc. into / usr/local/bin, which is equivalent to adding environment variables.

Redis Basic Operations

Redis Server Start

Redis is divided into client side and server side. First, we need to start the server side and execute:

redis-server

At this point, the command line will stop in the Redis service startup prompt, indicating that the Redis server is now running and can be aborted using Ctrl + Z.

However, we prefer Redis to run in the background as a service, requiring modifications to the redis.conf file:

cd /your/path/to/redis/redis.conf

Modify the daemonize value of 128 rows to yes, and the number of rows may vary depending on the version:

daemonize no  =>  daemonize yes

Then restart Redis:

redis-server /your/path/to/redis/redis.conf

Note that even if redis.conf is modified, you need to add this parameter after redis-server, otherwise it will still start with the default configuration. Here redis.conf serves only to provide configuration file templates.

After that, you can view the currently running process to check the start-up of the Redis service:

ps aux|grep redis-server

Stop service can be executed:

redis-cli shutdown

Redis client

Execute the following instructions to enter the client:

redis-cli

When we modify the default port number or need to connect to a remote location, we can use:

redis-cli -h 127.0.0.7 -p 6379

Redis data type

There are five data types in Redis:

  1. String: In the form of key-value pairs, the key is a string type and the value can be a string or a number.

  2. list: insert or delete at both ends to simulate queues and stacks;

  3. Set: set, the values of different elements are different;

  4. hash: A set of key-value pairs can be indexed by keys, which can be analogous to two-dimensional associative arrays.

  5. zset: An ordered set, organized as a score, value, and sorted according to the score value;

Redis Client Basic Operations

The following simple instructions are memorized, and other commands can be consulted:

https://redis.io/commands

The following instructions need to be executed after entering redis-cli:

1. string

get key1                // Gets the value corresponding to the key1 key and outputs nil if it does not exist
set key1 value1         // Set key1 => value1 key-value pairs
set key2 123            // Set key2 to 123
incr key2               // Increase the value of key2 by 1
decr key2               // Reduce the value of key2 by 1
incrby key2 10          // The corresponding value of key2 plus 10
decrby key2 10          // The corresponding value of key2 is subtracted by 10

2. list

lpush listA A           // Add A to the left of the list A queue
rpush listA B           // Add B to the right of the list A queue
lpop listA              // The first value pops up from the left side of the list A queue
rpop listA              // Pop the first value to the right of the list A queue
llen listA              // Get the length of listA

3. set

sadd setA A             // Add A to the set A
scard setA              // Get the length in the set A
sismember setA A        // Determine whether A is an element in a set A
srem setA A             // Remove element A from setA collection

4. hash

hset hashA key1 A       // Add key value pair key1 = > A to hashA
hget hashA key1         // Get the corresponding value of key1 in hashA
hlen hashA              // Obtain the logarithm of key values contained in hashA
hmget hashA key1 key2   // Get the values corresponding to multiple keys in hashA

5. zset

zadd zsetA 10.1 val1    //  val1 with score of 10.1 is added to zsetA
zrange zsetA 0 4        // Get elements 0 through 4 in zsetA from small to large

// Get the elements in zsetA from small to large in order 0 to 4, with their corresponding score attached to the output
zrange zsetA 0 4 withscores 

Note that the ordering in zset is done in ascending order from small to large, with 0 being the minimum. When multiple elements have the same score, they are sorted according to the dictionary order of their value s.

PHP-Redis Extension

We use phpize and configure for extended installation. We can check whether phpize has been installed by phpize-v. If not, we can install phpize in the following way:

yum install php-devel

Download the Extension Pack

Download the appropriate extended source packages as needed:

PHP5: https://codeload.github.com/phpredis/phpredis/zip/develop
PHP7: https://codeload.github.com/phpredis/phpredis/zip/php7

Compiler extension

After decompression, enter the folder and execute:

phpize

Then execute:

./configure -with-php-config=/your/path/to/php/bin/php-config

The latter path causes compiled redis.so to be placed in the directory specified by the extension in subsequent make install, eliminating the need to move manually.

Following implementation:

make

And:

make install

Students using MAMP can refer to: installing php extensions in MAMP environment when compiling problems

Adding Extension Configuration

Finally, you need to add an extended configuration to the configuration file to view the path of the php.ini file using the following instructions:

php --ini

Then edit the php.ini file and add:

extension=redis.so

You can see the Redis extension in echo phpinfo(); at this point.

Note: If you did not use the - with-php-config parameter before, you need to move the generated modules/redis.so into the extended directory after make install, which can be seen in php.ini:

extension_dir = /path/to/extensions/

Using Redis in PHP

The following lists only the basic usage, see the PHP-Redis documentation for more details

Redis is used in PHP in the same way as redis-cli. Note that redis-server needs to be run beforehand.

Connect Redis

$redis = new Redis();
$redis->connect("localhost");

string

$redis->set("key1", "value1");  // Setting key-value pairs
$redis->get("key1");            // Get value

$redis->set("number1", 123);
$redis->incr("number1");        // Self increment
$redis->decrBy("number1", 100); // subtraction
$redis->get("number1");

list

$redis->lPush("listA", "A");   // Add values to the left
$redis->lPush("listA", "B");   
$redis->lPush("listA", "C");

// Pop-up values from the right to simulate queues
$redis->rPop("listA");  

set

$redis->sAdd("setA", "A");
$redis->sAdd("setA", "B");
$redis->sAdd("setA", "C");
$redis->sAdd("setA", "A");
$redis->sCard("setA");
$redis->sMembers("setA");  // All elements of the output set

hash

$redis->hSet("hashA", "name", "iname");
$redis->hSet("hashA", "age", "age");

// Setting multiple values at the same time
$redis->hMset("hashA", [
    "gender" => "male",
    "salary" => 12000
]);
$redis->hGet("hashA", "salary");

// Get multiple values
$redis->hMGet("hashA", ["name", "gender"]);

zset

$redis->zAdd("ZSetA", 12, "A");
$redis->zAdd("ZSetA", 10.3, "B");
$redis->zAdd("ZSetA", 11.9, "C");
$redis->zAdd("ZSetA", 9.99, "D");
$redis->zRange("ZSetA", 0, 2);

// Output all values
$redis->zRange("ZSetA", 0, -1);

// Descending output from large to small
$redis->zRevRange("ZSetA", 0, -1);

Reference resources

  1. Use scenarios for five data types of redis - CSDN

  2. redis - Install and Open and Close - CSDN

  3. Method of installing PHP extension with MAMP PRO

  4. Installing php extension - CSDN in MAMP environment

  5. Redis Installation and PHP Extension under Linux (PHP 7 Applicable) - CSDN

  6. Installation of Redis in Mac Environment

Posted by JVassie on Mon, 15 Apr 2019 17:45:32 -0700