Problem with caching SQL results
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8212
- Loc: USA
- Status: Offline
Ok, I have the following function:
You see where it checks if it is cached? At the beginning to see if it needs to return near the very end where it checks if it was needed to be cached...
Anyway, the function that actually caches is:
The problem I'm having is when I try to run the SQL and be caching them, it caches them as integer. I do var_dump($results) and it prints int(1).
I check the cache files and they say i:1;
Any help?
PHP Code: [ Select ]
public function fetch_rowset($sql, $type = 'assoc')
{
// Checking if the SQL query was cached
if($this->cache_results === true)
{
// The name of the cached file
$sql_file = $this->cache_dir . md5($sql) . '.cache';
// Checking if the SQL file exists
if(file_exists($sql_file))
{
return unserialize(file_get_contents($sql_file));
}
}
// Creating a result resource
$results = $this->set_result_resource($sql, true);
// Initiating the result array
$result = array();
// Checking what type of result we want
if($type == 'assoc')
{
while($row = mysql_fetch_assoc($results))
{
$result[] = $row;
}
}
elseif($type == 'array')
{
while($row = mysql_fetch_array($results))
{
$result[] = $row;
}
}
elseif($type == 'object')
{
while($row = mysql_fetch_object($results))
{
$result[] = $row;
}
}
elseif($type == 'row')
{
while($row = mysql_fetch_row($results))
{
$result[] = $row;
}
}
else
{
while($row = mysql_fetch_assoc($results))
{
$result[] = $row;
}
}
// Checking if we need to free the result resource
if($this->free_result == true)
{
$this->free_result();
}
// Checking if we need to cache the result
if($this->cache_results === true)
{
return $this->cache_results($sql, $result);
}
// Returning the result (if not cached)
return ($type == 'object') ? (object) $result : $result;
}
{
// Checking if the SQL query was cached
if($this->cache_results === true)
{
// The name of the cached file
$sql_file = $this->cache_dir . md5($sql) . '.cache';
// Checking if the SQL file exists
if(file_exists($sql_file))
{
return unserialize(file_get_contents($sql_file));
}
}
// Creating a result resource
$results = $this->set_result_resource($sql, true);
// Initiating the result array
$result = array();
// Checking what type of result we want
if($type == 'assoc')
{
while($row = mysql_fetch_assoc($results))
{
$result[] = $row;
}
}
elseif($type == 'array')
{
while($row = mysql_fetch_array($results))
{
$result[] = $row;
}
}
elseif($type == 'object')
{
while($row = mysql_fetch_object($results))
{
$result[] = $row;
}
}
elseif($type == 'row')
{
while($row = mysql_fetch_row($results))
{
$result[] = $row;
}
}
else
{
while($row = mysql_fetch_assoc($results))
{
$result[] = $row;
}
}
// Checking if we need to free the result resource
if($this->free_result == true)
{
$this->free_result();
}
// Checking if we need to cache the result
if($this->cache_results === true)
{
return $this->cache_results($sql, $result);
}
// Returning the result (if not cached)
return ($type == 'object') ? (object) $result : $result;
}
- public function fetch_rowset($sql, $type = 'assoc')
- {
- // Checking if the SQL query was cached
- if($this->cache_results === true)
- {
- // The name of the cached file
- $sql_file = $this->cache_dir . md5($sql) . '.cache';
- // Checking if the SQL file exists
- if(file_exists($sql_file))
- {
- return unserialize(file_get_contents($sql_file));
- }
- }
- // Creating a result resource
- $results = $this->set_result_resource($sql, true);
- // Initiating the result array
- $result = array();
- // Checking what type of result we want
- if($type == 'assoc')
- {
- while($row = mysql_fetch_assoc($results))
- {
- $result[] = $row;
- }
- }
- elseif($type == 'array')
- {
- while($row = mysql_fetch_array($results))
- {
- $result[] = $row;
- }
- }
- elseif($type == 'object')
- {
- while($row = mysql_fetch_object($results))
- {
- $result[] = $row;
- }
- }
- elseif($type == 'row')
- {
- while($row = mysql_fetch_row($results))
- {
- $result[] = $row;
- }
- }
- else
- {
- while($row = mysql_fetch_assoc($results))
- {
- $result[] = $row;
- }
- }
- // Checking if we need to free the result resource
- if($this->free_result == true)
- {
- $this->free_result();
- }
- // Checking if we need to cache the result
- if($this->cache_results === true)
- {
- return $this->cache_results($sql, $result);
- }
- // Returning the result (if not cached)
- return ($type == 'object') ? (object) $result : $result;
- }
You see where it checks if it is cached? At the beginning to see if it needs to return near the very end where it checks if it was needed to be cached...
Anyway, the function that actually caches is:
PHP Code: [ Select ]
private function cache_results($sql, $results)
{
// Generating the file name for the cache
$sql_file = $this->cache_dir . md5($sql) . '.cache';
// Creating the cache file
if(touch($sql_file))
{
file_put_contents($sql_file, serialize($results));
}
return $results;
}
{
// Generating the file name for the cache
$sql_file = $this->cache_dir . md5($sql) . '.cache';
// Creating the cache file
if(touch($sql_file))
{
file_put_contents($sql_file, serialize($results));
}
return $results;
}
- private function cache_results($sql, $results)
- {
- // Generating the file name for the cache
- $sql_file = $this->cache_dir . md5($sql) . '.cache';
- // Creating the cache file
- if(touch($sql_file))
- {
- file_put_contents($sql_file, serialize($results));
- }
- return $results;
- }
The problem I'm having is when I try to run the SQL and be caching them, it caches them as integer. I do var_dump($results) and it prints int(1).
I check the cache files and they say i:1;
Any help?
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
November 5th, 2010, 10:10 pm
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8212
- Loc: USA
- Status: Offline
I still haven't being able find a solution to this problem yet. Is there anyone here who is able to help me solve this issue.
The function has now changed to the following:
The function has now changed to the following:
PHP Code: [ Select ]
public function fetch_rowset($sql, $type = 'assoc')
{
// Checking if the SQL query was cached
if($this->cache_results === true)
{
// The name of the cached file
$sql_file = $this->cache_dir . md5($sql) . '.cache';
// Checking if the SQL file exists
if(file_exists($sql_file))
{
return unserialize(file_get_contents($sql_file));
}
}
// Creating a result resource
$results = $this->set_result_resource($sql, true);
// Checking what type of result we want
if($type == 'assoc')
{
while($row = mysql_fetch_assoc($results))
{
$result = $row;
}
}
elseif($type == 'array')
{
while($row = mysql_fetch_array($results))
{
$result = $row;
}
}
elseif($type == 'object')
{
while($row = mysql_fetch_object($results))
{
$result = $row;
}
}
elseif($type == 'row')
{
while($row = mysql_fetch_row($results))
{
$result = $row;
}
}
else
{
while($row = mysql_fetch_assoc($results))
{
$result = $row;
}
}
// Checking if we need to free the result resource
if($this->free_result == true)
{
$this->free_result();
}
// Checking if we need to cache the result
if($this->cache_results === true)
{
$this->cache_result($sql, $result);
}
// Returning the result (if not cached)
return ($type == 'object') ? (object) $result : $result;
}
{
// Checking if the SQL query was cached
if($this->cache_results === true)
{
// The name of the cached file
$sql_file = $this->cache_dir . md5($sql) . '.cache';
// Checking if the SQL file exists
if(file_exists($sql_file))
{
return unserialize(file_get_contents($sql_file));
}
}
// Creating a result resource
$results = $this->set_result_resource($sql, true);
// Checking what type of result we want
if($type == 'assoc')
{
while($row = mysql_fetch_assoc($results))
{
$result = $row;
}
}
elseif($type == 'array')
{
while($row = mysql_fetch_array($results))
{
$result = $row;
}
}
elseif($type == 'object')
{
while($row = mysql_fetch_object($results))
{
$result = $row;
}
}
elseif($type == 'row')
{
while($row = mysql_fetch_row($results))
{
$result = $row;
}
}
else
{
while($row = mysql_fetch_assoc($results))
{
$result = $row;
}
}
// Checking if we need to free the result resource
if($this->free_result == true)
{
$this->free_result();
}
// Checking if we need to cache the result
if($this->cache_results === true)
{
$this->cache_result($sql, $result);
}
// Returning the result (if not cached)
return ($type == 'object') ? (object) $result : $result;
}
- public function fetch_rowset($sql, $type = 'assoc')
- {
- // Checking if the SQL query was cached
- if($this->cache_results === true)
- {
- // The name of the cached file
- $sql_file = $this->cache_dir . md5($sql) . '.cache';
- // Checking if the SQL file exists
- if(file_exists($sql_file))
- {
- return unserialize(file_get_contents($sql_file));
- }
- }
- // Creating a result resource
- $results = $this->set_result_resource($sql, true);
- // Checking what type of result we want
- if($type == 'assoc')
- {
- while($row = mysql_fetch_assoc($results))
- {
- $result = $row;
- }
- }
- elseif($type == 'array')
- {
- while($row = mysql_fetch_array($results))
- {
- $result = $row;
- }
- }
- elseif($type == 'object')
- {
- while($row = mysql_fetch_object($results))
- {
- $result = $row;
- }
- }
- elseif($type == 'row')
- {
- while($row = mysql_fetch_row($results))
- {
- $result = $row;
- }
- }
- else
- {
- while($row = mysql_fetch_assoc($results))
- {
- $result = $row;
- }
- }
- // Checking if we need to free the result resource
- if($this->free_result == true)
- {
- $this->free_result();
- }
- // Checking if we need to cache the result
- if($this->cache_results === true)
- {
- $this->cache_result($sql, $result);
- }
- // Returning the result (if not cached)
- return ($type == 'object') ? (object) $result : $result;
- }
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8212
- Loc: USA
- Status: Offline
- joebert
- Sledgehammer


- Joined: Feb 10, 2004
- Posts: 13458
- Loc: Florida
- Status: Offline
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8212
- Loc: USA
- Status: Offline
joebert wrote:
Glad you worked it out. I've looked at this thread a couple of times wondering why the MySQL query cache wasn't cache enough. 
The problem was with how I was implementing the cache.
I was using my _get_() function which calls mysql_num_rows(), which then, it caches it. Then it calls on mysql_fetch_row and caches it as well... the problem with that is that the SQL was the same for both queries.
Well, that wasn't THE problem, but they both tried to write the results to the same cache file.
With Ubuntu, as soon as it creates the written file, they are automatically chmodded as read-only, so mysql_fetch_row was returning int(1) since that's what is in the cache after mysql_num_rows.
I added _num to the SQL that mysql_num_row caches as file name md5($sql . '_num')
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 5 posts
- Users browsing this forum: No registered users and 154 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum
