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.
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.cache(string $key, mixed $data, int $minutes = null, array $tags = null): mixed: Caches data with configurable options, automatic tag support detection, and fallback mechanisms.get(string $key, mixed $default = null): mixed: Retrieves cached data.forget(string $key): Forgets cached data.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);
}
}
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
}
}
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);
}
}
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.
You can no longer publish this class, and you do not need to. A published copy kept its LaraUtilX namespace while landing in app/, where Composer's PSR-4 mapping expects App\, so the copy was never autoloaded and every call still resolved to the package.
Extend or wrap it instead:
namespace App\Support;
use LaraUtilX\Utilities\CachingUtil;
class AppCache extends CachingUtil
{
// override what you need
}
Then bind your subclass in a service provider if you want it resolved in place of the package's:
$this->app->bind(\LaraUtilX\Utilities\CachingUtil::class, \App\Support\AppCache::class);
This enhanced utility simplifies the process of caching data in Laravel applications with intelligent fallback mechanisms, improving overall performance, reliability, and user experience.
On a taggable store such as Redis, tagged entries live in their own namespace. Reads and writes therefore have to agree on the tags, which CachingUtil handles for you: get() and forget() use the same default tags the entry was written with.
$cache = new CachingUtil(60, ['reports']);
$cache->cache('monthly', $data); // written under the "reports" tag
$cache->get('monthly'); // read under the same tag
$cache->forget('monthly');
Pass tags explicitly when a single call needs different ones:
$cache->cache('weekly', $data, 30, ['exports']);
$cache->get('weekly', null, ['exports']);
When the store is not taggable, or no tags are configured, everything falls back to plain cache operations.
Changed in v1.5.4. Entries were written with tags but read and forgotten without them, so on a taggable store with
default_tagsconfigured every read missed.