PHP code example of decodelabs / stash

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

    

decodelabs / stash example snippets


use DecodeLabs\Stash;

$myCache = Stash::load('MyCache');

if(!$cache->has('myValue')) {
    $cache->set('myValue', [1, 2, 3]);
}

$total = 0;

foreach($cache->get('myValue', []) as $number) {
    $total += $number;
}

$cache->delete('myValue');

$myValue = $myCache->fetch('myValue', function() {
    return [1, 2, 3]; // Only called if key not found in cache
});

if(!isset($myCache['myValue'])) {
    $myCache['myValue'] = 'Hello world';
}

echo $myCache['myValue'];
unset($MyCache['myValue']);

$item = $myCache->myValue;

if(!$item->isHit()) {
    $item->set('Hello world');
}

echo $item->get();
$item->delete();

interface Config
{
    public function getDriverFor(string $namespace): ?string;
    public function isDriverEnabled(string $driver): bool;
    public function getAllDrivers(): array;
    public function getDriverSettings(string $driver): ?array;

    public function getPileUpPolicy(string $namespace): ?PileUpPolicy;
    public function getPreemptTime(string $namespace): ?int;
    public function getSleepTime(string $namespace): ?int;
    public function getSleepAttempts(string $namespace): ?int;
}

Stash::setConfig(new MyConfig());

namespace MyApp;

use DecodeLabs\Archetype;
use DecodeLabs\Stash\Store;
use DecodeLabs\Stash\Store\Generic;

class MyCache extends Generic
{

    public function getMyData(): string
    {
        return $this->fetch('myData', function() {
            return 'Hello world';
        });
    }
}

Archetype::map(Store::class, namespace::class);

$myCache = Stash::load('MyCache'); // Will now use MyApp\MyCache