speedwork/cache

This package is abandoned and no longer maintained. No replacement package was suggested.

Provides easy to use caching for Speedwork, built on top of the doctrine/cache package

v1.0.2 2016-09-30 06:55 UTC

This package is not auto-updated.

Last update: 2018-07-09 02:28:01 UTC


README

==================================================== codecov StyleCI SensioLabsInsight Latest Stable Version Latest Unstable Version License Total Downloads Build status Build Status

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

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Make your changes
  4. Run the tests, adding new ones for your own code if necessary (phpunit)
  5. Commit your changes (git commit -am 'Added some feature')
  6. Push to the branch (git push origin my-new-feature)
  7. Create new Pull Request