diff --git a/phpBB/includes/cache/driver/file.php b/phpBB/includes/cache/driver/file.php
index 0d3b06f621..f64a9e3ea8 100644
--- a/phpBB/includes/cache/driver/file.php
+++ b/phpBB/includes/cache/driver/file.php
@@ -364,7 +364,7 @@ class phpbb_cache_driver_file extends phpbb_cache_driver_base
 	/**
 	* Save sql query
 	*/
-	function sql_save($query, &$query_result, $ttl)
+	function sql_save($query, $query_result, $ttl)
 	{
 		global $db;
 
@@ -385,6 +385,8 @@ class phpbb_cache_driver_file extends phpbb_cache_driver_base
 		{
 			$query_result = $query_id;
 		}
+
+		return $query_id;
 	}
 
 	/**
diff --git a/phpBB/includes/cache/driver/interface.php b/phpBB/includes/cache/driver/interface.php
index 313a2d4b31..847ba97262 100644
--- a/phpBB/includes/cache/driver/interface.php
+++ b/phpBB/includes/cache/driver/interface.php
@@ -75,7 +75,7 @@ interface phpbb_cache_driver_interface
 	/**
 	* Save sql query
 	*/
-	public function sql_save($query, &$query_result, $ttl);
+	public function sql_save($query, $query_result, $ttl);
 
 	/**
 	* Ceck if a given sql query exist in cache
diff --git a/phpBB/includes/cache/driver/memory.php b/phpBB/includes/cache/driver/memory.php
index e25c9229a1..92971c6cb2 100644
--- a/phpBB/includes/cache/driver/memory.php
+++ b/phpBB/includes/cache/driver/memory.php
@@ -280,7 +280,7 @@ class phpbb_cache_driver_memory extends phpbb_cache_driver_base
 	/**
 	* Save sql query
 	*/
-	function sql_save($query, &$query_result, $ttl)
+	function sql_save($query, $query_result, $ttl)
 	{
 		global $db;
 
@@ -335,6 +335,8 @@ 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;
 	}
 
 	/**
diff --git a/phpBB/includes/cache/driver/null.php b/phpBB/includes/cache/driver/null.php
index c143803d0e..df2c6c026f 100644
--- a/phpBB/includes/cache/driver/null.php
+++ b/phpBB/includes/cache/driver/null.php
@@ -107,7 +107,7 @@ class phpbb_cache_driver_null extends phpbb_cache_driver_base
 	/**
 	* Save sql query
 	*/
-	function sql_save($query, &$query_result, $ttl)
+	function sql_save($query, $query_result, $ttl)
 	{
 	}
 
diff --git a/phpBB/includes/db/dbal.php b/phpBB/includes/db/dbal.php
index 159703d3be..1de236d3de 100644
--- a/phpBB/includes/db/dbal.php
+++ b/phpBB/includes/db/dbal.php
@@ -206,7 +206,7 @@ class dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_rowseek($rownum, $query_id);
 		}
@@ -256,7 +256,7 @@ class dbal
 				$this->sql_rowseek($rownum, $query_id);
 			}
 
-			if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
+			if (!is_object($query_id) && $cache->sql_exists($query_id))
 			{
 				return $cache->sql_fetchfield($query_id, $field);
 			}
diff --git a/phpBB/includes/db/firebird.php b/phpBB/includes/db/firebird.php
index 06c76fa94a..9f9b8a1abd 100644
--- a/phpBB/includes/db/firebird.php
+++ b/phpBB/includes/db/firebird.php
@@ -272,7 +272,7 @@ class dbal_firebird extends dbal
 				if ($cache_ttl)
 				{
 					$this->open_queries[(int) $this->query_result] = $this->query_result;
-					$cache->sql_save($query, $this->query_result, $cache_ttl);
+					$this->query_result = $cache->sql_save($query, $this->query_result, $cache_ttl);
 				}
 				else if (strpos($query, 'SELECT') === 0 && $this->query_result)
 				{
@@ -332,7 +332,7 @@ class dbal_firebird extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_fetchrow($query_id);
 		}
@@ -398,7 +398,7 @@ class dbal_firebird extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_freeresult($query_id);
 		}
