Symfony meets APC (alternative PHP Cache)

Source: Internet
Author: User
Tags apc i18n
Up-to-last week I was exclusively a XCache user if we talk about PHP accelerators. Recently I needed to the use of 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's should always being used on the production environment. Most of the accelerators also offer API which enables us to cache anything.

PHP Accelerators in Symfony

Taking advantage of the most popular accelerators are really easy in symfony. We is 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 is also drivers for XCache (Sfxcachecache), Eaccelera Tor (Sfeacceleratorcache), Memcache (Sfmemcachecache) and SQLite (Sfsqlitecache). We can also quite easily implement We own driver by extending SfCache class.

This short tutorial could is 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 is set to Sffilecache by default. We is 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'll be stored in a memory instead of a hard drive. This is symfony makes a lot less disk operations (which is slow).

Enabling DQL and Result Cache in Doctrine

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

In Symfony Doctrine was configured in Configuredoctrine () method of the project configuration class (Config/projectconfigur ation.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 has different life times. Also, result cannot is cached if we work with quickly changing data ("Once it's retrieved it ' s outdated" kind of thing). Enabling result cache for everything in situations won ' t is 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 is enabled not only on a manager level but also on the Connect Ion 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 is better maintained and there was no performance difference. Now the APC is actively developed and there am plans to include it in PHP core I has to reconsider my decision.

What does the use? Why? Did you run any benchmarks?

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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.