Changeset 3279 for branches/1.0/manual

Show
Ignore:
Timestamp:
11/30/07 22:54:37 (13 months ago)
Author:
jwage
Message:

Merged 3274 to 3278

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/1.0/manual/docs/en/caching.txt

    r2682 r3279  
    5353</code> 
    5454 
    55 ++ Caching queries 
     55++ Query Cache & Result Cache 
     56 
    5657+++ Introduction 
    5758 
    58 Doctrine provides means for caching DQL queries. Caching DQL queries can greatly increase performance. Consider the standard workflow of DQL query execution: 
     59Doctrine provides means for caching the results of the DQL parsing process, as well as the end results of DQL queries (the data). These two caching mechanisms can greatly increase performance. Consider the standard workflow of DQL query execution: 
    5960 
    6061# Init new DQL query 
     
    6768Now these phases can be very time consuming, especially phase 4 which sends the query to your database server. When Doctrine query cache is being used only the following phases occur: 
    6869# Init new DQL query 
    69 # If DQL query exists in cache return the cached result set, otherwise do normal phases 2...6 and save the result set into cache. 
     70# Execute the SQL query (grabbed from the cache) 
     71# Build the result set 
     72# Return the result set 
     73If a DQL query has a valid cache entry the cached SQL query is used, otherwise the phases 2-3 are executed normally and the result of these steps is then stored in the cache. 
     74The query cache has no disadvantages, since you always get a fresh query result. You should therefore always use it in a production environment. That said, you can easily use it during development, too. Whenever you change a DQL query and execute it the first time Doctrine sees that is has been modified and will therefore create a new cache entry, so you dont even need to invalidate the cache. It's worth noting that the effectiveness of the query cache greatly relies on the usage of prepared staments (which are used by Doctrine by default anyway). You should not directly embed dynamic query parts and always use placeholders instead. 
    7075 
    71 So not only does the DQL query cache skip the standard database query execution phases it also skips the building of the result set and parsing of the DQL query. You should always consider using query caching for queries that are issued often. 
     76When using a result cache things get even better. Then your query process looks as follows (assuming a valid cache entry is found): 
     77# Init new DQL query 
     78# Return the result set 
     79As you can see, the result cache implies the query cache shown previously. 
     80You should always consider using a result cache if the data returned by the query does not need to be up-to-date at any time. 
    7281 
    73 +++ Using a cache driver 
     82+++ Query Cache 
    7483 
    75 You can set a connection or manager level cache driver by using Doctrine::ATTR_CACHE. Setting a connection level cache driver means that all queries executed with this connection use the specified cache driver whereas setting a manager level cache driver means that all connections (unless overridden at connection level) will use the given cache driver. 
     84++++ Using the query cache 
    7685 
    77 Setting a manager level cache driver: 
     86You can set a connection or manager level query cache driver by using Doctrine::ATTR_QUERY_CACHE. Setting a connection level cache driver means that all queries executed with this connection use the specified cache driver whereas setting a manager level cache driver means that all connections (unless overridden at connection level) will use the given cache driver. 
     87 
     88Setting a manager level query cache driver: 
    7889<code type="php"> 
    7990$manager = Doctrine_Manager::getInstance(); 
    8091 
    81 $manager->setAttribute(Doctrine::ATTR_CACHE, $cacheDriver); 
     92$manager->setAttribute(Doctrine::ATTR_QUERY_CACHE, $cacheDriver); 
    8293</code> 
    8394 
     
    8798$conn    = $manager->openConnection('pgsql://user:pass@localhost/test'); 
    8899 
    89 $conn->setAttribute(Doctrine::ATTR_CACHE, $cacheDriver); 
     100$conn->setAttribute(Doctrine::ATTR_QUERY_CACHE, $cacheDriver); 
    90101</code> 
    91102 
    92 Usually the cache entries are valid for only some time. You can set global value for how long the cache entries should be considered valid by using Doctrine::ATTR_CACHE_LIFESPAN. 
     103++++ Fine-tuning 
     104 
     105In the previous chapter we used global caching attributes. These attributes can be overriden at the query level. You can override the cache driver by calling useQueryCache with a valid cacheDriver. This rarely makes sense for the query cache but is possible: 
     106 
     107<code type="php"> 
     108$query = new Doctrine_Query(); 
     109 
     110$query->useQueryCache(new Doctrine_Cache_Apc()); 
     111</code> 
     112 
     113+++ Result Cache 
     114 
     115++++ Using the result cache 
     116 
     117You can set a connection or manager level result cache driver by using Doctrine::ATTR_RESULT_CACHE. Setting a connection level cache driver means that all queries executed with this connection use the specified cache driver whereas setting a manager level cache driver means that all connections (unless overridden at connection level) will use the given cache driver. 
     118 
     119Setting a manager level cache driver: 
     120<code type="php"> 
     121$manager = Doctrine_Manager::getInstance(); 
     122 
     123$manager->setAttribute(Doctrine::ATTR_RESULT_CACHE, $cacheDriver); 
     124</code> 
     125 
     126Setting a connection level cache driver: 
     127<code type="php"> 
     128$manager = Doctrine_Manager::getInstance(); 
     129$conn    = $manager->openConnection('pgsql://user:pass@localhost/test'); 
     130 
     131$conn->setAttribute(Doctrine::ATTR_RESULT_CACHE, $cacheDriver); 
     132</code> 
     133 
     134Usually the cache entries are valid for only some time. You can set global value for how long the cache entries should be considered valid by using Doctrine::ATTR_RESULT_CACHE_LIFESPAN. 
    93135<code type="php"> 
    94136$manager = Doctrine_Manager::getInstance(); 
    95137 
    96138// set the lifespan as one hour (60 seconds * 60 minutes = 1 hour = 3600 secs) 
    97 $manager->setAttribute(Doctrine::ATTR_CACHE_LIFESPAN, 3600); 
     139$manager->setAttribute(Doctrine::ATTR_RESULT_CACHE_LIFESPAN, 3600); 
    98140</code> 
    99141Now as we have set a cache driver for use we can make a DQL query to use it: 
     
    106148      ->leftJoin('b.Comments c') 
    107149      ->limit(10) 
    108       ->useCache(true); 
     150      ->useResultCache(true); 
    109151       
    110152$entries = $query->execute(); 
    111153</code> 
    112154 
    113 +++ Fine-tuning 
     155++++ Fine-tuning 
    114156 
    115157In the previous chapter we used global caching attributes. These attributes can be overriden at the query level. You can override the cache driver by calling useCache with a valid cacheDriver: 
     
    118160$query = new Doctrine_Query(); 
    119161 
    120 $query->useCache(new Doctrine_Cache_Apc()); 
     162$query->useResultCache(new Doctrine_Cache_Apc()); 
    121163</code> 
    122164 
    123 Also you can override the lifespan attribute by calling setCacheLifeSpan(): 
     165Also you can override the lifespan attribute by calling setResultCacheLifeSpan(): 
    124166 
    125167<code type="php"> 
     
    127169 
    128170// set the lifespan as half an hour 
    129 $query->setCacheLifeSpan(60 * 30); 
     171$query->setResultCacheLifeSpan(60 * 30); 
    130172</code> 
    131173