Home > Article > PHP Framework > Laravel development: How to use Laravel Redis to implement data caching?
Laravel is a very popular PHP framework that is widely used in web development. The Laravel framework provides many convenient APIs and components, including data caching. Redis is a popular open source in-memory data structure storage that can very effectively improve data query and reading efficiency. This article will introduce how to use Laravel Redis to implement data caching in Laravel to improve the performance of web applications.
First, we need to install Laravel Redis. It can be installed using Composer by running the following command:
composer require predis/predis
Implement the Laravel Redis cache driver using the predis/predis package. After completing the installation, you need to set the REDIS_HOST, REDIS_PASSWORD and REDIS_PORT variables in the .env
file.
REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379
Modify the configuration in the config/cache.php
file and change the 'default' option to 'redis' to use Redis as the cache driver. Additionally, set the Redis connection information in the 'redis' array.
'default' => env('CACHE_DRIVER', 'redis'), 'redis' => [ 'client' => 'predis', 'default' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => env('REDIS_DB', 0), ], ],
Once the Laravel Redis driver and configuration are set up, we can start using it. Here are 3 methods for data caching through Laravel Redis:
The easiest way to use Laravel Redis is to use the basic method of the Cache
facade. The Laravel framework provides many advanced features, but here we emphasize the most basic methods of caching. Caching can be done through the following code:
use IlluminateSupportFacadesCache; $value = Cache::remember('key', $minutes, function () { return DB::table('users')->get(); });
The above code caches the results from the "users" table into Redis. After this, the cached copy in Redis will be reused when calling the key instead of regenerating the results from the source database. The second parameter of the Cache::remember
method is the cache validity period in minutes, or the Cache::forever
method can be used to permanently save the data to the cache.
Another advanced caching method using Laravel Redis is to use tags. Tags allow you to create and capture multiple cache entries and then use tags to clear all of them. Tag caching provides greater control over cache lifecycle.
use IlluminateSupportFacadesCache; Cache::tags(['people', 'artists'])->put('John', $john, $minutes); Cache::tags(['people', 'authors'])->put('Jane', $jane, $minutes);
The above code saves John
in two tags people
and artists
, and Jane
in In the two tags people
and authors
. It is now possible to use the flush
method of a specific tag to clear only all cached items under that tag.
Cache::tags(['people'])->flush();
The above code clears all caches with the people
tag and deletes them from Redis.
Laravel Redis also provides some auxiliary functions that have fewer functions but are more convenient to use. Here are some of them:
cache()->put('key', 'value', $minutes)
Cache the input value for a certain amount of time. cache()->get('key')
Retrieve a cached value. cache()->remember('key', $minutes, function() { return 'value'; })
Retrieve any cache items, returning a new value on failure. cache()->rememberForever()
Always remember cache items. Laravel Redis provides some simple and easy-to-use methods to cache data and achieve efficient data reading and querying in web applications. The Laravel framework is almost fully powered by Composer, which makes working with Laravel Redis easy. The benefit of using Laravel Redis cache on large web applications is that it reduces wasted time from slower databases and increases query speed for dynamic data.
The above is the detailed content of Laravel development: How to use Laravel Redis to implement data caching?. For more information, please follow other related articles on the PHP Chinese website!