PHP Cache library using mechanisms as originally proposed by Anthony Ferrara.
All the credit for the cache mechanisms go to @ircmaxell. Please go and read his blog post explaining why cache should be implemented this way.
Requires PHP 5.5 or later. There are no plans to support PHP 5.4 or PHP 5.3. In case this is an obstacle for you, conversion should be no problem. The library is very small.
It is installable and autoloadable via Composer as genkgo/cache.
To run the unit tests at the command line, issue phpunit -c tests/
. PHPUnit is required.
This library attempts to comply with PSR-1, PSR-2, and PSR-4. If you notice compliance oversights, please send a patch via pull request.
Create an adapter that implements the CacheAdapterInterface. A simple array adapter would look as follows. The array adapter is also shipped with this library.
<?php
namespace My\Namespace;
use Genkgo\Cache\CacheAdapterInterface;
class ArrayAdapter implements CacheAdapterInterface
{
private $data = [];
public function set($key, $value)
{
$this->data[$key] = $value;
}
public function get($key)
{
if ($this->exists($key)) {
return $this->data[$key];
}
}
public function delete($key)
{
if ($this->exists($key)) {
unset($this->data[$key]);
}
}
private function exists($key)
{
return isset($this->data[$key]) || array_key_exists($key, $this->data);
}
}
To use your adapter, inject it into another object and start using the api. If you do not want any cache, but your class relies on a cache adapter being there, inject the NullAdapter.
- Found a bug? Please try to solve it yourself first and issue a pull request. If you are not able to fix it, at least give a clear description what goes wrong. We will have a look when there is time.
- Want to see a feature added, issue a pull request and see what happens. You could also file a bug of the missing feature and we can discuss how to implement it.