From 2ff21cd93da9719fda05a50edd6760ca8f0bccfe Mon Sep 17 00:00:00 2001 From: Shay Anderson Date: Wed, 14 Jun 2017 17:09:15 -0400 Subject: [PATCH] add DB cache read/write control with custom keys --- app/mod/IndexController.php | 5 +++-- app/vendor/Eco/System/Database.php | 33 +++++++++++++++++++++++++++--- app/vendor/Eco/version.php | 2 +- docs/cache.md | 18 +++++++--------- docs/database.md | 22 ++++++++++++++++++++ 5 files changed, 63 insertions(+), 17 deletions(-) diff --git a/app/mod/IndexController.php b/app/mod/IndexController.php index 2cf772e..2603351 100644 --- a/app/mod/IndexController.php +++ b/app/mod/IndexController.php @@ -9,7 +9,8 @@ class IndexController */ public function home() { - view()->set('status', 'ready'); - view('home'); + view('home', [ + 'status' => 'ready' + ]); } } \ No newline at end of file diff --git a/app/vendor/Eco/System/Database.php b/app/vendor/Eco/System/Database.php index 47cb5fb..3f60c66 100644 --- a/app/vendor/Eco/System/Database.php +++ b/app/vendor/Eco/System/Database.php @@ -165,9 +165,11 @@ private function __hasConn($connection_id) */ private function __initCache(\Eco\Cache &$cache, $query, $params, $key_add = null) { - // auto set cache key - $key = trim($query) . ( $params ? '~' . $key_add . implode('~', $params) : null ); - $cache->key($cache->encodeKey($key), true); // set key + if(!$cache->hasKey()) // auto set cache key + { + $key = trim($query) . ( $params ? '~' . $key_add . implode('~', $params) : null ); + $cache->key($cache->encodeKey($key), true); // set key + } // set subdir for connection ID $cache->path('ecodb' . DIRECTORY_SEPARATOR . $this->__conn_id); @@ -397,6 +399,11 @@ public function get($table_or_sql, $params = null) $this->__initCache($cache, $table_or_sql, $this->__prepParams($param_index, func_get_args())); + if(isset($cache->db_query)) // stop + { + return $cache; + } + if($cache->has()) // cache exists { return $cache->get(); @@ -434,6 +441,11 @@ public function getAll($table_or_sql) $this->__initCache($cache, $table_or_sql, $this->__prepParams($param_index, func_get_args())); + if(isset($cache->db_query)) // stop + { + return $cache; + } + if($cache->has()) { return $cache->get(); @@ -634,6 +646,11 @@ public function pagination($query, $params = null) $this->__initCache($cache, $query, $this->__prepParams($param_index, func_get_args()), 'pg~'); + if(isset($cache->db_query)) // stop + { + return $cache; + } + if($cache->has()) { return $cache->get(); @@ -675,6 +692,11 @@ public function query($query, $params = null) $this->__initCache($cache, $query, $this->__prepParams($param_index, func_get_args())); + if(isset($cache->db_query)) // stop + { + return $cache; + } + if($cache->has()) { return $cache->get(); @@ -714,6 +736,11 @@ public function queryArrayParam($query, $params) $this->__initCache($cache, $query, $params); + if(isset($cache->db_query)) // stop + { + return $cache; + } + if($cache->has()) { return $cache->get(); diff --git a/app/vendor/Eco/version.php b/app/vendor/Eco/version.php index fdcacdf..7ec77f3 100644 --- a/app/vendor/Eco/version.php +++ b/app/vendor/Eco/version.php @@ -11,4 +11,4 @@ /** * Eco version */ -const ECO_VERSION = '1.3.1'; \ No newline at end of file +const ECO_VERSION = '1.3.2'; \ No newline at end of file diff --git a/docs/cache.md b/docs/cache.md index 57fcf04..9c94ee5 100644 --- a/docs/cache.md +++ b/docs/cache.md @@ -8,22 +8,18 @@ The `\Eco\Cache` class can be used for server-side caching, for example: $cache = new \Eco\Cache('article-14'); // check if cache exists -if(!$cache->has()) +if($cache->has()) // cache exists { - // cache does not exists - $data = 'the cache value'; - - // write cache - $cache->set($data); + $data = $cache->get(); } -else +else // cache does not exists { - // cache exists - $data = $cache->get(); + $data = 'the cache value'; + + $cache->set($data); // write cache } -// output -echo $data; +echo $data; // output ``` All cache data is serialized by default so different PHP types can be cached, for example: ```php diff --git a/docs/database.md b/docs/database.md index fdcb75e..8834c76 100644 --- a/docs/database.md +++ b/docs/database.md @@ -339,6 +339,28 @@ Caching can be used with these methods: - [`queryArrayParam()`](#execute-a-query) - [`pagination()`](#pagination) +Custom cache keys can be used, for example: +```php +$row = db()->get(new \Eco\Cache('table-1'), 'table WHERE x = ?', 1); +``` + +> It is possible to control a database cache without executing the cache read/write. For example, if you want to delete the cache without executing read/write it can be accomplished using: +```php +$cache = new \Eco\Cache; +// set property db_query to stop read/write +$cache->db_query = false; + +// no read/write is executed in call (no DB load) +// will not return row(s): +db()->get($cache, 'table WHERE x = ?', 1); + +// delete the cache with zero load on DB +$cache->delete(); +// or get cache info with zero DB load +$key = $cache->getKey(); +$path = $cache->getFilePath(); +``` + ### Other Methods The following methods are also available