PHP code example of gwa / gw-cache

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

    

gwa / gw-cache example snippets


composer 

use Gwa\Cache\Cache;

// Create a persistence layer for the cache
// Cache directory should be writable.
// (Cache will try to create it if it does not exist.)
$cachedir = __DIR__ . '/cachestore';
$persistence = new CacheDirectoryPersistence($cachedir);



// Set an optional group for the cache
$group = '';

// Set cache validity in minutes
$cacheminutes = 60 * 12;

// create a cache instance using an identifier unique to the group
$cache = new Cache('myidentifier', $group, $cacheminutes);
$cache->setPersistence($persistence);



$iscached = $cache->isCached(); // false

// write a value to the cache
$cache->set('foo');

// new object, same group and identifier
$cache2 = new Cache('myidentifier', $group, $cacheminutes);
$cache2->setPersistence($persistence);
$iscached = $cache2->isCached(); // true
$value = $cache2->get(); // 'foo'

// clear the cache
$cache2->clear();
$iscached = $cache2->isCached(); // false

$factory = new CacheFactory(new CacheDirectoryPersistence($cachedir));

$cache = $factory->create('myidentifier', $group, $cacheminutes);

$cache = new Cache('myidentifier', $group, $cacheminutes, Cache::TYPE_OBJECT);

// with a factory
$cache = $factory->create('myidentifier', $group, $cacheminutes, Cache::TYPE_OBJECT);