Skip to content

Instantly share code, notes, and snippets.

@marcghorayeb
Last active December 17, 2015 17:09
Show Gist options
  • Save marcghorayeb/5644257 to your computer and use it in GitHub Desktop.
Save marcghorayeb/5644257 to your computer and use it in GitHub Desktop.
Filter Lithium's SQL describe method so that the schema can be cached and retrieved quickly
<?php
// I have this in my bootstrap/cache.php file
Dispatcher::applyFilter('run', function ($self, $params, $chain) {
foreach (Connections::get() as $connection) {
$connection = Connections::get($connection);
$connection->applyFilter('describe', function($self, $params, $chain) {
if (!empty($params['fields'])) {
$fields = $params['fields'];
return $self->invokeMethod('_instance', array('schema', compact('fields')));
}
$key = 'SC.Schema.' . $self->invokeMethod('_entityName', array($params['entity']));
$schema = Cache::read('read_only', $key);
if ($schema) {
$fields = $schema;
return $self->invokeMethod('_instance', array('schema', compact('fields')));
}
$schema = $chain->next($self, $params, $chain);
Cache::write('default', $key, $schema->fields(), '+1 day');
return $schema;
});
}
});
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment