PHP code example of initphp / cache

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

    

initphp / cache example snippets

 
use \InitPHP\Cache\Cache;

$cache = Cache::create(\InitPHP\Cache\Handler\File::class, [
    'path'      => __DIR__ . '/Cache/';
]);

if(($posts = $cache->get('posts', null)) === null){
    $posts = [
        ['id' => '12', 'title' => 'Post 12 Title', 'content' => 'Post 12 Content'],
        ['id' => '15', 'title' => 'Post 15 Title', 'content' => 'Post 15 Content'],
        ['id' => '18', 'title' => 'Post 18 Title', 'content' => 'Post 18 Content']
    ];
    $cache->set('posts', $posts, 120);
}

echo '<pre>'; print_r($posts) echo '</pre>';
 
$options = [
    'prefix'    => 'cache_',
    'mode'      => 0640,
];
 
$options = [
    'prefix'    => 'cache_',
    'host'          => '127.0.0.1',
    'port'          => 11211,
    'weight'        => 1,
    'raw'           => false,
    'default_ttl'   => 60,
];
 
$options = [
    'prefix'    => 'cache_',
    'dsn'           => 'mysql:host=localhost;dbname=test',
    'username'      => null,
    'password'      => null,
    'charset'       => 'utf8mb4',
    'collation'     => 'utf8mb4_general_ci',
    'table'         => 'cache'
];
 
$options = [
    'prefix'    => 'cache_',
    'host'      => '127.0.0.1',
    'password'  => null,
    'port'      => 6379,
    'timeout'   => 0,
    'database'  => 0
];
 
$options = [
    'prefix'    => 'cache_', // Cache Name Prefix
    'default_ttl'   => 60, // Used if ttl is NULL or not specified.
];