Commit 899c53fa authored by romanb's avatar romanb

updated caching docs

parent 70addc55
......@@ -52,10 +52,11 @@ $conn = Doctrine_Manager::connection(new PDO('sqlite::memory:'));
$cacheDriver = new Doctrine_Cache_Sqlite(array('connection' => $conn));
</code>
++ Caching queries
++ Query Cache & Result Cache
+++ Introduction
Doctrine provides means for caching DQL queries. Caching DQL queries can greatly increase performance. Consider the standard workflow of DQL query execution:
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:
# Init new DQL query
# Parse DQL query
......@@ -66,19 +67,60 @@ Doctrine provides means for caching DQL queries. Caching DQL queries can greatly
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:
# Init new DQL query
# If DQL query exists in cache return the cached result set, otherwise do normal phases 2...6 and save the result set into cache.
# Execute the SQL query (grabbed from the cache)
# Build the result set
# Return the result set
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.
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.
When using a result cache things get even better. Then your query process looks as follows (assuming a valid cache entry is found):
# Init new DQL query
# Return the result set
As you can see, the result cache implies the query cache shown previously.
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.
+++ Query Cache
++++ Using the query cache
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.
Setting a manager level query cache driver:
<code type="php">
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine::ATTR_QUERY_CACHE, $cacheDriver);
</code>
Setting a connection level cache driver:
<code type="php">
$manager = Doctrine_Manager::getInstance();
$conn = $manager->openConnection('pgsql://user:pass@localhost/test');
$conn->setAttribute(Doctrine::ATTR_QUERY_CACHE, $cacheDriver);
</code>
++++ Fine-tuning
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:
<code type="php">
$query = new Doctrine_Query();
$query->useQueryCache(new Doctrine_Cache_Apc());
</code>
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.
+++ Result Cache
+++ Using a cache driver
++++ Using the result cache
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.
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.
Setting a manager level cache driver:
<code type="php">
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine::ATTR_CACHE, $cacheDriver);
$manager->setAttribute(Doctrine::ATTR_RESULT_CACHE, $cacheDriver);
</code>
Setting a connection level cache driver:
......@@ -86,15 +128,15 @@ Setting a connection level cache driver:
$manager = Doctrine_Manager::getInstance();
$conn = $manager->openConnection('pgsql://user:pass@localhost/test');
$conn->setAttribute(Doctrine::ATTR_CACHE, $cacheDriver);
$conn->setAttribute(Doctrine::ATTR_RESULT_CACHE, $cacheDriver);
</code>
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.
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.
<code type="php">
$manager = Doctrine_Manager::getInstance();
// set the lifespan as one hour (60 seconds * 60 minutes = 1 hour = 3600 secs)
$manager->setAttribute(Doctrine::ATTR_CACHE_LIFESPAN, 3600);
$manager->setAttribute(Doctrine::ATTR_RESULT_CACHE_LIFESPAN, 3600);
</code>
Now as we have set a cache driver for use we can make a DQL query to use it:
<code type="php">
......@@ -105,28 +147,28 @@ $query->select('b.title, COUNT(c.id) count')
->from('Blog b')
->leftJoin('b.Comments c')
->limit(10)
->useCache(true);
->useResultCache(true);
$entries = $query->execute();
</code>
+++ Fine-tuning
++++ Fine-tuning
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:
<code type="php">
$query = new Doctrine_Query();
$query->useCache(new Doctrine_Cache_Apc());
$query->useResultCache(new Doctrine_Cache_Apc());
</code>
Also you can override the lifespan attribute by calling setCacheLifeSpan():
Also you can override the lifespan attribute by calling setResultCacheLifeSpan():
<code type="php">
$query = new Doctrine_Query();
// set the lifespan as half an hour
$query->setCacheLifeSpan(60 * 30);
$query->setResultCacheLifeSpan(60 * 30);
</code>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment