> 백엔드 개발 > PHP 튜토리얼 > Symfony meets APC (Alternative PHP Cache)

Symfony meets APC (Alternative PHP Cache)

WBOY
풀어 주다: 2016-06-23 14:30:57
원래의
856명이 탐색했습니다.

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.

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:

/** * @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.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