PHP code example of parshikovpavel / array-cache

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

    

parshikovpavel / array-cache example snippets


final class CacheTest extends TestCase
{
    private $itemPool;

    protected function setUp(): void
    {
        $this->itemPool = new \ppCache\CacheItemPool();
    }
    
    /* ... */
}

final class CacheTest extends TestCase
{
    /* ... */
    
    public function testFeature(): void
    {
        $client = new Client($this->itemPool);
        
        /* ... */
    }
}

final class Client {

    private $itemPool;

    public function __construct(\Psr\Cache\CacheItemPoolInterface $itemPool)
    {
        $this->itemPool = $itemPool;
    }

    private function getValue(string $key, int $ttl = 3600)
    {
        $item = $this->itemPool->getItem($key);
        if (!$item->isHit()) {
            $value = $this->compute();
            $item->set($value);
            $item->expiresAfter($ttl);
            $this->itemPool->save($item);
        }
        return $item->get();
    }

    /* ... */
}

final class CacheTest extends TestCase
{
    private $cache;

    protected function setUp(): void
    {
        $this->cache = new \ppCache\Cache();
    }
    
    /* ... */
}

final class CacheTest extends TestCase
{
    /* ... */
    
    public function testFeature(): void
    {
        $client = new Client($this->cache);
        
        /* ... */
    }
}

final class Client
{
    private $cache;

    public function __construct(\Psr\SimpleCache\CacheInterface $cache)
    {
        $this->cache = $cache;
    }

    private function getValue(string $key, int $ttl = 3600)
    {
        if (null === ($value = $this->cache->get($key))) {
            $value = $this->compute();
            $this->cache->set($key, $value, $ttl);
        }

        return $value;
    }

    /* ... */
}

final class CacheTest extends TestCase
{
    private $countingCache;

    protected function setUp(): void
    {
        $this->countingCache = new \ppCache\CountingCache();
    }
    
    /* ... */
}

final class CacheTest extends TestCase
{
    /* ... */
    
    public function testFeature(): void
    {
        $client = new Client($this->countingCache);
        
        /* ... */
    }
}

final class Client
{
    private $countingCache;

    public function __construct(\ppCache\CountingCache $countingCache)
    {
        $this->countingCache = $countingCache;
    }

    private function changeValue(string $key): void
    {
        /* ... */ 
        
        $newValue = $this->countingCache->increment($key);
        
        /* ... */
        
        $newValue = $this->countingCache->decrement($key);
        
        /* ... */
    }

    /* ... */
}