Pop\Cache

The popphp/pop-cache component is a caching component that provides different adapters to cache data and have it persist for a certain length of time.

Installation

Install it directly into your project:

composer require popphp/pop-cache

Or, include it in your composer.json file:

{
    "require": {
        "popphp/pop-cache": "3.0.*",
    }
}

PHP 7

While this component has been updated and tested to work with PHP 7, please note:

  • Due to the unavailability or instability of the apc/apcu/apc_bc extensions, the APC class adapter may not function properly in PHP 7.
  • Due to the unavailability or instability of the memcache/memcached extensions, the Memcache & Memcached class adapter may not function properly in PHP 7.

Basic Use

Each adapter object can be created and passed configuration parameters specific to that adapter:

APC

use Pop\Cache\Adapter;

// Create an APC cache adapter object, with a 5 minute lifetime
$apcCache = new Adapter\Apc(300);

File

use Pop\Cache\Adapter;

// Create a file cache adapter object, with a 5 minute lifetime
$cacheAdapter = new Adapter\File('/path/to/my/cache/dir', 300);

Memcache

use Pop\Cache\Adapter;

// Create a Memcache cache adapter object, with a 5 minute lifetime
$cacheAdapter = new Adapter\Memcache(300);

Memcached

use Pop\Cache\Adapter;

// Create a Memcached cache adapter object, with a 5 minute lifetime
$cacheAdapter = new Adapter\Memcached(300);

Redis

use Pop\Cache\Adapter;

// Create a Redis cache adapter object, with a 5 minute lifetime
$cacheAdapter = new Adapter\Redis(300);

Session

use Pop\Cache\Adapter;

// Create a session cache adapter object, with a 5 minute lifetime
$cacheAdapter = new Adapter\Session(300);

SQLite

use Pop\Cache\Adapter;

// Create a database cache adapter object, with a 5 minute lifetime
$cacheAdapter = new Adapter\Sqlite('/path/to/my/.htcachedb.sqlite', 300);

You can then pass any of the above cache adapter objects into the main cache object to begin storing and recalling data.

use Pop\Cache\Cache;

$cache = new Cache($cacheAdapter);

// Save some data to the cache
$cache->save('foo', $myData);

// Recall that data later in the app.
// Returns false is the data does not exist or has expired.
$foo = $cache->load('foo');

To remove data from cache, you call the remove method:

$cache->remove('foo');

And to clear all data from cache, you call the clear method:

$cache->clear();