PHP code example of mk4u / cache

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

    

mk4u / cache example snippets


> [
>    //extension of cache files
>    'ext' =>'cache',
>    //directory where the cache will be stored, if it does not exist create it.
>    'dir' => '/cache',
>    //cache lifetime in seconds (default 5 minutes.)
>    'ttl' => 300
> ]
> 



// Cache driver configuration
$config = [
    'ext' => 'txt', // Extension of cache files.
    'dir' => '/cache', // Directory where the cache will be stored
    'ttl' => 3600 // Cache lifetime in seconds.
];

// Create an instance of the file cache driver.
$cache = Mk4U\Cache\CacheFactory::create('file', $config);



// Cache driver configuration
$config = [
    'ttl' => 3600 // cache lifetime in seconds (default 5 minutes.)
];

// Create an instance of the APCu cache driver.
$cache = Mk4U\Cache\CacheFactory::create('apcu', $config);

// Store the value in the cache
$cache->set('my_key', 'Hello, World!');

// Retrieve cache value
$cachedValue = $cache->get('my_key', 'Default value');

echo $cachedValue; // Print: Hello, World!

// Delete the value from the cache
$cache->delete('my_key');

// checks if a value exists
return $cache->has('my_key'); //false

// Clear the entire cache
$cache->clear();

$values = [
    'key1' => 'Value 1',
    'key2' => 'Value 2'
];

$cache->setMultiple($values);

$keys = ['key1', 'key2'];
$cachedValues = $cache->getMultiple($keys, 'Default value');

print_r($cachedValues); // Print stored values

$keysToDelete = ['key1', 'key2'];
$cache->deleteMultiple($keysToDelete);