summaryrefslogtreecommitdiff
path: root/idiorm.php
diff options
context:
space:
mode:
authorPeter Ivanov <[email protected]>2014-06-20 15:19:47 +0300
committerSimon Holywell <[email protected]>2014-06-22 01:03:11 +0100
commite7b77adc4378c9a7581d7793a7e6717cbadc10f8 (patch)
tree91561424fb6b0a10a45b70c323df01d4cc4c3beb /idiorm.php
parent00c6f6cce5f31c239c217a3a21e4ba96e9b1fc91 (diff)
Added custom caching functions
This is a combination of 20 commits: added cache callback #212 added test Added text for custom cache formating and tests added caching_auto_clear option moved custom cache test for php 5.3+ fixed ConfigTest.php tabs to spaces formating formating added `create_cache_key` callback option added $table_name to clear cache function added $table_name to _create_cache_key added missing params added $table_name to cache_query_result formating tabs added $table_name to check_query_cache unify cache parameters order `table_name` is more important than `connection_name` formating
Diffstat (limited to 'idiorm.php')
-rw-r--r--idiorm.php34
1 files changed, 24 insertions, 10 deletions
diff --git a/idiorm.php b/idiorm.php
index 47ccfab..8a3854d 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -72,6 +72,7 @@
'logging' => false,
'logger' => null,
'caching' => false,
+ 'caching_auto_clear' => false,
'return_result_sets' => false,
);
@@ -1758,7 +1759,10 @@
/**
* Create a cache key for the given query and parameters.
*/
- protected static function _create_cache_key($query, $parameters) {
+ protected static function _create_cache_key($query, $parameters, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) {
+ if(isset(self::$_config[$connection_name]['create_cache_key']) and is_callable(self::$_config[$connection_name]['create_cache_key'])){
+ return call_user_func_array(self::$_config[$connection_name]['create_cache_key'], array($query, $parameters, $table_name, $connection_name));
+ }
$parameter_string = join(',', $parameters);
$key = $query . ':' . $parameter_string;
return sha1($key);
@@ -1768,8 +1772,10 @@
* Check the query cache for the given cache key. If a value
* is cached for the key, return the value. Otherwise, return false.
*/
- protected static function _check_query_cache($cache_key, $connection_name = self::DEFAULT_CONNECTION) {
- if (isset(self::$_query_cache[$connection_name][$cache_key])) {
+ protected static function _check_query_cache($cache_key, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) {
+ if(isset(self::$_config[$connection_name]['check_query_cache']) and is_callable(self::$_config[$connection_name]['check_query_cache'])){
+ return call_user_func_array(self::$_config[$connection_name]['check_query_cache'], array($cache_key, $table_name, $connection_name));
+ } elseif (isset(self::$_query_cache[$connection_name][$cache_key])) {
return self::$_query_cache[$connection_name][$cache_key];
}
return false;
@@ -1778,15 +1784,20 @@
/**
* Clear the query cache
*/
- public static function clear_cache() {
+ public static function clear_cache($table_name = null, $connection_name = self::DEFAULT_CONNECTION) {
self::$_query_cache = array();
+ if(isset(self::$_config[$connection_name]['clear_cache']) and is_callable(self::$_config[$connection_name]['clear_cache'])){
+ return call_user_func_array(self::$_config[$connection_name]['clear_cache'], array($table_name, $connection_name));
+ }
}
/**
* Add the given value to the query cache.
*/
- protected static function _cache_query_result($cache_key, $value, $connection_name = self::DEFAULT_CONNECTION) {
- if (!isset(self::$_query_cache[$connection_name])) {
+ protected static function _cache_query_result($cache_key, $value, $table_name = null, $connection_name = self::DEFAULT_CONNECTION) {
+ if(isset(self::$_config[$connection_name]['cache_query_result']) and is_callable(self::$_config[$connection_name]['cache_query_result'])){
+ return call_user_func_array(self::$_config[$connection_name]['cache_query_result'], array($cache_key, $value, $table_name, $connection_name));
+ } elseif (!isset(self::$_query_cache[$connection_name])) {
self::$_query_cache[$connection_name] = array();
}
self::$_query_cache[$connection_name][$cache_key] = $value;
@@ -1801,8 +1812,8 @@
$caching_enabled = self::$_config[$this->_connection_name]['caching'];
if ($caching_enabled) {
- $cache_key = self::_create_cache_key($query, $this->_values);
- $cached_result = self::_check_query_cache($cache_key, $this->_connection_name);
+ $cache_key = self::_create_cache_key($query, $this->_values, $this->_table_name, $this->_connection_name);
+ $cached_result = self::_check_query_cache($cache_key, $this->_table_name, $this->_connection_name);
if ($cached_result !== false) {
return $cached_result;
@@ -1818,7 +1829,7 @@
}
if ($caching_enabled) {
- self::_cache_query_result($cache_key, $rows, $this->_connection_name);
+ self::_cache_query_result($cache_key, $rows, $this->_table_name, $this->_connection_name);
}
// reset Idiorm after executing the query
@@ -1986,7 +1997,10 @@
}
$success = self::_execute($query, $values, $this->_connection_name);
-
+ $caching_auto_clear_enabled = self::$_config[$this->_connection_name]['caching_auto_clear'];
+ if($caching_auto_clear_enabled){
+ self::clear_cache($this->_table_name, $this->_connection_name);
+ }
// If we've just inserted a new record, set the ID of this object
if ($this->_is_new) {
$this->_is_new = false;