Caching Utility

The CachingUtil in the LaraUtilX package provides enhanced functionality for caching data with configurable options, automatic tag support detection, and robust error handling for retrieving or forgetting cached data.

Constructor

The constructor accepts two parameters:

  • $defaultExpiration: The default expiration time for cached data in minutes.
  • $defaultTags: An array of default tags to associate with cached data.

Features

  • Smart Tag Support: Automatically detects if the cache store supports tagging
  • Fallback Mechanism: Gracefully falls back to regular caching if tags fail
  • Flexible Parameters: Uses constructor defaults when parameters are null
  • Error Handling: Robust error handling with automatic fallbacks
  • Time Conversion: Automatic conversion from minutes to seconds for Laravel's cache system

Methods

  1. cache(string $key, mixed $data, int $minutes = null, array $tags = null): mixed: Caches data with configurable options, automatic tag support detection, and fallback mechanisms.
  2. get(string $key, mixed $default = null): mixed: Retrieves cached data.
  3. forget(string $key): Forgets cached data.

Usage

  1. Cache Data: To cache data with configurable options, use the cache method:

    use LaraUtilX\Utilities\CachingUtil;
    
    class MyController extends Controller
    {
       protected $cachingUtil;
    
       public function __construct(CachingUtil $cachingUtil)
       {
           $this->cachingUtil = $cachingUtil;
       }
    
       public function index()
       {
           $key = 'cached_data';
           $data = [...]; // Data to be cached
           $minutes = 60; // Expiration time in minutes (optional, uses default if null)
           $tags = ['tag1', 'tag2']; // Tags to associate with cached data (optional, uses default if null)
    
           // The cache method will automatically:
           // - Use default expiration if $minutes is null
           // - Use default tags if $tags is null
           // - Detect if the cache store supports tagging
           // - Fall back to regular caching if tagging fails
           $this->cachingUtil->cache($key, $data, $minutes, $tags);
       }
    }
  2. Retrieve Cached Data: To retrieve cached data, use the get method:

    use LaraUtilX\Utilities\CachingUtil;
    
    class MyController extends Controller
    {
        protected $cachingUtil;
    
        public function __construct(CachingUtil $cachingUtil)
        {
            $this->cachingUtil = $cachingUtil;
        }
    
        public function getData()
        {
            $key = 'cached_data';
    
            $cachedData = $this->cachingUtil->get($key);
    
            // Use the retrieved cached data
        }
    }
  3. Forget Cached Data: To forget cached data, use the forget method:

    use LaraUtilX\Utilities\CachingUtil;
    
    class MyController extends Controller
    {
        protected $cachingUtil;
    
        public function __construct(CachingUtil $cachingUtil)
        {
            $this->cachingUtil = $cachingUtil;
        }
    
        public function forgetData()
        {
            $key = 'cached_data';
    
            $this->cachingUtil->forget($key);
        }
    }

Result

The enhanced utility provides robust methods for caching, retrieving, and forgetting data, with intelligent fallback mechanisms and automatic tag support detection.

  • Success Result: Data is efficiently cached with configurable options, retrieved when needed, and forgotten when no longer required. The utility automatically handles tag support detection and gracefully falls back to regular caching when needed.

  • Error Handling: The utility includes built-in error handling with automatic fallbacks. If tagging fails, it automatically falls back to regular caching without throwing exceptions, ensuring your application continues to function smoothly.

  • Performance: Automatic time conversion from minutes to seconds ensures optimal performance with Laravel's cache system.

Publish

You can publish this utility through the below command:

php artisan vendor:publish --tag=lara-util-x-caching



This enhanced utility simplifies the process of caching data in Laravel applications with intelligent fallback mechanisms, improving overall performance, reliability, and user experience.