PHP code example of christianklisch / phpcache

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

    

christianklisch / phpcache example snippets



$cache = new PHPCache();

$cache = new PHPCache(
	array('debug' => true, 'cacheDir' => 'cache', 'cacheTime' => 10)
);
/**
 * @param  array $settings Associative array of caching settings
 */

$myString = $cache->cacheVal('String to cache','cachingID');

$myObj = new FooBar();
$obj = $cache->cacheVal($myObj, $myObj->getId());

$myObj = new FooBar();

/**
 * Setting get-functions for primary keys of classes
 * @param  array $primkeys Associative array of class primary-key-function
 */
$cache->setPrimaryKeys(array(
    'FooBar' => 'getId'
));

$obj = $cache->cacheVal($myObj);

/**
 * check, if key or object in first parameter is cached. Returns true, if cached
 * @return bool
 */

if($cache->isCached($key))
    $obj = $cache->cacheVal($myObj, $key);  
else
    $obj = $logic->calcVal($key);  

/**
 * cache function result and return it
 * @return object     
 */    

$key = 1110;

$result = $cache->cacheFun(
    function () use ($key, $logic) {
        return $logic->getComplexResult();
    }
);

echo "result: ".$result; 

/**
 * delete old cached files 
 */    
$cache->gc();

/**
 * delete all cached files 
 */    
$cache->clearCache();