PHP code example of vectorface / cache

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

    

vectorface / cache example snippets


use Vectorface\Cache\PHPCache;

// PHPCache is a trivial array-backed cache.
$cache = new PHPCache();
$cache->get("foo"); // null, because we just created this cache.
$cache->get("foo", "dflt"); // "dflt"; same as above, but with our own default
$cache->set("foo", "bar"); // returns true if set. This cache always succeeds.
$cache->get("foo"); // "bar", because we just set it.

use Vectorface\Cache\APCCache;
use Vectorface\Cache\PHPCache;
use Vectorface\Cache\TempFileCache;

// The Memcache, Redis, and SQL-backed caches work the same way, but need a client/connection first.
$caches = [new APCCache(), new PHPCache(), new TempFileCache()];
foreach ($caches as $cache) {
    // Look ma! Same interface!
    $cache->set('foo', 'bar');
    $cache->get('foo');
}

use Vectorface\Cache\APCCache;
use Vectorface\Cache\AtomicCounter;

$cache = new APCCache();
assert($cache instanceof AtomicCounter);

// Can increment and decrement by key, defaults to steps of 1
assert($cache->increment("counter") === 1);
assert($cache->increment("counter") === 2);
assert($cache->decrement("counter") === 1);

// Can step by arbitrary amounts
assert($cache->increment("counter", 5) === 6);
assert($cache->decrement("counter", 2) === 4);

use Vectorface\Cache\APCCache;
use Vectorface\Cache\MCCache;
use Vectorface\Cache\TempFileCache;
use Vectorface\Cache\TieredCache;

$memcache = new Memcache();
$memcache->addServer("127.0.0.1");
$cache = new TieredCache([
    new APCCache(),
    new MCCache($memcache),
    new TempFileCache(),
]);

$cache->get("foo"); // Tries each cache in order and returns the first hit. Returns the default if none hit.
$cache->set("foo", "bar"); // Sets the value in every cache. Succeeds if at least one cache accepted it.
$cache->get("foo"); // Tries each cache in order. The fastest should hit and return quickly.
$cache->delete("foo"); // Deletes from every cache. Succeeds only if every cache succeeded.

use Vectorface\Cache\APCCache;
use Vectorface\Cache\CacheHelper;

$cache = new APCCache();

// Calls Report::build(2026, 9) only on a cache miss, and caches the result for 600 seconds.
$report = CacheHelper::fetch($cache, "report:2026-09", [Report::class, 'build'], [2026, 9], 600);

use Vectorface\Cache\LogDecorator;
use Vectorface\Cache\PHPCache;

$cache = new LogDecorator(new PHPCache(), $psr3Logger, 'debug');
$cache->set("foo", "bar"); // logged
$cache->get("foo");        // logged, as a HIT

use Psr\SimpleCache\CacheInterface;
use Vectorface\Cache\PHPCache;
use Vectorface\Cache\SimpleCacheAdapter;

$psr16Cache = new SimpleCacheAdapter(new PHPCache());

assert($psr16Cache instanceof CacheInterface);