Laravel Cache


Laravel Cache : Cache is one of the important part of any framework. Cache improves the system’s performance. Laravel has powerful cache mechanism which makes it perfect. Laravel provides a unified API for various caching systems. Laravel cache config file is located at config/cache.php where you can add configurations.
Laravel supports following types of caching-

  • Memcached
  • Redis

Laravel Cache Syntax And Example-

Let us go step by step –

Settings –

Databse :

Add a database setup to create table which will store information for database cache-

Laravel Create Database Cache Table –

Schema::create('cache', function($table) {
    $table->string('key')->unique();
    $table->text('value');
    $table->integer('expiration');
});

Memcached –

Add settings for memcached. Go to config/cache.php and add the memcached settings-

Laravel Memcached –

 'memcached' => [
            'driver'  => 'memcached',
            'servers' => [
                [
                    'host' => '127.0.0.1', 'port' => 11211, 'weight' => 100,
                ],
            ],
        ],

You can add your own host name.

Note : Memcached needs http://pecl.php.net/package/memcached to be installed.

Redis

Laravel Cache Redis Settings –

'redis' => [

    'cluster' => false,

    'default' => [
        'host'     => '127.0.0.1',
        'port'     => 6379,
        'database' => 0,
    ],

],
Note : Make sure predis/predis package (~1.0) is installed.

Laravel Cache Example

Let us understand the Laravel Cache functionality with simple Example –

Laravel Cache Syntax

Cache::put(); Accepts three parameters key, value and time in minutes you want to store the cache.

Laravel Cache Syntax –

Cache::put('key', 'value', $minutes);

Here is a simple example –

Laravel Create Cache Example Syntax

Cache::put(); Accepts three parameters key, value and time in minutes you want to store the cache.

Laravel Cache Syntax –

Route::get('/', function()
{
Cache::put('mykey', 'Welcome to Home Page !', 1);
});

Now if you hit the rout url /URL cache will be created but you will see nothing on browser. To check the cache created go to
app/storage/cache folder and check the file created after opening the file you will see the cache data along with content “Welcome to Home Page !”

Laravel Get Cache Example Syntax

Cache::get(); Accepts one parameter key to get the created cache.

Laravel Get Cache Syntax –

Route::get('/', function()
{
Cache::get('mykey');
});

Now if you hit the index url /URL it will return the cache data from cache file create above.

Laravel Create Cache Forever Example Syntax

Cache::forever(); If you want to keep the cache file forever means it will not expire until you delete it.

Laravel Forever Cache Syntax –

Route::get('/', function()
{
Cache::forever('mykey', 'Welcome to Home Page !');
});

Laravel Forget Cache/Delete Cache Example Syntax

Cache::forget(); is used to delete the cache created using the key. It accepts key as input parameter to remove the cache for that key.

Laravel Forget Cache Syntax –

Route::get('/', function()
{
Cache::forget('mykey');
});

Advertisements

Add Comment

📖 Read More