Last active
December 17, 2015 17:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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