Learn Laravel

Laravel Cache

Laravel provides powerful and easy caching systems.Genrally cache are used to speed up your dynamic application.Cache works as layer between your website and your database. your application became slow,when you have lots of traffic.Cache store data that has not been changed between requests.In laravel cache configuration file is located at app/config/cache.php

Storing an item in laravel cache

We use put() method to store item in laravel cache.

Syntax

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

Example

Cache::put('userCache',$user,20);

In above example we are putting the $user object in the cache for 20 minutes.

Storing an item in laravel cache if it doesn't exist

We use add() method to store item in laravel cache if it doesn't exist.add method will return true if the item is added to the cache.Otherwise,It will return false.

Syntax

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

Example

Cache::put('studentCache',$student,20);

add method will only store an item in the cache if the key does not already exist.

Checking if key exists in the cache

Example

you can use the has() method to check existence of key.

if (Cache::has('key')) { // }

Fetching an item from cache

To fetch an item from the cache, we can simply use the get() method.

Syntax

Cache::get('key');

Example

$student=Cache::get('studentCache');

Fetching an item from cache Or Returning a default value

We can also provide a default value if the key we are looking for does not exist.

Syntax

Cache::get('key', 'default');

Example

$student=Cache::get('studentCache',$default);

Example

$student =Cache::get('studentCache', function() { return DB::table('student')->get(); });

Storing an item in cache permanently

we can use forever method for storing an item in cache permanently.

Syntax

Cache::forever('key', 'value');

Example

Cache::forever('userCache', $user);

Pulling an item from cache

If we need to fetch an item from cache and then delete it, we can use the pull method.

Syntax

Cache::pull('key');

Example

$student = Cache::pull('studentCache');

Removing An Item From The Cache

we can use the forget method.

Syntax

Cache::forget('key');

Example

Cache::forget('studentCache');

Cache Tags

Cache tags allow us to group related items in the cache, and then we can flush all caches tagged based upon a tag name.

Storing an item with a cache tag

Syntax

Cache::tags('tagname')->put('key', $value, $minutes);

Example

Cache::tags('userTag')->put('userCache', $user,20);

Example

In below example ,We are using an array of tags.

// Using an array Cache::tags(array('userTag','userdata'))->put('userCache', $user,20);

Fetching a tagged cache

We need to pass list of tags and use the get method.

Syntax

Cache::tags('tagname')->get('key');

Example

Cache::tags('userTag')->get('userCache');

Example

// Using an array Cache::tags('array('userTag','userdata'))->get('userCache');

Flushing a tagged cache

Syntax

Cache::tags('tagname')->flush();

Example

Cache::tags('userTag')->flush();