PHP code example of yiisoft / cache-db

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


// Create db schema manager
$dbSchemaManager = new DbSchemaManager($db);

// Ensure table with default name
$dbSchemaManager->ensureTable();

// Ensure table with custom name
$dbSchemaManager->ensureTable('{{%custom_cache_table}}');

// Ensure no table with default name
$dbSchemaManager->ensureNoTable();

// Ensure no table with custom name
$dbSchemaManager->ensureNoTable('{{%custom_cache_table}}');

$cache = new \Yiisoft\Cache\Db\DbCache($db, $table, $gcProbability);

$cache = new \Yiisoft\Cache\Db\DbCache($db);
$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();

$cache = new \Yiisoft\Cache\Db\DbCache($db, $table, $gcProbability);
$cache->setLogger(new \Yiisoft\Log\Logger());