Welcome Guest, Not a member yet? Register   Sign In
Memcached Issue
#1

[eluser]Unknown[/eluser]
Hi,

I'm currently in the process of implementing memcached support into a Codeigniter project, and I am running into problems when caching query results that contain more than one row of data. Queries results that only contain one row of data are cached as exepected. Those that contain more than one row of data are caching empty results.

Below is the index function on the controller that allows a user to post.

Code:
function index()
{
        $username = $this->check_creds();
        
        if($username != false)
        {
            
            $memcache = new Memcache;
            $memcache->connect('127.0.0.1', 11211) or die (redirect('error', 'location'));
            
            $data['username'] = $username;
            
            $groupsKey = $username . '_groups';
            
            $data['groups'] = $memcache->get($groupsKey);
            $groups = $memcache->get($groupsKey);
            if(!$groups)
            {
                $groups = $this->friendsmodel->myGroups($username);
                $memcache->set($groupsKey, $groups, false, 3600) or die (redirect('error', 'location'));
            }
            $data['groups'] = $groups;
            
            $settingsKey = $username . '_settings';
            $settings = $memcache->get($settingsKey);
            if(!$settings)
            {
                $settings = $this->membertoolsmodel->dashboardGetSettings($username);
                $memcache->set($settingsKey, $settings, false, 3600) or die (redirect('error', 'location'));
            }
            $data['settings'] = $settings;
            $data['private'] = $settings->private_posts;
            if(isset($_SERVER['HTTP_REFERER']))
            {
                $data['referer'] = $_SERVER['HTTP_REFERER'];
            }
            
            $memcache->close();
            $this->load->view('write', $data);
        }
        else redirect('login', 'location');
}

This controller is relatively simple. If the user is logged in it starts up a new memcached instance, gets the users groups and settings via either the cache or through a query and then loads the required view. If the user is not logged in they are redirected to the login page.

The first set of data being gathered - the groups - is used to populate a drop down list in the view. This data populates fine when the cache is empty. If the cache has been set, the data is not populated in the dropdown. The data set returned contains more than one row of data. It's model function looks like this:

Code:
function myGroups($username)
    {
        $sql = "SELECT id, group_name FROM groups WHERE username = ? and status = 'Keep'";
        $query = $this->db->query($sql, array($username));
        if($query->num_rows() > 0)
        {
            return $query;
        }
        else return FALSE;
    }

The section of the view that populates the drop down is:

Code:
<?php
        echo '<select class = "sendselect" name="groups">';
        echo '<option value = "all">To Everyone</option>';
        echo '<option value = "me">To Me</option>';
        if($groups!= FALSE)
        {

            foreach($groups->result() as $group)
            {
    
                echo "<option value = \"$group->id\">To $group->group_name</option>";
    
            }
        }
        echo '</select>';?&gt;

The var_dump of the query is:

Code:
object(CI_DB_mysql_result)#19 (7) { ["conn_id"]=>  resource(30) of type (mysql link persistent) ["result_id"]=>  resource(45) of type (mysql result) ["result_array"]=>  array(0) { } ["result_object"]=>  array(0) { } ["current_row"]=>  int(0) ["num_rows"]=>  int(4) ["row_data"]=>  NULL }


The var_dump of the cache is:

Code:
object(CI_DB_mysql_result)#20 (7) { ["conn_id"]=>  int(0) ["result_id"]=>  int(0) ["result_array"]=>  array(0) { } ["result_object"]=>  array(0) { } ["current_row"]=>  int(0) ["num_rows"]=>  int(4) ["row_data"]=>  NULL }

The second set of data - settings - only contains one row of data, and is cached correctly. It's function looks like this:

Code:
function dashboardGetSettings($username)
    {
        $sql = "SELECT private_posts, notify_rss, rss_secret_word FROM settings WHERE username = ? LIMIT 1";
        $result = $this->db->query($sql, array($username));
        $row = $result->row();
        return $row;
    }


Anyone have an idea why the first result set - groups - is not being cached properly? I notice that both the query dump and the cache dump both show that the row data is NULL. I assume that this is the problem, but am unsure how to correct it.


Messages In This Thread
Memcached Issue - by El Forum - 08-31-2008, 08:42 PM
Memcached Issue - by El Forum - 08-31-2008, 09:01 PM
Memcached Issue - by El Forum - 08-28-2009, 03:27 AM



Theme © iAndrew 2016 - Forum software by © MyBB