10.04.2018 Views

Doctrine_manual-1-2-en

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 23: Caching 326<br />

// ...<br />

$manager->setAttribute(<strong>Doctrine</strong>_Core::ATTR_RESULT_CACHE_LIFESPAN, 3600);<br />

Now as we have set a cache driver for use we can make a DQL query use it by calling the<br />

useResultCache() method:<br />

Fetch blog post titles and the number of comm<strong>en</strong>ts:<br />

Listing<br />

23-11<br />

$q = <strong>Doctrine</strong>_Query::create()<br />

->select('b.title, COUNT(c.id) count')<br />

->from('BlogPost b')<br />

->leftJoin('b.Comm<strong>en</strong>ts c')<br />

->limit(10)<br />

->useResultCache(true);<br />

$blogPosts = $q->execute();<br />

Fine Tuning<br />

In the previous chapter we used global caching attributes. These attributes can be overrid<strong>en</strong><br />

at the query level. You can override the cache driver by calling useCache() and pass it an<br />

instance of a valid <strong>Doctrine</strong> cache driver.<br />

Listing<br />

23-12<br />

$q = <strong>Doctrine</strong>_Query::create()<br />

->useResultCache(new <strong>Doctrine</strong>_Cache_Apc());<br />

Also you can override the lifespan attribute by calling setResultCacheLifeSpan():<br />

Listing<br />

23-13<br />

$q = <strong>Doctrine</strong>_Query::create()<br />

->setResultCacheLifeSpan(60 * 30);<br />

Conclusion<br />

Using the caching feature of <strong>Doctrine</strong> is highly recomm<strong>en</strong>ded in both developm<strong>en</strong>t and<br />

production <strong>en</strong>vironm<strong>en</strong>ts. There are no adverse affects to using it and it will only help the<br />

performance of your application.<br />

The caching feature is the second to last feature we will discuss in this book before wrapping<br />

things up by discussing things like the technologies used (page 368) in <strong>Doctrine</strong>, coding<br />

standards (page 381) and unit testing (page 356). Lets move on to discuss the last feature of<br />

<strong>Doctrine</strong>, Migrations (page 327).<br />

----------------- Brought to you by

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!