[Laravel5.2 documentation] service-Redis

Source: Internet
Author: User
Tags redis server
[Laravel5.2 documentation] service-Redis 1. Introduction

Redis is an open-source and advanced key-value pair storage system. it is often used as a data structure server because it supports data structures such as string, Hash, list, set, and ordered set. Before using Redis in Laravel, you need to install the predis/predis package through Composer (~ 1.0 ).

Configuration

The application's Redis configuration is located in the configuration file config/database. php. In this file, you can see the Redis array containing the redis server used by the application:

'redis' => [    'cluster' => false,    'default' => [        'host'     => '127.0.0.1',        'port'     => 6379,        'database' => 0,    ],],

The default server configuration can meet the development needs. However, you can modify the array based on the environment. you only need to give each Redis server a name and specify the host and interface used by the Redis server.

The cluster option tells the Laravel Redis client to execute client sharding among multiple Redis nodes to form a node pool and create a large number of valid RAM resources. However, the client does not handle failover, so it is very suitable to obtain effective cache data from another primary data storage.

In addition, you can define the options array value in the Redis connection definition to allow you to specify a series of Predis client options.

If the Redis server requires authentication information, you can add the password configuration item to the Redis server configuration array to provide the password.

Note: If you install the Redis extension of PHP through PECL, you need to modify the alias of Redis in the config/app. PHP file.

2. Basic use

You can call multiple methods on the Redis facade to interact with Redis. this facade supports dynamic methods. Therefore, you can use any Redis command, which will be passed directly to Redis, in this example, we call the get command on Redis by calling the GET method on the Redis facade:

 $ User]) ;}}

Of course, as mentioned above, you can call any Redis command on the Redis facade. Laravel uses the magic method to pass commands to the Redis server. Therefore, you only need to pass the parameters and the Redis command as follows:

Redis::set('name', 'Taylor');$values = Redis::lrange('names', 5, 10);

In addition, you can use the command method to pass the command to the server. this method receives the command name as the first parameter, and the parameter value array as the second parameter:

$values = Redis::command('lrange', ['name', 5, 10]);

Use multiple Redis connections

You can obtain the Redis instance by calling the Redis: connection method:

$redis = Redis::connection();

This will obtain the default Redis server instance. if you do not use a server cluster, you can pass the server name to the connection method to obtain the specified server defined in the specified Redis configuration:

$redis = Redis::connection('other');

Pipeline command

When you need to send multiple commands to the server in one operation, you should use the pipeline. 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 will be executed in one operation:

Redis::pipeline(function ($pipe) {    for ($i = 0; $i < 1000; $i++) {        $pipe->set("key:$i", $i);    }});
3. publish/subscribe

Redis also provides interfaces to call Redis's publish and subscribe commands. These Redis commands allow you to listen to messages in a given channel. you can publish messages from another application to this channel, or even use other programming languages, this allows you to easily communicate with different applications/processes.

First, let's use the subscribe method to set the listener on a channel through Redis. Since calling the subscribe method will start a resident process, we will call this method in the Artisan command:

     

Now we can use publish to publish messages to this channel:

Route: get ('Publish ', function () {// routing logic... redis: publish ('test-channel', json_encode (['foo' => 'bar']);});

Wildcard subscription

Using the psubscribe method, you can subscribe to a channel defined by a wildcard, which is useful when obtaining all messages on all corresponding channels. $ Channel will be passed as the second parameter to the provided callback closure:

Redis::psubscribe(['*'], function($message, $channel) {    echo $message;});Redis::psubscribe(['users.*'], function($message, $channel) {    echo $message;});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.