diff --git a/phpBB/includes/db/mssql.php b/phpBB/includes/db/mssql.php
index e40510835a..bde283c3ea 100644
--- a/phpBB/includes/db/mssql.php
+++ b/phpBB/includes/db/mssql.php
@@ -162,7 +162,7 @@ class dbal_mssql extends dbal
 				if ($cache_ttl)
 				{
 					$this->open_queries[(int) $this->query_result] = $this->query_result;
-					$cache->sql_save($query, $this->query_result, $cache_ttl);
+					$this->query_result = $cache->sql_save($query, $this->query_result, $cache_ttl);
 				}
 				else if (strpos($query, 'SELECT') === 0 && $this->query_result)
 				{
@@ -234,7 +234,7 @@ class dbal_mssql extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_fetchrow($query_id);
 		}
@@ -271,7 +271,7 @@ class dbal_mssql extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_rowseek($rownum, $query_id);
 		}
@@ -310,7 +310,7 @@ class dbal_mssql extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_freeresult($query_id);
 		}
diff --git a/phpBB/includes/db/mssql_odbc.php b/phpBB/includes/db/mssql_odbc.php
index 3c9a9599ec..687bc52abc 100644
--- a/phpBB/includes/db/mssql_odbc.php
+++ b/phpBB/includes/db/mssql_odbc.php
@@ -181,7 +181,7 @@ class dbal_mssql_odbc extends dbal
 				if ($cache_ttl)
 				{
 					$this->open_queries[(int) $this->query_result] = $this->query_result;
-					$cache->sql_save($query, $this->query_result, $cache_ttl);
+					$this->query_result = $cache->sql_save($query, $this->query_result, $cache_ttl);
 				}
 				else if (strpos($query, 'SELECT') === 0 && $this->query_result)
 				{
@@ -254,7 +254,7 @@ class dbal_mssql_odbc extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_fetchrow($query_id);
 		}
@@ -295,7 +295,7 @@ class dbal_mssql_odbc extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_freeresult($query_id);
 		}
diff --git a/phpBB/includes/db/mssqlnative.php b/phpBB/includes/db/mssqlnative.php
index d35337d05b..36ff461a29 100644
--- a/phpBB/includes/db/mssqlnative.php
+++ b/phpBB/includes/db/mssqlnative.php
@@ -338,7 +338,7 @@ class dbal_mssqlnative extends dbal
 				if ($cache_ttl)
 				{
 					$this->open_queries[(int) $this->query_result] = $this->query_result;
-					$cache->sql_save($query, $this->query_result, $cache_ttl);
+					$this->query_result = $cache->sql_save($query, $this->query_result, $cache_ttl);
 				}
 				else if (strpos($query, 'SELECT') === 0 && $this->query_result)
 				{
@@ -418,7 +418,7 @@ class dbal_mssqlnative extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_fetchrow($query_id);
 		}
@@ -478,7 +478,7 @@ class dbal_mssqlnative extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_freeresult($query_id);
 		}
diff --git a/phpBB/includes/db/mysql.php b/phpBB/includes/db/mysql.php
index dbab1ec0b8..5b4ff86579 100644
--- a/phpBB/includes/db/mysql.php
+++ b/phpBB/includes/db/mysql.php
@@ -190,7 +190,7 @@ class dbal_mysql extends dbal
 				if ($cache_ttl)
 				{
 					$this->open_queries[(int) $this->query_result] = $this->query_result;
-					$cache->sql_save($query, $this->query_result, $cache_ttl);
+					$this->query_result = $cache->sql_save($query, $this->query_result, $cache_ttl);
 				}
 				else if (strpos($query, 'SELECT') === 0 && $this->query_result)
 				{
@@ -249,7 +249,7 @@ class dbal_mysql extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_fetchrow($query_id);
 		}
@@ -270,7 +270,7 @@ class dbal_mysql extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_rowseek($rownum, $query_id);
 		}
@@ -298,7 +298,7 @@ class dbal_mysql extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_freeresult($query_id);
 		}
