Caching Utility

The CachingUtil in the LaraUtilX package provides functionality for caching data with configurable options and 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.

Methods

  1. cache(string $key, mixed $data, int $minutes = null, array $tags = null): mixed: Caches data with configurable options.
  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 omarchouman\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
           $tags = ['tag1', 'tag2']; // Tags to associate with cached data
    
           $this->cachingUtil->cache($key, $data, $minutes, $tags);
       }
    }
  2. Retrieve Cached Data: To retrieve cached data, use the get method:

    use omarchouman\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 omarchouman\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 utility provides convenient methods for caching, retrieving, and forgetting data, enhancing the performance and scalability of Laravel applications.

  • Success Result: Data can be efficiently cached with configurable options, retrieved when needed, and forgotten when no longer required.

  • Error Result: If an error occurs during caching or retrieval, appropriate error handling mechanisms should be implemented based on application requirements.

Publish

You can publish this utility through the below command:

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



This utility simplifies the process of caching data in Laravel applications, improving overall performance and user experience.