PHP code example of phossa2 / cache

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

    

phossa2 / cache example snippets


  /*
   * cache dir default to local sys_get_temp_dir() . '/cache'
   */
  $cache = new CachePool();

  $item = $cache->getItem('/data.cache');
  if (!$item->isHit()) {
      $value = calcuate_data();
      $item->set($value);
      $cache->save($item);
  }
  $data = $item->get();
  

  use Phossa2\Cache\Driver\StorageDriver;
  use Phossa2\Storage\Storage;
  use Phossa2\Storage\Filesystem;
  use Phossa2\Storage\Driver\LocalDriver;

  $driver = new StorageDriver(
      new Storage('/', new Filesystem(new LocalDriver(sys_get_temp_dir()))),
      '/cache'
  );

  $cache = new CachePool($driver);
  

  /*
   * DistributedExpiration extension
   */
  use Phossa2\Cache\CachePool;
  use Phossa2\Cache\Extension\DistributedExpiration;

  $cache = new CachePool();
  $cache->addExtension(new DistributedExpiration());