PHP code example of thecodingmachine / cache-utils

1. Go to this page and download the library: Download thecodingmachine/cache-utils library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thecodingmachine / cache-utils example snippets


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');

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

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

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');


class ClassBoundCache implements ClassBoundCacheInterface
{
    public function __construct(FileBoundCacheInterface $fileBoundCache, bool $analyzeParentClasses = true, bool $analyzeTraits = true, bool $analyzeInterfaces = false)
}

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

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

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;
});