speedwork / cache
Provides easy to use caching for Speedwork, built on top of the doctrine/cache package
Requires
- php: >=5.6
- doctrine/cache: ^1.6
Requires (Dev)
- phpunit/phpunit: ~5.5
- satooshi/php-coveralls: 2.*
Replaces
- speedwork/container: v1.0.2
This package is not auto-updated.
Last update: 2018-07-09 02:28:01 UTC
README
====================================================
This service provider for Speedwork uses the Cache classes from [Doctrine
Common][] to provide a cache
service to a Speedwork application, and
other service providers.
Install
If you haven't got composer:
```shell
$ wget http://getcomposer.org/composer.phar
````
Add speedwork/cache
to your composer.json
:
$ php composer.phar require speedwork/cache
Configuration
If you only need one application wide cache, then it's sufficient to
only define a default cache, by setting the default
key in cache.options
.
The cache definition is an array of options, with driver
being the
only mandatory option. All other options in the array, are treated as
constructor arguments to the driver class.
The cache named default
is the cache available through the app's
cache
service.
<?php $app = new Speedwork\Application; $app->register(new \Speedwork\Cache\CacheServiceProvider, array( 'cache.options' => array("default" => array( "driver" => "apc" )) ));
The driver name can be either:
- A fully qualified class name
- A simple identifier like "apc", which then gets translated to
\Doctrine\Common\Cache\ApcCache
. - A Closure, which returns an object implementing
\Doctrine\Common\Cache\Cache
.
This cache is then available through the cache
service, and provides
an instance of Doctrine\Common\Cache\Cache
:
if ($app['cache']->contains('foo')) { echo $app['cache']->fetch('foo'), "<br>"; } else { $app['cache']->save('foo', 'bar'); }
To configure multiple caches, define them as additional keys in
cache.options
:
$app->register(new \Speedwork\Cache\CacheServiceProvider, array( 'cache.options' => array( 'default' => array('driver' => 'apc'), 'file' => array( 'driver' => 'filesystem', 'directory' => '/tmp/myapp' ), 'global' => array( 'driver' => function() { $redis = new \Doctrine\Common\Cache\RedisCache; $redis->setRedis($app['redis']); return $redis; } ) ) ));
Usage
All caches (including the default) are then available via the cache
service:
$app['cache.file']->save('foo', 'bar');
#Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Make your changes
- Run the tests, adding new ones for your own code if necessary (
phpunit
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request