speedwork / cache
此包已被弃用且不再维护。未建议替换包。
为Speedwork提供易于使用的缓存,基于doctrine/cache包构建
v1.0.2
2016-09-30 06:55 UTC
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
====================================================
此Speedwork服务提供商使用[Doctrine Common][]中的Cache类为Speedwork应用程序和其他服务提供商提供cache
服务。
安装
如果您还没有composer
```shell
$ wget https://getcomposer.org.cn/composer.phar
````
将speedwork/cache
添加到您的composer.json
$ php composer.phar require speedwork/cache
配置
如果您只需要一个应用程序范围内的缓存,则只需要在cache.options
中定义默认缓存即可。
缓存定义是一个选项数组,其中driver
是唯一必需的选项。数组中的所有其他选项都视为驱动类构造函数的参数。
名为default
的缓存是通过应用程序的cache
服务可用的缓存。
<?php $app = new Speedwork\Application; $app->register(new \Speedwork\Cache\CacheServiceProvider, array( 'cache.options' => array("default" => array( "driver" => "apc" )) ));
驱动名称可以是
- 一个完全限定的类名
- 一个简单的标识符,如"apc",然后将其转换为
\Doctrine\Common\Cache\ApcCache
。 - 一个闭包,返回一个实现
\Doctrine\Common\Cache\Cache
的对象。
然后此缓存可通过cache
服务访问,并提供一个Doctrine\Common\Cache\Cache
的实例。
if ($app['cache']->contains('foo')) { echo $app['cache']->fetch('foo'), "<br>"; } else { $app['cache']->save('foo', 'bar'); }
要配置多个缓存,请在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; } ) ) ));
使用
所有缓存(包括默认缓存)都可通过cache
服务访问
$app['cache.file']->save('foo', 'bar');
## 贡献
- 将其分支
- 创建您的功能分支(
git checkout -b my-new-feature
) - 进行更改
- 运行测试,如有必要添加您自己的代码的新测试(
phpunit
) - 提交您的更改(
git commit -am 'Added some feature'
) - 推送到分支(
git push origin my-new-feature
) - 创建新的Pull Request