Set, retrieve and delete cache in laravel

Cache is used to store data in temporary or permanent environment for better performance and user experience. Caching is essential for achieving high performance and scalability. Setting up proper caching can reduce the load on the server and is helpful for minimal page response time. Laravel has in-built caching functionality with multiple options.

In this article, we will know more about caching and implement caching in our application.

While learning about cache, it's common to think about why caching is required? With help of caching you can provide minimal load time which helps your SEO strategy. As per Google, if your response time is lower then there are better chances to be at top of the search engine result page.

What Is Laravel Caching?

Laravel provides a robust and easy-to-use implementation of caching with different caching drivers like database, memcached, redis, DynamoDB. In Laravel cache, you can use caching functionality with writing minimal code or almost no code.

Setting up Cache In Laravel application

Before using cache in Laravel, you need to configure caching in Laravel application. First thing is driver configuration into .env file. In .env file, CACHE_DRIVER

You can use any cache driver as per your requirement by default file driver is configured. But before changing database driver, there are things to configure as per you selected driver. For example, you are using database driver to store cache then you need to create table for storing cache data. Using the Memcached driver requires the Memcached PECL package to be installed.

In this example, we will use database driver. To create cache table into our database either we create new migration or use php artisan cache:table command to create migration file.

Retreiveing Cache Value

To access cache store instance, we will use Cache facade. With cache facade we can use all cache related methods as abstract methods. we just have to use use Illuminate\Support\Facades\Cache;

To get cache values, we will use get() method. Cache's get method is just like session's get method. You can get value by key and also configure it for default value if relevant key is not found. We just have to pass second argument as default value.

Checking For Item Existence

Sometimes, we need to check key is exists in cache and perform actions based on result. By using has() method, we can determine particular key exists in cache instance or not. it will return true or false based. But value of key is null then it will also return false.

Incrementing / Decrementing Values

Sometimes we require to perform increment or decrement value of particular cache. For example, we are counting page views for user and storing those page-views into cache then we can use increment method. Suppose we have given trial of particular service for limited requests then we can use use decrement method.

Here, we can also specify increment or decrement amount by passing as second parameter.

Storing Data In Cache

To store data in cache, we will use put method on cache facade. The put method will take three parameters. First is key, second is value and third one is time in second or Date Time object to set expiry time.

Here, if time is not passed then it will store it indefinitely. For time, we can pass date time object as expiry time like below example :

To store cache data permently, you can use forever method. For removing this cache data, you need to perform forget method manually.

The add method will only add the item to the cache if it does not exist in the cache store. It's used when we require to store data if it's not exists.

Removing Cache Data In Laravel

You can remove cache data using multiple methods like forget, put or flush.

The forget method, will take key as parameter and remove it from cache instance.

We can also use put method to remove cache. But in put method we update expiry time for particular data in negative or zero. So it will be automatically removed.

The flush method, will used when we want to clear all cache data. It will clear entire cache instance.

Conclusion

Here, We have taken short examples for storing, retrieve or deleting cache into the Laravel application. There are plenty of other concepts to use cache for better optimization. We will learn more about those in further tutorials.


Share your thoughts

Ask anything about this examples