PHP code example of phlak / stash

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

    

phlak / stash example snippets


use PHLAK\Stash;

$stash = Stash\Cache::file($config);
$stash = Stash\Cache::memcached($config);
$stash = Stash\Cache::redis($config);
$stash = Stash\Cache::apcu($config);
$stash = Stash\Cache::ephemeral();

$stash = Stash\Cache::make($driver, $config);

$stash = Stash\Cache::file(function (): void {
    $this->setCacheDir('path/to/cache');
});

$stash = Stash\Cache::memcached(function (Memcached $memcached): void {
    $memcached->addServer('localhost', 11211);
    // $memcached->setOption(Memcached::OPT_PREFIX_KEY, 'some_prefix');
});

$stash = Stash\Cache::redis(function (Redis $redis): void {
    $redis->pconnect('localhost', 6379);
    // $redis->setOption(Redis::OPT_PREFIX, 'some_prefix');
});

$stash = Stash\Cache::apcu();

$stash = Stash\Cache::apcu(function (): void {
    $this->setPrefix('some_prefix');
});

$stash = Stash\Cache::ephemeral();

// Cache a value for 15 minutes
$stash->put('foo', 'some value', 15);

// Cache a value indefinitely
$stash->put('bar', false);

$stash->forever('foo', 'some value');

$stash->get('foo');

// Return 'default' if 'bar' doesn't exist
$stash->get('bar', 'default');

$stash->has('foo');

$stash->remember('foo', 60, function() {
    return new FooClass();
});

$stash->rememberForever('pokemon', function() {
    return new Pokemon($name, $description);
});

// Increment by 1
$stash->increment('foo');

// Increment by 10
$stash->increment('bar', 10);

 // Decrements by 1
$stash->decrement('foo');

 // Decrements by 10
$stash->decrement('bar', 10);

 // Extend the expiration by 5 minutes
$stash->touch('foo', 5);

 // Extend the expiration indefinitely
$stash->touch('bar');

// Extend the expiration of multiple items by 5 minutes
$stash->touch(['foo', 'bar', 'baz'], 5);

$stash->forget('foo');

$stash->flush();