Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-28-2008, 04:40 PM
Junior Member
 
Join Date: Mar 2008
Posts: 2
Unhappy Implementation of Zend_Db_Adapter_Mysqli to use Zend_Cache

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 10-31-2008, 10:25 PM
Junior Member
 
Join Date: Oct 2008
Posts: 1
Default

Months later, I wanted to add to this (moreso for newbies like me than anyone else). I wanted to start using Zend_Cache and kept getting the same errors as fiskah had. This was the only search result that even discussed it.

I believe the issue is that we were attempting to cache a Zend_Db_Statement_Mysqli object, which was the returned result from (something similary to) $db->query($query);.

Unfortunately, when grabbed from the cache, you no longer have access to its methods, such as fetch(), so you get an error.

For me, the solution is to use fetchAll() instead of query(). fetchAll() returns a simple array, which can be accessed from the cache using a foreach loop.

If I stated anything wrong, please correct me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 03:13 AM.