This article mainly introduces to you about Laravel using Redis to implement Web site cache read the relevant information, the text through the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value, the need for friends to learn together.
Introduction to Redis
Redis is fully open source free, adheres to the BSD protocol and is a high-performance Key-value database. Redis and other Key-value
The cache product has the following three features: Redis support data persistence, you can keep the in-memory data on disk, restart can be loaded again for use.
Redis not only supports simple key-value types of data, but also provides storage of data structures such as List,set,zset,hash.
Redis supports backup of data, that is, Master-slave mode of data backup.
Redis Benefits
The performance is very high –redis can read the speed is 110,000 times/s, the write speed is 81,000 times/s.
Rich data types –redis support binary case Strings, Lists, hashes, sets and Ordered sets data type operations.
All atomic –redis operations are atomic, and Redis supports atomic execution of several operations.
Rich features –redis also supports publish/subscribe, notifications, key expiration, and more.
How to install Redis
1.Ubuntu (because I use the Homestead Environment virtual machine is Ubuntu so only used this method to install)
The official Ubuntu installation method
$sudo apt-get update$sudo apt-get Install Redis-server
Because my site uses the Laravel framework, to use the composer method to install
Composer require Predis/predis
Installation of Illuminate/redis (5.2.*) is required if the lumen micro-frame is installed
Composer require Illuminate/redis (5.2.*)
Configuration
If the lumen is installed
You need to include this code $app->register (Illuminate\redis\redisserviceprovider::class) in bootstrap/app.php; $app Configure (' database ');
Redis can be used as the primary database, so the configuration information for Redis in Laravel is in config/database.php:
' Redis ' = [' cluster ' + false, ' default ' = [' host ' = ' 127.0.0.1 ', ' port ' = ' 6379 ', ' database ' = 0,] ,
Where the cluster option indicates whether to shard between multiple redis nodes, we locally test only one node and set it to false.
The default option means that Redis hosts a single host, so the default port for host 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) |
Read_write_timeout | Timeout 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, you also need to configure the Redis option in config/cache.php:
' Redis ' = [' Driver ' = ' redis ', ' connection ' = ' default ',],
The connection here corresponds to the default configuration for Redis in Config/database.
With this configuration in place, we can use Redis for data access in the app code.
See an example ———— laravel MySQL and Redis collocation
My idea is to read the time will go to the cache to find, if found that the prosperous, can not find and then go to the database to find and tune into the cache.
if (Cache::has ($key)) { //search for cache first if found $values = Cache::get ($>key);//Read cache return $values directly;} else{ //If there is no $values in the cache = Db::select ($sql); Cache::p ut ($key, $value, $time); return $values; }
But Laravel's cache also provides a remember function.
$values = Cache::remember ($key, $time, function () { return Db::select ($this->sql); });
If the cache has a direct read and returns, if the cache entry does not exist in the cache, the closure returned to the Remember method will be run, and the result of the closure will be stored in the cache.
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!