Installation, configuration, and basic usage of Laravel 5.1 Redis framework

Source: Internet
Author: User
Tags redis redis server

1. Install and start Redis and its dependent packages

If you are using the Homestead virtual machine as the local development environment, Homestead has installed and started Redis for us. If you are using the Windows development environment, refer to this article to install and start Redis.

After Redis is started, run the following command in the root directory of the Laravel project to install the predis dependency package using Composer:

Composer require predis/predis 1. 0 .*

2. Configure Redis

We have mentioned that Redis can be used as the primary database, so the configuration information of Redis in Laravel is located in config/database. php:

'Redis '=> [
'Cluster' => false,
'Default' => [
'Host' => '2017. 0.0.1 ',
'Port' => 6379,
'Database' => 0,
],
],

The cluster option indicates whether to perform sharding among multiple Redis nodes. Here, we test that there is only one node, so we set it to false.

The default option indicates that the Redis host is connected by default. Redis and the Web server share one host, so the host is 127.0.0.1, and the default port of Redis is 6379.

In addition, the default option supports more connection parameters (if needed ):

 

In addition, if Redis is used as a cache tool, you also need to configure the redis option in config/cache. php:

'Redis '=> [
'Driver '=> 'redis ',
'Connection' => 'default ',
],

The connection corresponds to the default host default configuration of redis in config/database.

After completing the above configuration, we can use Redis in the application code for data access.

3. Basic use


By default, Laravel has added the Redis facade to the aliases array of config/app. php. Therefore, you can directly use this facade in the code for Redis operations. We can call any commands provided by the Redis client in a static way on the Redis facade (Redis command Daquan), then Laravel uses the magic method to pass the commands to the Redis server and return the obtained results.

The simplest read operation is as follows:

$ Key = 'User: name: 6 ';

$ User = User: find (6 );
If ($ user ){
// Store the user name in Redis
Redis: set ($ key, $ user-> name );
}

// Determine whether the specified key exists
If (Redis: exists ($ key )){
// Obtain the key value based on the key name
Dd (Redis: get ($ key ));
}

The above is a simple string access. Let's look at a more complex example to store the obtained data in the collection:

$ Key = 'posts: title ';

$ Posts = Post: all ();
Foreach ($ posts as $ post ){
// Store the article title in the collection
Redis: sadd ($ key, $ post-> title );
}

// Obtain the total number of elements in the set (if the specified key does not exist, 0 is returned)
$ Nums = Redis: scard ($ key );

If ($ nums> 0 ){
// Obtain three titles randomly from the specified set
$ Post_titles = Redis: srandmember ($ key, 3 );
Dd ($ post_titles );
}

Note: the difference between a set and a list is that repeated elements are not allowed to appear in the set. Yes, this is the embodiment of the interaction in mathematics; the difference between an ordered set and a set is that an ordered set is an ordered set, which is a manifestation of the disorder of a mathematical set.

Related Article

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.