Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Adding support for tables and columns list caching #30

Closed
wants to merge 2 commits into from

3 participants

Fernando Carlétti Kohana Builds Woody Gilk
Fernando Carlétti

Hello,

Well, I did this little trick to save some queries on a production environment.

Now all tables and columns will be cached in the cache mechanism of user choice.

config/database.php
@@ -24,9 +24,14 @@
24 24 'password' => FALSE,
25 25 'persistent' => FALSE,
26 26 ),
27   - 'table_prefix' => '',
28   - 'charset' => 'utf8',
29   - 'caching' => FALSE,
  27 + 'table_prefix' => '',
1
Woody Gilk Owner
shadowhand added a note

Whitespace looks broken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
config/database.php
@@ -49,8 +54,13 @@
49 54 *
50 55 * string identifier set the escaping identifier
51 56 */
52   - 'table_prefix' => '',
53   - 'charset' => 'utf8',
54   - 'caching' => FALSE,
  57 + 'table_prefix' => '',
1
Woody Gilk Owner
shadowhand added a note

Ditto.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Fernando Carlétti

Ooops... Fixed it!

Kohana Builds
Collaborator

Build Scheduled

Woody Gilk
Owner

This doesn't belong in the Database layer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 69 additions and 6 deletions. Show diff stats Hide diff stats

  1. +53 0 classes/Kohana/Database/MySQL.php
  2. +16 6 config/database.php
53 classes/Kohana/Database/MySQL.php
@@ -322,6 +322,28 @@ public function rollback()
322 322
323 323 public function list_tables($like = NULL)
324 324 {
  325 + if ($this->_config['cache_tables'])
  326 + {
  327 + $key = 'kohana_tables_';
  328 + $key .= $this->_config['connection']['hostname'];
  329 + $key .= $this->_config['connection']['database'];
  330 + $key .= $this->_config['connection']['username'];
  331 +
  332 + try
  333 + {
  334 + $cache = Cache::instance(isset($this->_config['cache_group']) ? $this->_config['cache_group'] : NULL);
  335 + $data = $cache->get($key);
  336 +
  337 + if ($data !== NULL)
  338 + {
  339 + return $data;
  340 + }
  341 + }
  342 + catch (Cache_Exception $e) {
  343 + throw new Database_Exception('Could not send tables to cache. Verify your configuration.');
  344 + }
  345 + }
  346 +
325 347 if (is_string($like))
326 348 {
327 349 // Search for table names
@@ -339,11 +361,37 @@ public function list_tables($like = NULL)
339 361 $tables[] = reset($row);
340 362 }
341 363
  364 + if (isset($cache))
  365 + {
  366 + $cache->set($key, $tables, isset($this->_config['caching_lifetime']) ? $this->_config['caching_lifetime'] : Cache::DEFAULT_EXPIRE);
  367 + }
  368 +
342 369 return $tables;
343 370 }
344 371
345 372 public function list_columns($table, $like = NULL, $add_prefix = TRUE)
346 373 {
  374 + if ($this->_config['cache_columns'])
  375 + {
  376 + $key = 'kohana_columns_'.$table;
  377 + $key .= ( ! is_null($like)) ? $like : '';
  378 + $key .= $add_prefix ? 'true' : 'false';
  379 +
  380 + try
  381 + {
  382 + $cache = Cache::instance(isset($this->_config['cache_group']) ? $this->_config['cache_group'] : NULL);
  383 + $data = $cache->get($key);
  384 +
  385 + if ($data !== NULL)
  386 + {
  387 + return $data;
  388 + }
  389 + }
  390 + catch (Cache_Exception $e) {
  391 + throw new Database_Exception('Could not send table columns to cache. Verify your configuration.');
  392 + }
  393 + }
  394 +
347 395 // Quote the table name
348 396 $table = ($add_prefix === TRUE) ? $this->quote_table($table) : $table;
349 397
@@ -421,6 +469,11 @@ public function list_columns($table, $like = NULL, $add_prefix = TRUE)
421 469 $columns[$row['Field']] = $column;
422 470 }
423 471
  472 + if (isset($cache))
  473 + {
  474 + $cache->set($key, $columns, isset($this->_config['caching_lifetime']) ? $this->_config['caching_lifetime'] : Cache::DEFAULT_EXPIRE);
  475 + }
  476 +
424 477 return $columns;
425 478 }
426 479
22 config/database.php
@@ -24,9 +24,14 @@
24 24 'password' => FALSE,
25 25 'persistent' => FALSE,
26 26 ),
27   - 'table_prefix' => '',
28   - 'charset' => 'utf8',
29   - 'caching' => FALSE,
  27 + 'table_prefix' => '',
  28 + 'charset' => 'utf8',
  29 + 'caching' => FALSE,
  30 + 'cache_tables' => TRUE,
  31 + 'cache_columns' => TRUE,
  32 + 'cache_group' => 'default',
  33 + 'caching_lifetime' => 3600,
  34 + 'profiling' => TRUE,
30 35 ),
31 36 'alternate' => array(
32 37 'type' => 'PDO',
@@ -49,8 +54,13 @@
49 54 *
50 55 * string identifier set the escaping identifier
51 56 */
52   - 'table_prefix' => '',
53   - 'charset' => 'utf8',
54   - 'caching' => FALSE,
  57 + 'table_prefix' => '',
  58 + 'charset' => 'utf8',
  59 + 'caching' => FALSE,
  60 + 'cache_tables' => TRUE,
  61 + 'cache_columns' => TRUE,
  62 + 'cache_group' => 'default',
  63 + 'caching_lifetime' => 3600,
  64 + 'profiling' => TRUE,
55 65 ),
56 66 );

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.