Skip to content

Getting Started

J0sh0nat0r edited this page Jul 23, 2017 · 4 revisions

Installation

composer require j0sh0nat0r/simple-cache (make sure to include autoload.php!)

Initialization

Step 1 - Import the Cache object

use J0sh0nat0r\SimpleCache\Cache

Step 2 - Import a driver (APC in this case)

use J0sh0nat0r\SimpleCache\Drivers\APC

Step 3 - Instantiate the Cache object

$cache = new Cache(APC::class)

Storing, Retrieving and Removing Items

Storing Items

Standard Syntax

$cache->store('foo', 'bar')
The cache now contains an item with the key foo and the value bar. Because we didn't pass a third argument (time) this value will expire in 1 hour

Array Syntax

$cache->store(['foo' => 'bar'])
The same as standard syntax, but in this case we passed an array of key-value pairs

Retrieving Items

Standard Syntax

$foo = $cache->get('foo')
$foo Will now contain the value bar (provided you followed all previous steps)

Array Syntax

$baz = $cache->get(['foo'])'
$baz Is now an array of values, keyed in the same fashion as the cache. In this case $baz['foo'] will contain bar

Removing Items

Standard Syntax

$cache->remove('foo')
The cache no-longer contains the item with the key foo

Array Syntax

$cache->remove(['foo'])
The same as standard syntax, but in this case we passed an array of keys to remove

Clearing the Cache

$cache->clear()
The cache is now empty