Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Cache Utils Laravel Package

thecodingmachine/cache-utils

Small set of PHP cache helpers by TheCodingMachine: adapters and utilities around PSR-6/PSR-16 caches, including cache wrappers and simple tooling to standardize access, reduce boilerplate, and ease integration in frameworks and libraries.

View on GitHub
Deep Wiki
Context7

Latest Stable Version Total Downloads Latest Unstable Version License Scrutinizer Code Quality Build Status Coverage Status

Why?

This package contains a number of utility classes to play with PSR-6 caches.

File bound cache

Most PHP cache systems (like PSR-6 or PSR-16) are storing items in cache and attributing to items a time to live (TTL).

If you are developing a PHP framework or a PHP analysis library that relies a lot on reflection, it is quite common to have cache items that are related to PHP files or PHP classes.

For instance, Doctrine Annotations in a class do not change unless the class file(s) is changed. Therefore, it makes sense to bind the cache invalidation to the modification date of the file. thecodingmachine/cache-utils provides just that.

use TheCodingMachine\CacheUtils\FileBoundCache;

$fileBoundCache = new FileBoundCache($psr6Cache);

// Put the $myDataToCache object in cache.
// If 'FooBar.php' and 'FooBaz.php' are modified, the cache item is purged.
$fileBoundCache->set('cache_key', $myDataToCache, 
[
    'FooBar.php',
    'FooBaz.php'
]);

// Fetching data
$myDataToCache = $fileBoundCache->get('cache_key');

You can also use the FileBoundMemoryAdapter to store the cache in memory for even faster access in the same query.

use TheCodingMachine\CacheUtils\FileBoundCache;
use TheCodingMachine\CacheUtils\FileBoundMemoryAdapter;

$fileBoundCache = new FileBoundMemoryAdapter(new FileBoundCache($psr6Cache));

Class bound cache

You can also bind a cache item to a class / trait / interface using the ClassBoundCache class. The cache will expire if the class / trait / interface is modified.

use TheCodingMachine\CacheUtils\FileBoundCache;
use TheCodingMachine\CacheUtils\ClassBoundCache;

$fileBoundCache = new FileBoundCache($psr6Cache);
$classBoundCache = new ClassBoundCache($fileBoundCache);

// Put the $myDataToCache object in cache.
// If the FooBar class is modified, the cache item is purged.
$classBoundCache->set('cache_key', $myDataToCache, new ReflectionClass(FooBar::class));

// Fetching data
$myDataToCache = $classBoundCache->get('cache_key');

The ClassBoundCache constructor accepts 3 additional parameters:


class ClassBoundCache implements ClassBoundCacheInterface
{
    public function __construct(FileBoundCacheInterface $fileBoundCache, bool $analyzeParentClasses = true, bool $analyzeTraits = true, bool $analyzeInterfaces = false)
}
  • $analyzeParentClasses: if set to true, the cache will be invalidated if one of the parent classes is modified
  • $analyzeTraits: if set to true, the cache will be invalidated if one of the traits is modified
  • $analyzeInterfaces: if set to true, the cache will be invalidated if one of the interfaces implemented is modified

You can also use the ClassBoundMemoryAdapter to store the cache in memory for even faster access in the same query.

use TheCodingMachine\CacheUtils\ClassBoundCache;
use TheCodingMachine\CacheUtils\ClassBoundMemoryAdapter;

$classBoundCache = new ClassBoundMemoryAdapter(new ClassBoundCache($psr6Cache));

Easier interface with cache contracts

You can even get an easier to use class bound cache using the ClassBoundCacheContract.

use TheCodingMachine\CacheUtils\FileBoundCache;
use TheCodingMachine\CacheUtils\ClassBoundCache;
use TheCodingMachine\CacheUtils\ClassBoundMemoryAdapter;
use TheCodingMachine\CacheUtils\ClassBoundCacheContract;

$fileBoundCache = new FileBoundCache($psr6Cache);
$classBoundCache = new ClassBoundMemoryAdapter(new ClassBoundCache($psr6Cache));
$classBoundCacheContract = new ClassBoundCacheContract(new ClassBoundCache($fileBoundCache));

// If the FooBar class is modified, the cache item is purged.
$item = $classBoundCache->get(new ReflectionClass(FooBar::class), function() {
    // ...
    // let's return the item to be cached.
    // this function is called only if the item is not in cache yet.
    return $item;
});

With cache contracts, there is not setters. Only a getter that takes in parameter a callable that will resolve the cache item if the item is not available in the cache.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours