Changeset 3279 for branches/1.0/manual
- Timestamp:
- 11/30/07 22:54:37 (13 months ago)
- Files:
-
- 1 modified
-
branches/1.0/manual/docs/en/caching.txt (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/1.0/manual/docs/en/caching.txt
r2682 r3279 53 53 </code> 54 54 55 ++ Caching queries 55 ++ Query Cache & Result Cache 56 56 57 +++ Introduction 57 58 58 Doctrine provides means for caching DQL queries. Caching DQL queries can greatly increase performance. Consider the standard workflow of DQL query execution:59 Doctrine 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: 59 60 60 61 # Init new DQL query … … 67 68 Now 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: 68 69 # 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 73 If 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. 74 The 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. 70 75 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. 76 When 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 79 As you can see, the result cache implies the query cache shown previously. 80 You 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. 72 81 73 +++ Using a cache driver82 +++ Query Cache 74 83 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 76 85 77 Setting a manager level cache driver: 86 You 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 88 Setting a manager level query cache driver: 78 89 <code type="php"> 79 90 $manager = Doctrine_Manager::getInstance(); 80 91 81 $manager->setAttribute(Doctrine::ATTR_ CACHE, $cacheDriver);92 $manager->setAttribute(Doctrine::ATTR_QUERY_CACHE, $cacheDriver); 82 93 </code> 83 94 … … 87 98 $conn = $manager->openConnection('pgsql://user:pass@localhost/test'); 88 99 89 $conn->setAttribute(Doctrine::ATTR_ CACHE, $cacheDriver);100 $conn->setAttribute(Doctrine::ATTR_QUERY_CACHE, $cacheDriver); 90 101 </code> 91 102 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 105 In 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 117 You 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 119 Setting 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 126 Setting 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 134 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_RESULT_CACHE_LIFESPAN. 93 135 <code type="php"> 94 136 $manager = Doctrine_Manager::getInstance(); 95 137 96 138 // 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); 98 140 </code> 99 141 Now as we have set a cache driver for use we can make a DQL query to use it: … … 106 148 ->leftJoin('b.Comments c') 107 149 ->limit(10) 108 ->use Cache(true);150 ->useResultCache(true); 109 151 110 152 $entries = $query->execute(); 111 153 </code> 112 154 113 +++ Fine-tuning155 ++++ Fine-tuning 114 156 115 157 In 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: … … 118 160 $query = new Doctrine_Query(); 119 161 120 $query->use Cache(new Doctrine_Cache_Apc());162 $query->useResultCache(new Doctrine_Cache_Apc()); 121 163 </code> 122 164 123 Also you can override the lifespan attribute by calling set CacheLifeSpan():165 Also you can override the lifespan attribute by calling setResultCacheLifeSpan(): 124 166 125 167 <code type="php"> … … 127 169 128 170 // set the lifespan as half an hour 129 $query->set CacheLifeSpan(60 * 30);171 $query->setResultCacheLifeSpan(60 * 30); 130 172 </code> 131 173