I've tried to make an implementation of Zend_Db_Adapter_Mysqli that adds a two parameters:
bool cache, and
int ttl. To specify whether to use caching, and if so how long the cache has to live.
The reason for this implementation is that I want to make it easier to cache database queries.
Code:
PHP Code:
<?php
class Site_Core_Database extends Zend_Db_Adapter_Mysqli
{
function query($sql, $bind = array(), $cache = false, $cache_ttl = 3600)
{
// Do we want to use caching?
if($cache !== true)
{
// No - perform the query
return parent::query($sql, ($bind == null ? array() : $bind));
} else {
// Valid time to live (ttl)?
if(!is_numeric($cache_ttl))
{
$cache_ttl = 3600;
}
// The cache object
$cache = Zend_Registry::get('cache');
// Generate cache key
$key = md5($sql . serialize($bind));
// Can we load the existing result?
if(!($result = $cache->load($key)))
{
// If not generate a result
$db = Zend_Registry::get('db');
// Bind parameters
if($bind != null)
{
$result = $db->query($sql, $bind);
} else {
$result = $db->query($sql);
}
$cache->save($result, $key, array('db'), $cache_ttl);
}
return $result;
}
}
}
?>
It seems though that there are trouble caching/serializing the result sets, because cache fetching fails.
Saving to the cache works fine, but when I reload the page I get this error:
Quote:
Warning: mysqli_stmt::fetch() [function.mysqli-stmt-fetch]: Couldn't fetch mysqli_stmt in [...]/lib/Zend/Db/Statement/Mysqli.php on line 271
Warning: mysqli_stmt::reset() [function.mysqli-stmt-reset]: Couldn't fetch mysqli_stmt in [...]/lib/Zend/Db/Statement/Mysqli.php on line 275
|
Anyone got a clue what I can do to implement a caching solution this way?
Thanks in advance.