diff --git a/phpBB/includes/db/mysqli.php b/phpBB/includes/db/mysqli.php
index fc98de31fb..1f13bd5459 100644
--- a/phpBB/includes/db/mysqli.php
+++ b/phpBB/includes/db/mysqli.php
@@ -196,7 +196,7 @@ class dbal_mysqli extends dbal
 
 				if ($cache_ttl)
 				{
-					$cache->sql_save($query, $this->query_result, $cache_ttl);
+					$this->query_result = $cache->sql_save($query, $this->query_result, $cache_ttl);
 				}
 			}
 			else if (defined('DEBUG_EXTRA'))
@@ -251,7 +251,7 @@ class dbal_mysqli extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
+		if (!is_object($query_id) && $cache->sql_exists($query_id))
 		{
 			return $cache->sql_fetchrow($query_id);
 		}
@@ -278,7 +278,7 @@ class dbal_mysqli extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
+		if (!is_object($query_id) && $cache->sql_exists($query_id))
 		{
 			return $cache->sql_rowseek($rownum, $query_id);
 		}
@@ -306,7 +306,7 @@ class dbal_mysqli extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
+		if (!is_object($query_id) && $cache->sql_exists($query_id))
 		{
 			return $cache->sql_freeresult($query_id);
 		}
diff --git a/phpBB/includes/db/oracle.php b/phpBB/includes/db/oracle.php
index 4954f4d398..de2729e973 100644
--- a/phpBB/includes/db/oracle.php
+++ b/phpBB/includes/db/oracle.php
@@ -421,7 +421,7 @@ class dbal_oracle extends dbal
 				if ($cache_ttl)
 				{
 					$this->open_queries[(int) $this->query_result] = $this->query_result;
-					$cache->sql_save($query, $this->query_result, $cache_ttl);
+					$this->query_result = $cache->sql_save($query, $this->query_result, $cache_ttl);
 				}
 				else if (strpos($query, 'SELECT') === 0 && $this->query_result)
 				{
@@ -473,7 +473,7 @@ class dbal_oracle extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_fetchrow($query_id);
 		}
@@ -525,7 +525,7 @@ class dbal_oracle extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_rowseek($rownum, $query_id);
 		}
@@ -594,7 +594,7 @@ class dbal_oracle extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_freeresult($query_id);
 		}
diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php
index 41838d2613..f0a4a7a7a2 100644
--- a/phpBB/includes/db/postgres.php
+++ b/phpBB/includes/db/postgres.php
@@ -218,7 +218,7 @@ class dbal_postgres extends dbal
 				if ($cache_ttl)
 				{
 					$this->open_queries[(int) $this->query_result] = $this->query_result;
-					$cache->sql_save($query, $this->query_result, $cache_ttl);
+					$this->query_result = $cache->sql_save($query, $this->query_result, $cache_ttl);
 				}
 				else if (strpos($query, 'SELECT') === 0 && $this->query_result)
 				{
@@ -285,7 +285,7 @@ class dbal_postgres extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_fetchrow($query_id);
 		}
@@ -306,7 +306,7 @@ class dbal_postgres extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_rowseek($rownum, $query_id);
 		}
@@ -355,7 +355,7 @@ class dbal_postgres extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_freeresult($query_id);
 		}
diff --git a/phpBB/includes/db/sqlite.php b/phpBB/includes/db/sqlite.php
index d930567773..2cf55b07e2 100644
--- a/phpBB/includes/db/sqlite.php
+++ b/phpBB/includes/db/sqlite.php
@@ -135,7 +135,7 @@ class dbal_sqlite extends dbal
 				if ($cache_ttl)
 				{
 					$this->open_queries[(int) $this->query_result] = $this->query_result;
-					$cache->sql_save($query, $this->query_result, $cache_ttl);
+					$this->query_result = $cache->sql_save($query, $this->query_result, $cache_ttl);
 				}
 				else if (strpos($query, 'SELECT') === 0 && $this->query_result)
 				{
@@ -193,7 +193,7 @@ class dbal_sqlite extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_fetchrow($query_id);
 		}
@@ -214,7 +214,7 @@ class dbal_sqlite extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_rowseek($rownum, $query_id);
 		}
@@ -242,7 +242,7 @@ class dbal_sqlite extends dbal
 			$query_id = $this->query_result;
 		}
 
-		if (isset($cache->sql_rowset[$query_id]))
+		if ($cache->sql_exists($query_id))
 		{
 			return $cache->sql_freeresult($query_id);
 		}
