emag / cache-bundle
4.2.0
2018-10-01 09:14 UTC
Requires
- php: >=7.0
- doctrine/annotations: ~1.3
- ocramius/proxy-manager: 2.*
- psr/cache: 1.0.*
- psr/container: ^1.0
- psr/log: 1.*
- symfony/dependency-injection: >3.3
Requires (Dev)
- phing/phing: @stable
- phpmd/phpmd: @stable
- phpunit/phpunit: 5.*
- satooshi/php-coveralls: ~1.0
- sebastian/phpcpd: ~2
- squizlabs/php_codesniffer: 2.*
- symfony/cache: 3.*
- symfony/framework-bundle: @stable
- symfony/http-kernel: 3.*
- symfony/monolog-bundle: @stable
Suggests
- symfony/cache: 3.*
README
安装
为了在方法上启用缓存,您需要使用composer安装它
- 添加需求
$ composer require emag/cache-bundle
- 将以下代码添加到您的app/AppKernel.php中
class AppKernel extends Kernel { public function registerBundles() { $bundles = [ //... new Emag\CacheBundle\EmagCacheBundle(), //... ]; //... } //.... }
- 配置包所需信息
您必须配置一个符合PSR6规范的服务名称,这意味着它将需要实现Psr\Cache\CacheItemPoolInterface
# app/config/services.yml services: cache.array: class: Symfony\Component\Cache\Adapter\ArrayAdapter cache.redis: class: Symfony\Component\Cache\Adapter\RedisAdapter arguments: ['@predis']
#app/config/config.yml # eMAG CachingBundle emag_cache: provider: cache.redis ignore_namespaces: - 'Symfony\\' - 'Doctrine\\' - 'Twig_' - 'Monolog\\' - 'Swift_' - 'Sensio\\Bundle\\'
如何使用
将@Cache注解添加到您想要缓存的函数上
use Emag\CacheBundle\Annotation\Cache; /** * @Cache(cache="<put your prefix>", [key="<name of argument to include in cache key separated by comma>", [ttl=600, [reset=true ]]]) */
以下是一个服务器的示例
namespace AppCacheBundle\Service; use Emag\CacheBundle\Annotation\Cache; class AppService { /** * @Cache(cache="app_high_cpu", ttl=60) * * @return int */ public function getHighCPUOperation() { // 'Simulate a time consuming operation'; sleep(10); return 20; } }
想要贡献?
提交一个PR并加入我们的乐趣。
享受!