PHP code example of yiisoft / cache-file

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

    

yiisoft / cache-file example snippets


$cache = new \Yiisoft\Cache\File\FileCache('/path/to/directory');

$cache = new \Yiisoft\Cache\File\FileCache(
    cachePath: '/path/to/directory',
    directoryMode: 0777,        // Permission for new directories (default: 0775)
    fileSuffix: '.cache',       // Cache file suffix (default: '.bin')
    fileMode: 0644,             // Permission for new cache files (default: null)
    directoryLevel: 2,          // Sub-directory levels (default: 1)
    gcProbability: 100,         // GC probability in parts per million (default: 10)
);

$cache = new \Yiisoft\Cache\File\FileCache('/path/to/directory', 0777); // default is 0775

$cache = new \Yiisoft\Cache\File\FileCache(
    cachePath: '/path/to/directory',
    fileSuffix: '.cache', // default is '.bin'
);

$cache = new \Yiisoft\Cache\File\FileCache(
    cachePath: '/path/to/directory',
    fileMode: 0644, // default is null
);

$cache = new \Yiisoft\Cache\File\FileCache(
    cachePath: '/path/to/directory',
    directoryLevel: 2, // default is 1
);

$cache = new \Yiisoft\Cache\File\FileCache(
    cachePath: '/path/to/directory',
    gcProbability: 100, // default is 10
);

$cache = new \Yiisoft\Cache\File\FileCache('/path/to/directory');
$parameters = ['user_id' => 42];
$key = 'demo';

// try retrieving $data from cache
$data = $cache->get($key);

if ($data === null) {
    // $data is not found in cache, calculate it from scratch
    $data = calculateData($parameters);
    
    // store $data in cache for an hour so that it can be retrieved next time
    $cache->set($key, $data, 3600);
}

// $data is available here

$cache->delete($key);
// Or all cache
$cache->clear();