diff --git a/phpBB/install/install_update.php b/phpBB/install/install_update.php
index c2feaa086a..88b00f1cf1 100644
--- a/phpBB/install/install_update.php
+++ b/phpBB/install/install_update.php
@@ -349,7 +349,7 @@ class install_update extends module
 
 				// We are directly within an update. To make sure our update list is correct we check its status.
 				$update_list = ($request->variable('check_again', false, false, phpbb_request_interface::POST)) ? false : $cache->get('_update_list');
-				$modified = ($update_list !== false) ? @filemtime($cache->cache_dir . 'data_update_list.' . $phpEx) : 0;
+				$modified = ($update_list !== false) ? @filemtime($cache->get_driver()->cache_dir . 'data_update_list.' . $phpEx) : 0;
 
 				// Make sure the list is up-to-date
 				if ($update_list !== false)
diff --git a/tests/cache/cache_test.php b/tests/cache/cache_test.php
index 564bd35863..c5f5fac88c 100644
--- a/tests/cache/cache_test.php
+++ b/tests/cache/cache_test.php
@@ -9,7 +9,7 @@
 
 require_once dirname(__FILE__) . '/../../phpBB/includes/functions.php';
 
-class phpbb_cache_test extends phpbb_test_case
+class phpbb_cache_test extends phpbb_database_test_case
 {
 	private $cache_dir;
 
@@ -18,8 +18,15 @@ class phpbb_cache_test extends phpbb_test_case
 		$this->cache_dir = dirname(__FILE__) . '/../tmp/cache/';
 	}
 
+	public function getDataSet()
+	{
+		return $this->createXMLDataSet(dirname(__FILE__) . '/fixtures/config.xml');
+	}
+
 	protected function setUp()
 	{
+		parent::setUp();
+
 		if (file_exists($this->cache_dir))
 		{
 			// cache directory possibly left after aborted
@@ -35,6 +42,8 @@ class phpbb_cache_test extends phpbb_test_case
 		{
 			$this->remove_cache_dir();
 		}
+
+		parent::tearDown();
 	}
 
 	private function create_cache_dir()
@@ -67,4 +76,34 @@ class phpbb_cache_test extends phpbb_test_case
 			'File ACM put and get'
 		);
 	}
+
+	public function test_cache_sql()
+	{
+		$driver = new phpbb_cache_driver_file($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);
+
+		$this->assertFileExists($this->cache_dir . 'sql_' . md5(preg_replace('/[\n\r\s\t]+/', ' ', $sql)) . '.php');
+
+		$sql = "SELECT * FROM phpbb_config
+			WHERE config_name = 'foo'";
+		$result = $db->sql_query($sql, 300);
+
+		$this->assertEquals($first_result, $db->sql_fetchrow($result));
+
+		$sql = "SELECT * FROM phpbb_config
+			WHERE config_name = 'bar'";
+		$result = $db->sql_query($sql, 300);
+
+		$this->assertNotEquals($first_result, $db->sql_fetchrow($result));
+
+		$db->sql_close();
+	}
 }
diff --git a/tests/cache/fixtures/config.xml b/tests/cache/fixtures/config.xml
new file mode 100644
index 0000000000..9d395b685c
--- /dev/null
+++ b/tests/cache/fixtures/config.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<dataset>
+	<table name="phpbb_config">
+		<column>config_name</column>
+		<column>config_value</column>
+		<column>is_dynamic</column>
+		<row>
+			<value>foo</value>
+			<value>23</value>
+			<value>0</value>
+		</row>
+		<row>
+			<value>bar</value>
+			<value>42</value>
+			<value>1</value>
+		</row>
+	</table>
+</dataset>
diff --git a/tests/mock/cache.php b/tests/mock/cache.php
index c6d08afef0..b64c92ea89 100644
--- a/tests/mock/cache.php
+++ b/tests/mock/cache.php
@@ -121,7 +121,7 @@ class phpbb_mock_cache implements phpbb_cache_driver_interface
 	public function sql_load($query)
 	{
 	}
-	public function sql_save($query, &$query_result, $ttl)
+	public function sql_save($query, $query_result, $ttl)
 	{
 	}
 	public function sql_exists($query_id)