epiphany / service-cache-bundle
通过注释其方法来缓存或存储 Symfony2 服务的响应
Requires
- php: >=5.3.0
- symfony/monolog-bundle: 2.3.*
- symfony/symfony: 2.3.*
Requires (Dev)
- symfony/framework-bundle: 2.3.*
This package is not auto-updated.
Last update: 2024-09-24 05:51:07 UTC
README
允许缓存 Symfony2 服务方法调用。这是通过注释服务类中的所需方法来实现的。
此功能可能对以下场景有用:
- 应用程序对 API 进行昂贵的调用,这些调用可以缓存或与其他应用程序共享,从而减少整体 API 调用。
- 应用程序对服务进行耗时调用,这些调用可以缓存以提高性能。
- 作为钩入服务方法调用的手段,然后捕获其参数和返回值。
配置
这假设您正在使用 Symfony2(v2.3)应用程序,并使用 composer 进行包管理。
使用此捆绑包需要四个步骤。
- 将此包添加到您的 composer.json 文件中,并运行 composer update,将包添加到您的 AppKernel
- 更新您的应用程序的 app/autoload.php 文件,以包含对 ProxyGenerator::registerNamespace() 的调用,如下所示,以便应用程序可以加载代理类
use Epiphany\ServiceCacheBundle\Proxy\ProxyGenerator; .... ProxyGenerator::registerNamespace($loader,__DIR__); return $loader;
- 标记任何您想要注册进行缓存的服务的服务,并标记一个实现 Epiphany\ServiceCacheBundle\Cache\ServiceCacheInterface 的服务。这应在您的 service.yml/xml 文件中完成
services: # note the 'epiphany_service_cache.register' tag - this indicates our weather data # service should have some (or all) of its method calls cached weather_data_service: class: Epiphany\ServiceCacheDemoBundle\Service\WeatherDataService arguments: [] tags: - { name: epiphany_service_cache.register} # note the 'epiphany_service_cache.cache' tag - this indicates the service # should be used by the service_cache as the caching mechanism simple_cache_service: class: Epiphany\ServiceCacheDemoBundle\Service\SimpleCacheService arguments: [localhost, 12345] tags: - { name: epiphany_service_cache.cache}
- 注释任何您想要缓存的服务。
class WeatherDataService { /** * Get the weather forecast for a date and location * * @service-cache-enable * * @service-cache-key param date * @service-cache-key param location * @service-cache-key date Y-m-d-H * * @service-cache-option compressed y * * @service-cache-expires 0 * * @param DateTime $date * @param string $location * @return array forecast data */ public function forecastForDate(\DateTime $date, $location) { //....
注释
@service-cache-enable
- 这标记了一个用于 Service Cache 的方法
@service-cache-key <date|param> <value>
- 必须使用这些注释之一来定义存储数据的唯一键。任何可以转换为字符串的方法参数或当前日期(带格式字符串)都可以使用。
@service-cache-option <name> <value>
- 名称/值对将在获取/设置操作期间通过关联数组传递给缓存服务。请参阅 Epiphany\ServiceCacheBundle\Cache\ServiceCacheInterface。这允许用户传递可能特定于其缓存服务实现的额外信息,例如压缩、数据过期(如果您正在使用 MongoDB 的话是集合名称)
@service-cache-expires <n>
- 缓存数据的过期时间(秒)。零表示永不过期。
实现缓存机制
用户有权决定使用哪种缓存方式。所有要求是用户提供一个带有 'epiphany_service_cache.cache' 标记的 Symfony2 服务,该服务实现 Epiphany\ServiceCacheBundle\Cache\ServiceCacheInterface。当无法从缓存中检索数据时,getDataForKey() 方法应返回一个 null 值。
注意
此包期望容器中存在 Symfony2 的 logger 服务(通常是 monolog,但任何实现 Symfony\Component\HttpKernel\Log\LoggerInterface 的服务都可以使用)。
为了插入缓存层,此包为任何标记的服务生成代理对象,然后在服务容器的预优化编译过程中覆盖服务定义。目前,必须在应用程序的 app/cache/dsproxy 目录中手动删除代理对象,每当它们的服务的更新时。
class EpiphanyServiceCacheBundle extends Bundle { public function build(ContainerBuilder $container) { parent::build($container); // this pass will override standard service classes with our generated proxy classes $container->addCompilerPass(new OverrideServiceCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION); } }
发布说明
v1.0.2 - 2013年9月23日
- 更新了 Readme。
v1.0.1 - 2013年9月23日
- 更新了 Readme。
v1.0.0 - 2013年9月23日
- 初始提交,基本功能。