PHP code example of thomas-institut / datacache

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

    

thomas-institut / datacache example snippets

 
class MyDataCache implements DataCache {
// ...
}

$cache = new MyDataCache(...);

$cache->set('someKey', 'someValue'); 

$cache->setDefaultTtl($newDefaultTtl); 

$cache->set('someKey', 'someValue', $customTtl); 

$value = $cache->get('someKey');

$exists = $cache->isInCache('someKey');

$remainingTtl = $cache->getRemainingTtl('someKey');

$cache->delete('someKey');

$cache->flush();

$cache->clean();

$cache = new InMemoryDataCache();

$cache = new DirectoryDataCache('/path/to/directory', $cacheName);

$directoryPath . '/' . $cacheName . $sep . $keyOrHash . $sep . $exp . '.' . $ext 

$cache = new MultiCacheDataCache( [ $cache1, $callable2, $cache3 ...]);

$cache2 = new MultiCacheDataCache( 
    [ $cache1, $callable2, ...], 
    [ 'prefix1', 'prefix2', ...], true);


class MyCacheAware  implements CacheAware { 
  
    public function someMethod() {
     
         // ...
        if ($this->isCacheInUse()) {
            // do something with the cache
            $this->getDataCache()->set(...);
        }
    }
}


$myInstance = new MyCacheWare(...);

$myInstance->setCache($someDataCacheInstanceOrSomeCallable);

$myInstance->useCache();
$myInstance->someMethod();  // cache will be used

$myInstance->doNotUseCache();
$myInstance->someMethod(); // cache will not be used