PHP code example of shasoft / psr-cache

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

    

shasoft / psr-cache example snippets


    // Создать объект для работы с КЭШем
    $cache = new Shasoft\PsrCache\CacheItemPool(
        new Shasoft\PsrCache\Adapter\CacheAdapter()
    );
    // Получить элемент КЭШа
    $itemCache = cache->getItem('myKey');
    // Если элемент не найден в КЖШе
    if( !$itemCache->isHit() ) {
        // то установить значение
        $itemCache->set('valueCacheItem');
        // и сохранить в КЭШ
        $cache->save($itemCache);
    }
    // Вывести значение
    echo $itemCache->get();
 

abstract class CacheAdapter
{
    // Получить значения (Если $has=true, то только проверить наличие значения. 
    // Т.е. вернуть либо false, либо true)
    abstract public function get(array $keys, bool $has): array;
    // Удалить указанные значения
    abstract public function delete(array $keys): bool;
    // Удалить все значения
    abstract public function clear(): bool;
    // Сохранить элементы ['key1'=>'value11, 'key2'=>'value12, ...]
    // Возвращает список ключей успешно сохраненных элементов
    abstract public function save(array $items): array;
}