Symfony meets APC (Alternative PHP Cache)

php中文网
发布: 2016-06-23 14:30:57
原创
978人浏览过

up to last week i was exclusively an xcache user if we talk about php accelerators. recently i needed to use apc with a symfony application. as symfony offers nice apc integration it went quite smooth.

Note that just enabling PHP accelerator (any) improves performance because of the opcode caching. That's why it should always be used on the production environment. Most of accelerators also offer API which enables us to cache anything.

PHP Accelerators in symfony

Taking advantage of the most popular accelerators is really easy in symfony. We are able to change caching strategies for several factories (view, internationalization, routing). We can also cache Doctrine's DQL queries and results.

Symfony not only offers APC support with sfAPCCache class but there are also drivers for XCache (sfXCacheCache), EAccelerator (sfEacceleratorCache), memcache (sfMemcacheCache) and SQLite (sfSQLiteCache). We can also quite easily implement our own driver by extending sfCache class.

This short tutorial could be also used for any other accelerator. It's just a matter of replacing sfAPCCache/Doctrine_Query_Cache with appropriate classes.

立即学习PHP免费学习笔记(深入)”;

Enabling APC in Factories

All caching strategies are set to sfFileCache by default. We are able to change the settings for routing, view and i18n in a factory file (i.e. apps/frontend/config/factories.yml):

all:  routing:    class: sfPatternRouting    param:      generate_shortest_url:            true      extra_parameters_as_query_string: true      cache:        class: sfAPCCache        param:          automatic_cleaning_factor: 0          lifetime:                  31556926  view_cache:    class: sfAPCCache  i18n:    param:      cache:        class: sfAPCCache        param:          automatic_cleaning_factor: 0          lifetime:                  31556926
登录后复制

From now on our routing, view cache and translations will be stored in a memory instead of a hard drive. This way symfony makes a lot less disk operations (which are slow).

Enabling DQL and Result Cache in Doctrine

Enabling query cache in Doctrine is a quite safe operation. As long as we use prepared statements and don't create queries by string concatenation we don't have to worry. I think query cache can be enabled in most well written projects.

In symfony Doctrine is configured in configureDoctrine() method of the project configuration class (config/ProjectConfiguration.class.php). Enabling query cache is a matter of setting ATTR_QUERY_CACHE attribute:

PHP多文件上传插件
PHP多文件上传插件

PHP多文件上传插件

PHP多文件上传插件 149
查看详情 PHP多文件上传插件

/** * @param Doctrine_Manager $manager * @return null */public function configureDoctrine(Doctrine_Manager $manager){  $manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, new Doctrine_Cache_Apc());}
登录后复制

登录后复制

Enabling result cache might be tricky. It really depends on the project. Various result sets could have different life times. Also, result cannot be cached if we work with quickly changing data ("once it's retrieved it's outdated" kind of thing). Enabling result cache for everything in most situations won't be the best solution:

$manager->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE, new Doctrine_Cache_Apc());
登录后复制

It's worth to mention that both query and result caches can be enabled not only on a manager level but also on the connection and query levels:

// Connection level result cache$connection->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE, new Doctrine_Cache_Apc());// Query level query cache$query = Doctrine_Query::create()  ->useQueryCache(new Doctrine_Cache_Apc());// Query level result cache$query = Doctrine_Query::create()  ->useResultCache(new Doctrine_Cache_Apc());
登录后复制

Doctrine's documentation offers detailed description of both query and result caches: Query Cache & Result Cache.

The Future of APC

Long time ago I chose XCache because at that time it was better maintained and there was no performance difference. Now that APC is actively developed and there are plans to include it in PHP core I have to reconsider my decision.

What do you use? Why? Did you run any benchmarks?

 

From: http://www.zalas.eu/symfony-meets-apc-alternative-php-cache

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号