Caching in Laravel 8/9/10 with Example

In this tutorial, you will learn about caching in Laravel 8/9/10 with example.

Caching is a very powerful tool that helps to store frequently needed data in a very fast data store so that the data can be retrieved quickly on subsequent requests for the same information.

Laravel supports caching by providing a powerful, unified API for numerous cache backends, allowing web applications to benefit from lightning-fast data retrieval.

Laravel has built-in support for Memcached, Redis, DynamoDB, and relational databases, as well as other common caching backends. Laravel also supports a file-based cache driver, as well as array and "null" cache drivers that provides simple cache backends for automated tests.

Laravel is set up to use the file cache driver by default, which saves serialized, cached objects to the server's filesystem. However, more robust driver, such as Memcached or Redis should be used for larger applications. We can even set up numerous cache settings for the same driver.

In this example, you will learn about file-based caching in Laravel. In file-based caching, data is stored and retrieved by writing it to files on the server's filesystem. This approach can be valuable for caching frequently accessed data, such as query results, rendered views, or any other data that you want to temporarily store and quickly retrieve.

Here's how to use file-based caching in Laravel:

  1. Configuring:
  2. Make sure that your Laravel application is correctly configured. You can find the cache configuration settings in the config/cache.php file. By default, Laravel uses the file driver for caching. File-based cached objects are stored at storage/framework/cache/ location.

  3. Storing Data in Cache:
  4. You can store data in the cache using the Cache facade or the cache() helper function. Here's an example of how to store data in the cache:

    // importing
    use Illuminate\Support\Facades\Cache;
    use Illuminate\Support\Carbon;
    //Adding new item to cache for no longer than 10 minutes
    $expiresAt = Carbon::now()->addMinutes(10);
    $key = 'key';
    $value = 'value';
    Cache::add($key, $value, $expiresAt);

    In the above code, 'key' is the cache key, 'value' is the data you want to store, and $expiresAt is the number of minutes the data should be cached.

    The add method only adds an item to the cache if it does not already exist in the cache store. This method returns true if the item is added to the cache, otherwise it returns false. We can also pass DateTime instance representing the cached item's desired expiration time.

  5. Retrieving Data from Cache:
  6. You can retrieve data from the cache using the get method. The get method retrieves an item from the cache. It returns null if the item is not present in the cache. Here's an example:

    // Retrieve data from cache
    $data = Cache::get('key', 'defaultValue');
  7. Checking if Data Exists:
  8. You can check if a cache key exists using the has method. Here's an example:

    if (Cache::has('key')) {
        // Data exists in the cache
    } else {
        // Data doesn't exist in the cache
  9. Removing or Deleting Data from Cache:
  10. The forget method removes the item with the specified key from the cache. Here's an example:

  11. Clearing Entire Cache:
  12. The flush method deletes everything from the cache. Here's an example: