Comprehensive guide to Redis. Part 5.

In this tutorial, we are going to try out a Redis client library for PHP. Unlike the Node.js client we used in the previous tutorials, the chosen PHP client library is synchronous and do not require a callback function.

We are going to use a client called Predis. The PHP version for all examples is 5.4. To install Predis we will use the PHP Composer.

Create a file called composer.json in the tutorial5 folder with the following content:

Then inside the tutorial5 folder run Composer via composer update. This command will install the Predis library.

Basic commands

Each executed command via Predis returns a value synchronously. Here is the sample code for some basic functions.

Create a file called index.php with the following code:

Then inside the tutorial folder run a PHP local server via php -S localhost:8080. Now open http://localhost:8080 in your browser. You should see the following:

img1

Blocking commands

There are 3 connection-blocking List commands: BRPOP, BLPOP, and BRPOPLPUSH. In the Node.js client, these commands expect a callback. In Predis, they don’t expect callbacks.

The BRPOP and BLPOP commands expect a list of keys and a timeout. BRPOPLPUSH expects a source key, a destination key, and a timeout. The timeout default value is zero. If the timeout is zero, the call will hang until an item is found. Redis client is blocked until there is at least one element in the List or until the timeout has been exceeded.

BRPOP: Blocking version of RPOP. An element is popped from the tail of the first List that is not empty.
BLPOP: Blocking version of LPOP. An element is popped from the head of the first List that is not empty.
BRPOPLPUSH: An element is popped from the tail of the source key and inserted at the head of the destination key.

Now modify the index.php file this way:

Run this code. You should see the following output:

img2

Pipelines

There 2 ways to work with pipelines in Predis:
1. A client executes a pipeline inside an anonymous function(similar to callbacks in Node.js, but not asynchronous).
2. A client returns a pipeline instance with the ability to chain commands.

Modify the index.php file this way:

You should see the following:

img3

Transactions

Predis provides an interface based on MULTI and EXEC with the ability to chain commands.

Modify the index.php file this way:

Output:

img4

Lua scripting

Predis provides a higher level abstraction to register commands as if they were native Redis commands. Internally, Predis uses the EVALSHA command. EVAL is used as a fallback if needed.

You need to create a PHP class that extends Predis\Command\ScriptCommand and implements 2 methods:
1. getKeyCount: Returns the number of arguments that should be considered as keys.
2. getScript: Returns the body of a Lua code.

In the following code, we define a class called MultiplyValue, where we create a multiply command. It will obtain the value of a key, multiply it by the argument, and update the key with the new value.

Modify the index.php file this way:

You should see the following output:

img5

That’s all for today.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">