Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adding support for tables and columns list caching #30

Closed
wants to merge 2 commits into from

3 participants

@xurumelous

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 @@
'password' => FALSE,
'persistent' => FALSE,
),
- 'table_prefix' => '',
- 'charset' => 'utf8',
- 'caching' => FALSE,
+ 'table_prefix' => '',
@shadowhand Owner

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 @@
*
* string identifier set the escaping identifier
*/
- 'table_prefix' => '',
- 'charset' => 'utf8',
- 'caching' => FALSE,
+ 'table_prefix' => '',
@shadowhand Owner

Ditto.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@xurumelous

Ooops... Fixed it!

@Kohana-Builds
Collaborator

Build Scheduled

@shadowhand
Owner

This doesn't belong in the Database layer.

@shadowhand shadowhand closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 25, 2012
  1. @xurumelous
  2. @xurumelous

    Fixing whitespaces

    xurumelous authored
This page is out of date. Refresh to see the latest.
Showing with 69 additions and 6 deletions.
  1. +53 −0 classes/Kohana/Database/MySQL.php
  2. +16 −6 config/database.php
View
53 classes/Kohana/Database/MySQL.php
@@ -322,6 +322,28 @@ public function rollback()
public function list_tables($like = NULL)
{
+ if ($this->_config['cache_tables'])
+ {
+ $key = 'kohana_tables_';
+ $key .= $this->_config['connection']['hostname'];
+ $key .= $this->_config['connection']['database'];
+ $key .= $this->_config['connection']['username'];
+
+ try
+ {
+ $cache = Cache::instance(isset($this->_config['cache_group']) ? $this->_config['cache_group'] : NULL);
+ $data = $cache->get($key);
+
+ if ($data !== NULL)
+ {
+ return $data;
+ }
+ }
+ catch (Cache_Exception $e) {
+ throw new Database_Exception('Could not send tables to cache. Verify your configuration.');
+ }
+ }
+
if (is_string($like))
{
// Search for table names
@@ -339,11 +361,37 @@ public function list_tables($like = NULL)
$tables[] = reset($row);
}
+ if (isset($cache))
+ {
+ $cache->set($key, $tables, isset($this->_config['caching_lifetime']) ? $this->_config['caching_lifetime'] : Cache::DEFAULT_EXPIRE);
+ }
+
return $tables;
}
public function list_columns($table, $like = NULL, $add_prefix = TRUE)
{
+ if ($this->_config['cache_columns'])
+ {
+ $key = 'kohana_columns_'.$table;
+ $key .= ( ! is_null($like)) ? $like : '';
+ $key .= $add_prefix ? 'true' : 'false';
+
+ try
+ {
+ $cache = Cache::instance(isset($this->_config['cache_group']) ? $this->_config['cache_group'] : NULL);
+ $data = $cache->get($key);
+
+ if ($data !== NULL)
+ {
+ return $data;
+ }
+ }
+ catch (Cache_Exception $e) {
+ throw new Database_Exception('Could not send table columns to cache. Verify your configuration.');
+ }
+ }
+
// Quote the table name
$table = ($add_prefix === TRUE) ? $this->quote_table($table) : $table;
@@ -421,6 +469,11 @@ public function list_columns($table, $like = NULL, $add_prefix = TRUE)
$columns[$row['Field']] = $column;
}
+ if (isset($cache))
+ {
+ $cache->set($key, $columns, isset($this->_config['caching_lifetime']) ? $this->_config['caching_lifetime'] : Cache::DEFAULT_EXPIRE);
+ }
+
return $columns;
}
View
22 config/database.php
@@ -24,9 +24,14 @@
'password' => FALSE,
'persistent' => FALSE,
),
- 'table_prefix' => '',
- 'charset' => 'utf8',
- 'caching' => FALSE,
+ 'table_prefix' => '',
+ 'charset' => 'utf8',
+ 'caching' => FALSE,
+ 'cache_tables' => TRUE,
+ 'cache_columns' => TRUE,
+ 'cache_group' => 'default',
+ 'caching_lifetime' => 3600,
+ 'profiling' => TRUE,
),
'alternate' => array(
'type' => 'PDO',
@@ -49,8 +54,13 @@
*
* string identifier set the escaping identifier
*/
- 'table_prefix' => '',
- 'charset' => 'utf8',
- 'caching' => FALSE,
+ 'table_prefix' => '',
+ 'charset' => 'utf8',
+ 'caching' => FALSE,
+ 'cache_tables' => TRUE,
+ 'cache_columns' => TRUE,
+ 'cache_group' => 'default',
+ 'caching_lifetime' => 3600,
+ 'profiling' => TRUE,
),
);
Something went wrong with that request. Please try again.