PHP code example of germania-kg / cachecallable

1. Go to this page and download the library: Download germania-kg/cachecallable 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/ */

    

germania-kg / cachecallable example snippets



use phpFastCache\CacheManager;
use Monolog\Logger;
use Germania\Cache\CacheCallable;

// Setup dependencies
$cacheItemPool = CacheManager::getInstance('files', [ options ]);
$lifetime      = 3600;
$monolog       = new Logger( "My App" );
$content_creator = function( $keyword ) {
	return "Cache keyword: " . $keyword;
};


//
// Setup Cache wrapper
// 
$wrapped_cache = new CacheCallable(
	$cacheItemPool,
	$lifetime,
	$creator,
	$monolog // optional
);

// Identifying key. Example for a web page:
$keyword = sha1($_SERVER['REQUEST_URI']);
echo $wrapped_cache( $keyword );

$wrapped_cache->setCacheKeyCreator( function($raw) { return sha1($raw); } );


namespace Germania\Cache;

interface LifeTimeInterface {
	// Return seconds to expiration
	public function getValue();
}


use Germania\Cache\CacheCallable;
use Germania\Cache\LifeTime;

// Setup LifeTime object
$lifetime_object = new LifeTime( 3600 );

// Use Factory method:
$lifetime_object = LifeTime::create( 3600 );

// Create from Lifetime instance
$another_lifetime = new LifeTime( $lifetime_object );
$another_lifetime = LifeTime::create( $lifetime_object );


use Germania\Cache\CacheCallable;
use Germania\Cache\LifeTime;

// Taken from example above
$wrapped_cache = new CacheCallable(
	$cacheItemPool,
	$lifetime_object,
	$creator
);


namespace Germania\Cache;

interface LifeTimeInterface {
	// Return seconds to expiration
	public function getValue();
}


use Germania\Cache\CacheCallable;
use Germania\Cache\LifeTime;

// Setup LifeTime object
$lifetime_object = new LifeTime( 3600 );

// Use Factory method:
$lifetime_object = LifeTime::create( 3600 );

// Create from Lifetime instance
$another_lifetime = new LifeTime( $lifetime_object );
$another_lifetime = LifeTime::create( $lifetime_object );

// Change LifeTime value during runtime, 
// e.g. in router or controller
$lifetime_object->setValue( 0 );


use Germania\Cache\CacheCallable;
use Germania\Cache\LifeTime;

// Taken from example above
$wrapped_cache = new CacheCallable(
	$cacheItemPool,
	$lifetime_object,
	$creator
);

// Default content creator
$default_creator = function($file) {
	return json_decode( file_get_contents($file) );
};

// Setup Service
$wrapped_cache = new CacheCallable(
    $cacheItemPool,
    $lifetime_object,
    $default_creator
);

// Override content creation 
$config = $wrapped_cache("config.json", function( $file ) {
	return array('foo' => 'bar');
};