Redis use of the Laravel framework

Source: Internet
Author: User
Tags apc

The premise is that the Redis server has been installed and can be started (I only summed up the Mac installation method: Portal)I use the Mac system myself, there is a tutorial can refer to the following, Portal:1. Install PHP Predis1>predis is a PHP extension package that accesses Redis and only needs to download the original code, without the need to install PHP extensions (such as php-redis.so). 2>laravel installing third-party packages through composer (managing dependencies)3> Run the following command under the Laravel project root directory to install the Predis dependency package using composer:composer require Predis/predis 1.0.*2. Configurationby default, the Redis service provides 16 databases, and Laravel uses database 0 as the cache and Session storage. when you execute the command PHP artisan Cache:clear, the session is also removed, causing all users to log back in. Our goal is to have the cache, which is the default Redis, stored in database No. No. 0, and the Session is stored in database 1th. (' database ' = 0, ' database ' = 1) 1> Configuring the Redis databaseThe application's Redis configuration is located in config/database.php, where you can modify the array as you wish, just by giving each Redis server a name and specifying the hosts and interfaces that the Redis server uses.  ' Redis ' = [' cluster ' = False, //default Redis server' Default ' = [' Host ' = env (' redis_host ', ' localhost '),' Password ' = env (' Redis_password ', null),' Port ' = env (' Redis_port ', 6379),' database ' = 0,], //For session use' Session ' = [' Host ' = env (' redis_host ', ' localhost '),' Password ' = env (' Redis_password ', null),' Port ' = env (' Redis_port ', 6379),' database ' = 1,], //Configure more Redis servers ... ], 2> Specifying session usage DatabaseModify the connection option in the Config/session.php file as session:' Connection ' = ' session ', 3> The Session_driver option to modify the. env file is Redis:Session_driver=redis 4>, test it, please.Check to see if you are logged out after executing the following command:PHP Artisan cache:clear 3. Further analysisIn addition Redis if will be as cache tool, we config/cache.php file, please note the following two configurations ' Default ' = env (' cache_driver ', ' file '), ' stores ' = [ ' APC ' = [' driver ' = ' APC ',],' file ' = [' driver ' = ' file ',' path ' = Storage_path (' Framework/cache '),],//Some other configuration omitted ...' Redis ' = [' driver ' = ' redis ',' connection ' = ' default ',],],Analysis:1> This time the cache is using the file driverRedis in the 2>stores array corresponds to the default Redis configuration (Redis configuration options in the default,config/database.php file)changes: We can directly change the ' default ' = env (' Cache_driver ', ' redis ') so our cache is using Redis for caching (regardless of the configuration of the. env file) Further analysis:config/session.php file has ' driver ' = env (' session_driver ', ' file '),the config/cache.php file has ' default ' = env (' cache_driver ', ' file '),we're looking back. env file with the followingCache_driver=fileSession_driver=redisQueue_driver=sync redis_host=192.168.1.248Redis_password=nullredis_port=6379We know here we can refresh the original file for some configuration, in the. env file we can configure the session and the cache to use which way to drive, we can also configure the Redis server address and so on 4. Using Rediswe can invoke any command provided by the Redis client in a static way on the Redis façade, and then Laravel use the Magic method to pass the command to the Redis server and return the obtained results.  the premise is to introduce a redis façadeUse Illuminate\support\facades\redis;some basic useredis::set (' key ', ' value ');//Deposit to Redisredis::get (' key ');//Get the value in Redisredis::llen (' key ');//Length of queueredis::rpop (' key ');//Right out queueredis::rpush (' key ', ' value ');//On the right side of the queueredis::exists ($key)//redis If this key exists

Redis use of the Laravel framework

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.