From 4f65a003344a3aeec0c5f608e990c2860146d39e Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 26 Aug 2012 18:40:23 +0200 Subject: [PATCH 01/12] [ticket/10875] Remove useless assignment from phpbb_cache_driver_memory. sql_save() no longer takes $query_result as a reference, thus this assignment is a no-op. PHPBB3-10875 --- phpBB/includes/cache/driver/memory.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/phpBB/includes/cache/driver/memory.php b/phpBB/includes/cache/driver/memory.php index e0771ab1d3..bc08494c0c 100644 --- a/phpBB/includes/cache/driver/memory.php +++ b/phpBB/includes/cache/driver/memory.php @@ -334,8 +334,6 @@ abstract class phpbb_cache_driver_memory extends phpbb_cache_driver_base $this->_write('sql_' . $hash, $this->sql_rowset[$query_id], $ttl); - $query_result = $query_id; - return $query_id; } From e28f0aa336e4b4d8965d1025d6e0d28e6b1fcde9 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 26 Aug 2012 18:42:21 +0200 Subject: [PATCH 02/12] [ticket/10875] Fix return value of phpbb_cache_driver_null::sql_save(). phpbb_cache_driver_null::sql_save() has to return $query_result as is instead of null. PHPBB3-10875 --- phpBB/includes/cache/driver/null.php | 1 + 1 file changed, 1 insertion(+) diff --git a/phpBB/includes/cache/driver/null.php b/phpBB/includes/cache/driver/null.php index df2c6c026f..687604d14f 100644 --- a/phpBB/includes/cache/driver/null.php +++ b/phpBB/includes/cache/driver/null.php @@ -109,6 +109,7 @@ class phpbb_cache_driver_null extends phpbb_cache_driver_base */ function sql_save($query, $query_result, $ttl) { + return $query_result; } /** From 59af4d68fc6f5455e156c5a192b35ca1ffe6872b Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 26 Aug 2012 18:45:20 +0200 Subject: [PATCH 03/12] [ticket/10875] Fix phpbb_mock_cache::sql_save() to return $query_result. PHPBB3-10875 --- tests/mock/cache.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/mock/cache.php b/tests/mock/cache.php index b64c92ea89..bc18ca066b 100644 --- a/tests/mock/cache.php +++ b/tests/mock/cache.php @@ -123,6 +123,7 @@ class phpbb_mock_cache implements phpbb_cache_driver_interface } public function sql_save($query, $query_result, $ttl) { + return $query_result; } public function sql_exists($query_id) { From 4e83cd461ce2adb008889593abf352745d9adff6 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sun, 26 Aug 2012 18:46:08 +0200 Subject: [PATCH 04/12] [ticket/10875] Fix logic in phpbb_cache_driver_file::sql_save(). Previously (develop-olympus) $query_result was passed via reference into sql_save(). Now the (possibly changed) result is returned by value. Adjust logic to take this change into account correctly. PHPBB3-10875 --- phpBB/includes/cache/driver/file.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/includes/cache/driver/file.php b/phpBB/includes/cache/driver/file.php index f64a9e3ea8..d45497af56 100644 --- a/phpBB/includes/cache/driver/file.php +++ b/phpBB/includes/cache/driver/file.php @@ -383,10 +383,10 @@ class phpbb_cache_driver_file extends phpbb_cache_driver_base if ($this->_write('sql_' . md5($query), $this->sql_rowset[$query_id], $ttl + time(), $query)) { - $query_result = $query_id; + return $query_id; } - return $query_id; + return $query_result; } /** From 969369254f8860c7ab4cc5a1185f1e8ebf043b63 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Wed, 31 Oct 2012 18:14:24 -0400 Subject: [PATCH 05/12] [ticket/10875] Add comment about cache's sql_load() method. PHPBB3-10875 --- phpBB/includes/cache/driver/interface.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/phpBB/includes/cache/driver/interface.php b/phpBB/includes/cache/driver/interface.php index 847ba97262..669eb75f63 100644 --- a/phpBB/includes/cache/driver/interface.php +++ b/phpBB/includes/cache/driver/interface.php @@ -69,6 +69,12 @@ interface phpbb_cache_driver_interface /** * Load cached sql query + * + * @param string $query SQL query + * + * @return int|bool Query ID (integer) if cache contains a rowset + * for the specified query. + * False otherwise. */ public function sql_load($query); From 831d5ad5052b269ae93a2b0c5513605e8097513a Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Wed, 14 Nov 2012 03:38:11 +0100 Subject: [PATCH 06/12] [ticket/10875] Add docblocks for phpbb_cache_driver_interface PHPBB3-10875 --- phpBB/includes/cache/driver/interface.php | 37 ++++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/phpBB/includes/cache/driver/interface.php b/phpBB/includes/cache/driver/interface.php index 669eb75f63..6adc95a86a 100644 --- a/phpBB/includes/cache/driver/interface.php +++ b/phpBB/includes/cache/driver/interface.php @@ -70,41 +70,70 @@ interface phpbb_cache_driver_interface /** * Load cached sql query * - * @param string $query SQL query + * @param string $query SQL query * - * @return int|bool Query ID (integer) if cache contains a rowset - * for the specified query. - * False otherwise. + * @return int|bool Query ID (integer) if cache contains a rowset + * for the specified query. + * False otherwise. */ public function sql_load($query); /** * Save sql query + * + * @param string $query SQL query, should be used for generating storage key + * @param mixed $query_result The result from dbal::sql_query, to be passed to + * dbal::sql_fetchrow to get all rows and store them + * in cache. + * @param int $ttl Time to live, after this timeout the query should + * expire from the cache. + * @return int|mixed If storing in cache succeeded, an integer $query_id + * representing the query should be returned. Otherwise + * the original $query_result should be returned. */ public function sql_save($query, $query_result, $ttl); /** * Ceck if a given sql query exist in cache + * + * @param int $query_id + * @return bool */ public function sql_exists($query_id); /** * Fetch row from cache (database) + * + * @param int $query_id + * @return array|bool The query result if found in the cache, otherwise + * false. */ public function sql_fetchrow($query_id); /** * Fetch a field from the current row of a cached database result (database) + * + * @param int $query_id + * @param $field The name of the column. + * @return string|bool The field of the query result if found in the cache, + * otherwise false. */ public function sql_fetchfield($query_id, $field); /** * Seek a specific row in an a cached database result (database) + * + * @param int $rownum Row to seek to. + * @param int $query_id + * @return bool */ public function sql_rowseek($rownum, $query_id); /** * Free memory used for a cached database result (database) + * + * @param int $query_id + * @return bool */ public function sql_freeresult($query_id); } From ee16ed7b76315f629adc1f234d6cf3ddd9552a97 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Fri, 30 Nov 2012 11:53:19 -0500 Subject: [PATCH 07/12] [ticket/10875] Spelling fix. PHPBB3-10875 --- phpBB/includes/cache/driver/interface.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpBB/includes/cache/driver/interface.php b/phpBB/includes/cache/driver/interface.php index 6adc95a86a..26dfd497aa 100644 --- a/phpBB/includes/cache/driver/interface.php +++ b/phpBB/includes/cache/driver/interface.php @@ -63,7 +63,7 @@ interface phpbb_cache_driver_interface public function destroy($var_name, $table = ''); /** - * Check if a given cache entry exist + * Check if a given cache entry exists */ public function _exists($var_name); @@ -94,7 +94,7 @@ interface phpbb_cache_driver_interface public function sql_save($query, $query_result, $ttl); /** - * Ceck if a given sql query exist in cache + * Check if a given sql query exists in cache * * @param int $query_id * @return bool From 3eb15ab59e202f6d995f40da7cbee0056e73f5d1 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Fri, 30 Nov 2012 12:04:05 -0500 Subject: [PATCH 08/12] [ticket/10875] More documentation. PHPBB3-10875 --- phpBB/includes/cache/driver/interface.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/phpBB/includes/cache/driver/interface.php b/phpBB/includes/cache/driver/interface.php index 26dfd497aa..d403bbcd71 100644 --- a/phpBB/includes/cache/driver/interface.php +++ b/phpBB/includes/cache/driver/interface.php @@ -68,7 +68,7 @@ interface phpbb_cache_driver_interface public function _exists($var_name); /** - * Load cached sql query + * Load result of an SQL query from cache. * * @param string $query SQL query * @@ -79,7 +79,11 @@ interface phpbb_cache_driver_interface public function sql_load($query); /** - * Save sql query + * Save result of an SQL query in cache. + * + * In persistent cache stores, this function stores the query + * result to persistent storage. In other words, there is no need + * to call save() afterwards. * * @param string $query SQL query, should be used for generating storage key * @param mixed $query_result The result from dbal::sql_query, to be passed to @@ -94,7 +98,7 @@ interface phpbb_cache_driver_interface public function sql_save($query, $query_result, $ttl); /** - * Check if a given sql query exists in cache + * Check if result for a given SQL query exists in cache. * * @param int $query_id * @return bool From 1ebc6eb68bedc4d6708cb6f23959d129030cf31e Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Fri, 30 Nov 2012 12:11:52 -0500 Subject: [PATCH 09/12] [ticket/10875] Must return query result on failure. PHPBB3-10875 --- phpBB/includes/cache/driver/memory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/cache/driver/memory.php b/phpBB/includes/cache/driver/memory.php index bc08494c0c..e4da5767cf 100644 --- a/phpBB/includes/cache/driver/memory.php +++ b/phpBB/includes/cache/driver/memory.php @@ -294,7 +294,7 @@ abstract class phpbb_cache_driver_memory extends phpbb_cache_driver_base if (!preg_match('/FROM \\(?(`?\\w+`?(?: \\w+)?(?:, ?`?\\w+`?(?: \\w+)?)*)\\)?/', $query, $regs)) { // Bail out if the match fails. - return; + return $query_result; } $tables = array_map('trim', explode(',', $regs[1])); From 7bba09811c65acfd98ebf1e6626f59de7a16cbb3 Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Fri, 30 Nov 2012 12:18:33 -0500 Subject: [PATCH 10/12] [ticket/10875] Revise sql cache test. Delete data from database before retrieving it from cache, ensuring results come from cache. PHPBB3-10875 --- tests/cache/cache_test.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/cache/cache_test.php b/tests/cache/cache_test.php index c5f5fac88c..40eef91d53 100644 --- a/tests/cache/cache_test.php +++ b/tests/cache/cache_test.php @@ -89,20 +89,26 @@ class phpbb_cache_test extends phpbb_database_test_case WHERE config_name = 'foo'"; $result = $db->sql_query($sql, 300); $first_result = $db->sql_fetchrow($result); + $expected = array('config_name' => 'foo', 'config_value' => '23', 'is_dynamic' => 0); + $this->assertEquals($expected, $first_result); $this->assertFileExists($this->cache_dir . 'sql_' . md5(preg_replace('/[\n\r\s\t]+/', ' ', $sql)) . '.php'); + $sql = "DELETE FROM phpbb_config"; + $result = $db->sql_query($sql); + $sql = "SELECT * FROM phpbb_config WHERE config_name = 'foo'"; $result = $db->sql_query($sql, 300); - $this->assertEquals($first_result, $db->sql_fetchrow($result)); + $this->assertEquals($expected, $db->sql_fetchrow($result)); $sql = "SELECT * FROM phpbb_config - WHERE config_name = 'bar'"; - $result = $db->sql_query($sql, 300); + WHERE config_name = 'foo'"; + $result = $db->sql_query($sql); - $this->assertNotEquals($first_result, $db->sql_fetchrow($result)); + $no_cache_result = $db->sql_fetchrow($result); + $this->assertSame(false, $no_cache_result); $db->sql_close(); } From 0c06ac466f61cdab1c647cec23ea66ef70b2ad7e Mon Sep 17 00:00:00 2001 From: Oleg Pudeyev Date: Fri, 30 Nov 2012 12:28:13 -0500 Subject: [PATCH 11/12] [ticket/10875] Test for null cache driver and sql cache. PHPBB3-10875 --- tests/cache/cache_test.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/cache/cache_test.php b/tests/cache/cache_test.php index 40eef91d53..c904aa6c41 100644 --- a/tests/cache/cache_test.php +++ b/tests/cache/cache_test.php @@ -112,4 +112,33 @@ class phpbb_cache_test extends phpbb_database_test_case $db->sql_close(); } + + public function test_null_cache_sql() + { + $driver = new phpbb_cache_driver_null($this->cache_dir); + + global $db, $cache; + $db = $this->new_dbal(); + $cache = new phpbb_cache_service($driver); + + $sql = "SELECT * FROM phpbb_config + WHERE config_name = 'foo'"; + $result = $db->sql_query($sql, 300); + $first_result = $db->sql_fetchrow($result); + $expected = array('config_name' => 'foo', 'config_value' => '23', 'is_dynamic' => 0); + $this->assertEquals($expected, $first_result); + + $sql = "DELETE FROM phpbb_config"; + $result = $db->sql_query($sql); + + // As null cache driver does not actually cache, + // this should return no results + $sql = "SELECT * FROM phpbb_config + WHERE config_name = 'foo'"; + $result = $db->sql_query($sql, 300); + + $this->assertSame(false, $db->sql_fetchrow($result)); + + $db->sql_close(); + } } From e4d2ad6b2788d9c3c030382f5ad2f02b6b7f75db Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Sat, 1 Dec 2012 00:36:34 +0100 Subject: [PATCH 12/12] [ticket/10875] tests/cache/cache_test.php: Use single quotes where possible. PHPBB3-10875 --- tests/cache/cache_test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/cache/cache_test.php b/tests/cache/cache_test.php index c904aa6c41..285af5cd0c 100644 --- a/tests/cache/cache_test.php +++ b/tests/cache/cache_test.php @@ -94,7 +94,7 @@ class phpbb_cache_test extends phpbb_database_test_case $this->assertFileExists($this->cache_dir . 'sql_' . md5(preg_replace('/[\n\r\s\t]+/', ' ', $sql)) . '.php'); - $sql = "DELETE FROM phpbb_config"; + $sql = 'DELETE FROM phpbb_config'; $result = $db->sql_query($sql); $sql = "SELECT * FROM phpbb_config @@ -128,7 +128,7 @@ class phpbb_cache_test extends phpbb_database_test_case $expected = array('config_name' => 'foo', 'config_value' => '23', 'is_dynamic' => 0); $this->assertEquals($expected, $first_result); - $sql = "DELETE FROM phpbb_config"; + $sql = 'DELETE FROM phpbb_config'; $result = $db->sql_query($sql); // As null cache driver does not actually cache,