PHP code example of phossa / phossa-cache

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

    

phossa / phossa-cache example snippets


    /*
     * construct the driver manually
     */
    $driver = new Driver\FilesystemDriver([
        'hash_level'    => 1,
        'file_pref'     => 'cache.',
        'dir_root'      => '/var/tmp/cache',
    ]);
    

    // default memcache driver
    $driver = new Driver\MemcacheDriver([
        'server' => [ '127.0.0.1', 11211 ]
    ]);

    // set a fallback filesystem driver
    $driver->setFallback(new Driver\FilesystemDriver([
        'dir_root' => '/var/tmp/cache'
    ]));

    $cache = new CachePool($driver);
    

    /*
     * set the composite driver
     */
    $driver = new Driver\CompositeDriver(
        // front-end driver
        new Driver\MemcacheDriver([
            'server' => [ '127.0.0.1', 11211 ]
        ]),

        // backend driver
        new Driver\FilesystemDriver([
            'dir_root' => '/var/tmp/cache'
        ]),

        // other settings
        [
            // if size > 10k, stores at backend only
            'tester' => function($item) {
                if (strlen($item->get()) > 10240) return false;
                return true;
            }
        ]
    );
    

  /*
   * set the logger
   */
  $cache->setLogger($psrLogger);
  $cache->log('info', 'this is an info');
  

  /*
   * the third argument is used for configuring CachePool
   */
  $cache = new CachePool($driver, [],
      'logger' => $psrLogger
  );
  $cache->log('info', 'this is an info');
  

  /*
   * create cache pool, exceptions may thrown here
   */
  $cache = new CachePool();
  $cache->setLogger($psrLogger);

  $item = $cache->getItem('widget_list');
  $val  = $item->get();
  if ($cache->hasError()) {
      $cache->log('error', $cache->getError());
      $widget_list = compute_expensive_widget_list();
      $item->set($widget_list);
      $item->expiresAfter(3600); // expires after an hour
      $cache->save($item);
      if ($cache->hasError()) $cache->log('error', $cache->getError());
  } else {
      $widget_list = $val;
  }
  

    /*
     * use the default FilesystemDriver which also set default cache
     * directory to sys_get_temp_dir() .'/cache'
     */
    $cache = new CachePool();

    $item = $cache->getItem('widget_list');
    if (!$item->isHit()) {
        $value = compute_expensive_widget_list();
        $item->set($value);
        $cache->save($item);
    }
    $widget_list = $item->get();
    

    $driver = new Driver\FilesystemDriver([
        'hash_level'    => 1, // subdirectory hash levels
        'file_pref'     => 'cache.', // cache file prefix
        'file_suff'     => '.txt',   // cache file suffix
        'dir_root'      => '/var/tmp/cache' // reset cache root
    ]);

    $cache = new CachePool($driver);
    

    /*
     * SerializeExtension is the default ext, always used.
     * Second argument is an array of ExtensionInterface or config array
     */
    $cache = new CachePool(
        $driver,
        [
            new Extension\BypassExtension(),
            new Extension\StampedeExtension(['probability' => 80 ])
        ]
    );
    

    $cache = new CachePool($driver);
    $cache->addExtension(new Extension\BypassExtension());
    

    // hierarchy key
    $item = $cache->getItem('mydomain/host1/newfile_xxx');

    // ending '/' means delete the hierarchy structure
    $cache->deleteItem('mydomain/host1/');