The Laravel framework itself already has the corresponding Redis configuration we only need to change the configuration when using, but in the use of the time must pay attention to the problem of the namespace, you can see config/app.php below the aliases array in the specific path
1. Install boot Redis and dependency packages
If you are using a homestead virtual machine as the local development environment, Homestead has already installed Redis and boot up for us, if you are using the Windows development environment, you can refer to this article to install boot Redis.
After Redis starts, you also need to run the following command under the Laravel project root to install the predis
dependency package using composer:
1.0.*
2. Configuring Redis
As we mentioned earlier, Redis can be used as the primary database, so the configuration information for Redis in Laravel is config/database.php
in:
' Redis ' = [ false, ' default ' = = '127.0.0.1 ',6379,0, ],
Where the cluster
option indicates whether to shard between multiple redis nodes, we locally test only one node and set it to false
.
default
Option represents the default Redis host connection, where Redis and Web servers share a single host, so host
the default port for 127.0.0.1,redis is 6379.
In addition, the default
option supports more connection parameters, if needed:
Parameters |
Significance |
Default value |
Host |
Server IP address/host name |
127.0.0.1 |
Port |
The port number the Redis server listens on |
6379 |
Password |
If the server requires an authentication password |
Do not use passwords |
Database |
Database index selected at connection time |
No index set |
Timeout |
Connect to Redis server time-out |
5 Seconds |
Read_write_timeout |
Time-out for read and write operations over a network connection |
system default (No Limit timeout is set to-1) |
In addition, if Redis is used as a caching tool, the configuration option is also required config/cache.php
redis
:
' Redis ' = [ ' Redis ', ' Default ',],
This connection
corresponds config/database
to the redis
default host default
configuration in.
With this configuration in place, we can use Redis for data access in the app code.
3. Basic use
Because Laravel has added the Redis façade to config/app.php
the array by default aliases
, you can use the façade for Redis operations directly in your code. We can invoke any command provided by the Redis client in a static way on the Redis façade (Redis command Encyclopedia), and then Laravel use the Magic method to pass the command to the Redis server and return the obtained results.
Let's look at the simplest read operation:
' User:name:6 '; $user = User::find (6); if ($user) {//stores the user name in Redis redis::set ($key,$user->name);} //Determines if the specified key exists if (redis::exists ($key)) {//Gets the key value according to 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 data obtained 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);} //gets the total number of collection elements (returns 0 if the specified key does not exist) $nums = Redis::scard ( $key); if ( $nums >0) { //randomly gets three titles from the specified collection $post _titles = Redis::srandmember ( span class= "variable" > $key, 3); DD ( $post _titles);}
Note: The difference between a collection and a list is that duplicate elements are not allowed in the collection, and yes, that is the embodiment of the cross-anisotropy of the set in mathematics; the difference between an ordered set and a set is that the ordered set is orderly, which is the embodiment of the disorder of the mathematical set.
Laravel operating Redis