php-solution/app-cache-bundle
Composer 安装命令:
composer require php-solution/app-cache-bundle
包简介
Symfony bundle for using Redis as main cache on app.
README 文档
README
This bundle allows developer to use Redis as main cache provider on Symfony application.
Better Performance:
- Bundle composer.json includes
ext-redison require part, because phpredis lib allow you better performance using Redis on app. - For serialization please install and enable php extension igbinary.
Redis Clients
You can specify Redis Client as service on Symfony DI.
As default new DI Reference will created via factory \PhpSolution\Utils\ClientFactory::createRedisClient() and set correct serializer option for \Redis.
Example of configuration:
app_cache:
redis_clients:
general:
public: false
id: 'app_cache.redis_clients.general'
class: 'Redis'
dsn: 'redis://localhost:6379/db_name'
options: ~
On app
/* @var $client \Redis */
$client = $this->container->get('app_cache.redis_clients.general');
$client->set('key', 'value');
$string = $client->get('key'); // $string === 'value'
Integration with SymfonyFrameworkBundle
Cache Spool
You can use Redis client service as SF cache pool provider
Example of app_cache bundle configuration:
app_cache:
redis_clients:
general:
id: 'app_cache.redis_clients.general'
dsn: 'redis://localhost:6379/data_base_name'
Example of FrameworkBundle configuration:
framework:
cache:
pools:
cache_pools.redis.general:
public: false
default_lifetime: 0
adapter: 'cache.adapter.redis'
provider: 'app_cache.redis_clients.general' # Redis client service id
Cache providers
SymfonyFrameworkBundle allows you to cache:
- validation mapper cache (on prod use file_cache)
- serializer mapper cache (on prod use file_cache)
- annotations (you can use php_array, )
- templating (use only for php template engine, see more)
Use Redis for validation mapper cache:
app_cache:
redis_clients:
general:
id: 'app_cache.redis_clients.general'
dsn: 'redis://localhost:6379/db_name'
framework:
validator:
enabled: true
client_id: 'app_cache.redis_clients.general'
namespace: 'sf_validator'
framework:
validation:
enable_annotations: true
cache: 'app_cache.framework.validator_mapping_cache'
Use Redis for serializer mapper cache:
app_cache:
redis_clients:
general:
id: 'app_cache.redis_clients.general'
dsn: 'redis://localhost:6379/db_name'
framework:
serializer:
enabled: true
client_id: 'app_cache.redis_clients.general'
namespace: 'sf_serializer'
framework:
validation:
enable_annotations: true
cache: 'app_cache.framework.validator_mapping_cache'
Swiftmailer Spool
You can use Redis as storage for spool swiftmailer messages.
AppCacheBundle configuration:
app_cache:
redis_clients:
general:
id: 'app_cache.redis_clients.general'
dsn: 'redis://localhost:6379/data_base_name'
swiftmailer:
enabled: true
client_id: 'app_cache.redis_clients.general'
SwiftmailerBundle configuration:
swiftmailer:
spool:
type: 'service'
id: 'app_cache.cacheable.swiftmailer_spool'
You can customize spool service:
app_cache:
swiftmailer:
enabled: true
client_id: 'app_cache.redis_clients.general'
cache_key: 'swiftmailer_spool'
Sessions Handler
You can use Redis as storage for session.
Bundle includes SessionHandle, on DI 'app_cache.cacheable.session_handler' which injects \Redis client and stores sessions on \Redis.
Example of AppCacheBundle configuration:
app_cache:
redis_clients:
general:
id: 'app_cache.redis_clients.general'
dsn: 'redis://localhost:6379/db_name'
session:
enabled: true
client_id: 'app_cache.redis_clients.general'
Example of FrameworkBundle configuration:
framework:
session:
handler_id: 'app_cache.cacheable.session_handler'
You can customize Session Handler:
app_cache:
session:
enabled: true
client_id: 'app_cache.redis_clients.general'
ttl: 86400
cache_key: 'sfs_'
Doctrine Cache Providers
You can specify services as Doctrine cache providers. Example of configurations:
app_cache:
redis_clients:
general:
id: 'app_cache.redis_clients.general'
dsn: 'redis://localhost:6379/db_name'
doctrine:
providers:
general:
id: 'app_cache.doctrine_providers.redis'
class: 'Doctrine\Common\Cache\RedisCache'
client_id: 'app_cache.redis_clients.general'
namespace: 'doctrine'
Correct Doctrine Cache Driver configuration
Correct configuration for doctrine:
app_cache:
redis_clients:
general:
id: 'app_cache.redis_clients.general'
dsn: 'redis://localhost:6379/db_name'
doctrine:
providers:
general:
id: 'app_cache.doctrine_providers.redis'
client_id: 'app_cache.redis_clients.general'
namespace: 'doctrine'
parameters:
doctrine_cache.metadata_cache_driver: {type: 'service', id: 'app_cache.doctrine_providers.redis'}
doctrine_cache.query_cache_driver: {type: 'service', id: 'app_cache.doctrine_providers.redis'}
doctrine_cache.result_cache_driver: {type: 'service', id: 'app_cache.doctrine_providers.redis'}
doctrine_cache.slc_driver: {type: 'service', id: 'app_cache.doctrine_providers.redis'}
doctrine:
orm:
entity_managers:
default:
metadata_cache_driver: '%doctrine_cache.metadata_cache_driver%'
query_cache_driver: '%doctrine_cache.query_cache_driver%'
result_cache_driver: '%doctrine_cache.result_cache_driver%'
second_level_cache:
region_cache_driver: '%doctrine_cache.slc_driver%'
regions:
concurrent_entity_region:
type: 'filelock'
cache_driver: '%doctrine_cache.slc_driver%'
entity_region:
lifetime: 0
cache_driver: '%doctrine_cache.slc_driver%'
Doctrine Cache Driver configuration (ONLY FOR BASIC USAGE!!!)
This Bundle allows you to specify cache provider for:
- metadata_cache
- result_cache
- query_cache
- second_level_cache
This functionality works like on SncRedisBundle and assign Doctrine cache provider automatically, but use only alias for cache provider, not creates separate provider.
Example of configuration:
app_cache:
redis_clients:
general:
id: 'app_cache.redis_clients.general'
dsn: 'redis://localhost:6379/db_name'
doctrine:
providers:
general:
id: 'app_cache.doctrine_providers.redis'
client_id: 'app_cache.redis_clients.general'
cache:
metadata_cache:
provider_id: 'app_cache.doctrine_providers.redis'
entity_managers: ['default']
Bundle Extension create new alias for services (patter name: doctrine.orm.%s_%s) using as cache_driver.
For example doctrine.orm.default_metadata_cache - service name for metadata_cache for default entity manager.
Use this feature only for basic configuration of Doctrine, because:
- you cannot rewrite for env's cache driver params
- you cannot specify
second_level_cacheregions parameters - this bundle must be specified on AppKernel after DoctrineBundle
Twig cache
Twig use Filesystem cache as provider for cache templates. You must use Filesystem cache (not Redis and other provider) and for best performance please configure PHP opcache.
Additional info about Twig cache on link
Full Default Configuration
app_cache:
redis_clients:
general:
id: 'app_cache.redis_clients.general'
class: 'Redis'
dsn: 'redis://localhost:6379/db_name'
options: ~
framework:
validator:
enabled: true
client_id: 'app_cache.redis_clients.general'
namespace: 'sf_validator'
serializer:
enabled: true
client_id: 'app_cache.redis_clients.general'
namespace: 'sf_serializer'
swiftmailer:
enabled: true
client_id: 'app_cache.redis_clients.general'
cache_key: 'swiftmailer_spool'
session:
enabled: true
client_id: 'app_cache.redis_clients.general'
ttl: 86400
cache_key: 'sfs_'
doctrine:
providers:
general:
id: 'app_cache.doctrine_providers.redis'
class: 'Doctrine\Common\Cache\RedisCache'
client_id: 'app_cache.redis_clients.general'
namespace: 'doctrine'
cache:
metadata_cache:
provider_id: 'app_cache.doctrine_providers.redis'
entity_managers: ['default']
document_managers: ['default']
result_cache:
provider_id: 'app_cache.doctrine_providers.redis'
entity_managers: ['default']
document_managers: ['default']
query_cache:
provider_id: 'app_cache.doctrine_providers.redis'
entity_managers: ['default']
document_managers: ['default']
second_level_cache:
provider_id: 'app_cache.doctrine_providers.redis'
entity_managers: ['default']
document_managers: ['default']
Working with not prod environments
Test environment
The test environment must be the same as production environment. Therefore, for test environment, it is recommended to clean the cache before starting the test cases.
Cleaning cache with listeners. phpunit.xml example:
<phpunit>
...
<listeners>
...
<listener class="PhpSolution\FunctionalTest\PHPUnit\Listener\CommandLauncher">
<arguments>
<string>doctrine:cache:clear-metadata</string>
<array>
<element key="--flush">
<null/>
</element>
</array>
</arguments>
</listener>
<listener class="PhpSolution\FunctionalTest\PHPUnit\Listener\CommandLauncher">
<arguments>
<string>doctrine:cache:clear-query</string>
<array>
<element key="--flush">
<null/>
</element>
</array>
</arguments>
</listener>
<listener class="PhpSolution\FunctionalTest\PHPUnit\Listener\CommandLauncher">
<arguments>
<string>doctrine:cache:clear-result</string>
<array>
<element key="--flush">
<null/>
</element>
</array>
</arguments>
</listener>
...
</listeners>
</phpunit>
Dev environment
For dev environment, it is not recommended to use caching
php-solution/app-cache-bundle 适用场景与选型建议
php-solution/app-cache-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.64k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 php-solution/app-cache-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 php-solution/app-cache-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5.64k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-04-04