1. Introduction
Redis is an open-source, advanced key-value pair storage system that is often used as a data structure server because of its support for string, Hash, list, set, and ordered collections. Before using Redis in Laravel, you need to install the Predis/predis package (~1.0) through composer.
Configuration
The Redis configuration for your app is located in profile config/database.php. In this file, you can see the Redis array that contains the Redis server used by the app:
' Redis ' = [ ' cluster ' + false, ' default ' = = ' host ' = ' 127.0.0.1 ', ' port ' = > 6379, ' database ' = 0, ],
The default server configuration can meet development needs, however, you can modify the array based on the environment, just give each Redis server a name and specify the hosts and interfaces that the Redis server uses.
The cluster option tells the Laravel Redis client to perform client-side sharding across multiple redis nodes, forming a pool of nodes and creating a large amount of valid RAM. However, client shards do not handle failover, so it is well suited to obtain valid cache data from another primary data store.
In addition, you can define the options array values in the Redis connection definition, allowing you to specify a range of Predis client options.
If the Redis server requires authentication information, you can provide the password by adding the password configuration item to the Redis server configuration array.
Note: If you install the Redis extension for PHP through PECL, you need to modify the Redis alias in the config/app.php file.
2. Basic use
You can interact with Redis by invoking multiple methods on the Redis façade, which supports dynamic methods, so you can have any Redis command that will be passed directly to Redis, in this case, by calling the Redis façade Get method to invoke the Get command on Redis:
$user]);} }
Of course, as mentioned above, any Redis command can be called on the Redis façade. Laravel uses the Magic method to pass commands to the Redis server, so simply pass parameters and Redis commands as follows:
Redis::set (' name ', ' Taylor '); $values = Redis::lrange (' names ', 5, 10);
You can also use the command method to pass commands to the server, which receives the command name as the first parameter and an array of parameter values as the second parameter:
$values = Redis::command (' Lrange ', [' name ', 5, 10]);
Using multiple Redis connections
You can get Redis instances by calling the Redis::connection method:
$redis = Redis::connection ();
This will get the default Redis server instance, and if you are not using a server cluster, you can pass the server name to the connection method to get the specified server as defined in the Redis configuration specified:
$redis = redis::connection (' other ');
Pipeline command
When you need to send multiple commands to the server in one operation, you should use a pipeline, and the pipeline method receives a parameter: Receives the closure of the Redis instance. You can send all Redis commands to this Redis instance, and these commands are executed in one operation:
Redis::p ipeline (function ($pipe) { for ($i = 0; $i <; $i + +) { $pipe->set ("Key: $i", $i); });
3. Publish/Subscribe
Redis also provides an interface for calling Redis's publish and subscribe commands. These Redis commands allow you to listen to messages on a given "channel", you can post messages from another app to this channel, and even use other programming languages, allowing you to easily communicate between different apps/processes.
First, let's use the Subscribe method to set up listeners on a channel through Redis. Since calling the Subscribe method will open a resident process, we will call the method in the artisan command:
Now we can use publish to post messages to this channel:
Route::get (' Publish ', function () { //routing logic ... Redis::p ublish (' Test-channel ', Json_encode ([' foo ' = ' bar ')]);
Wildcard Subscription
Using the Psubscribe method, you can subscribe to a wildcard-defined channel, which is useful for getting all the messages on all the corresponding channels. The $channel name is passed as the second argument to the provided callback closure:
Redis::p Subscribe ([' * '], function ($message, $channel) { echo $message;}); Redis::p Subscribe ([' users.* '), function ($message, $channel) { echo $message;});