Skip to content

Commit

Permalink
Adding caching for database describes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuswilms committed Jan 13, 2015
1 parent b8e720d commit 71a6bba
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions app/config/bootstrap/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
*/

use lithium\storage\Cache;
use lithium\storage\cache\adapter\Apc;
use lithium\core\Libraries;
use lithium\core\Environment;
use lithium\action\Dispatcher;
use lithium\storage\cache\adapter\Apc;
use lithium\data\Connections;
use lithium\data\source\Database;

/**
* Configuration
Expand Down Expand Up @@ -49,14 +51,17 @@
* Applies caching to neuralgic points of the framework but only when we are running
* in production. This is also a good central place to add your own caching rules.
*
* Here we cache paths for auto-loaded and service-located classes.
* A couple of caching rules are already defined below:
* 1. Cache paths for auto-loaded and service-located classes.
* 2. Cache describe calls on all connections that use a `Database` based adapter.
*
* @see lithium\core\Environment
* @see lithium\core\Libraries
*/
if (!Environment::is('production')) {
return;
}

Dispatcher::applyFilter('run', function($self, $params, $chain) {
$cacheKey = 'core.libraries';

Expand All @@ -72,4 +77,25 @@
return $result;
});

Dispatcher::applyFilter('run', function($self, $params, $chain) {
foreach (Connections::get() as $name) {
if (!(($connection = Connections::get($name)) instanceof Database)) {
continue;
}
$connection->applyFilter('describe', function($self, $params, $chain) use ($name) {
if ($params['fields']) {
return $chain->next($self, $params, $chain);
}
$cacheKey = "data.connections.{$name}.sources.{$params['entity']}.schema";

return Cache::read('default', $cacheKey, array(
'write' => function() use ($self, $params, $chain) {
return array('+1 day' => $chain->next($self, $params, $chain));
}
));
});
}
return $chain->next($self, $params, $chain);
});

?>

0 comments on commit 71a6bba

Please sign in to comment.