0

So I've just started to learn about how to use memcache yesterday. So there's still a lot to do. So far I've got down how to install it, and saw a couple tutorials on how to use it. But they all explain how to type it out statically. I do all my db queries in classes and run through the data returned with a foreach loop. I got it working without any compile errors, but the memcache won't actually work. I'm confused as to why. My class is set up this way.

class Feed extends Database
{
private $Memcache;

public function __construct()
{
    parent::__construct();
    $this->Memcache = new Memcache;
} //end __construct

public function getFeed ($var)
{
    $feed = array(); //Initialize an empty array

    $this->Memcache->connect('localhost', 11211);
    $key = "SELECT col FROM table WHERE value = " . $var . " ORDER BY timestamp DESC LIMIT 30";
    $cache = $this->Memcache->get($key);
    if ($cache)
    {
        $feed = '';
    }
    else
    {
        //Query the database
        $Statement = $this->Database->prepare("SELECT col FROM table WHERE value = ? ORDER BY timestamp DESC LIMIT 30");
        $Statement->execute(array($var));


        while($row = $Statement->fetch(PDO::FETCH_ASSOC))
        {
            $feed[] = array("row1" => $row["col1"], 
                            "row2" => $row["col2"] );

        }

        $this->Memcache->set($key, $row, 0, 40); // Store the result of the query for 40 seconds
    }

    return $feed;
} //end getFeed
} //end Feed

The output is set up like so

<?php foreach ($Feed->getFeed($var) as $feed): ?>

    <p><?php echo $feed["row1"]; ?></p>

<?php endforeach; ?>

So obviously if the memcache was working their would be a compile error since I run through the $feed with a foreach loop. Since I'm limited in my knowledge of memcache do any of you know why it isn't retrieve the data.

Thank you do much!

4
  • what errors to do you get? Commented Mar 11, 2013 at 20:15
  • So obviously if the memcache was working their would be a compile error... if this is influenced at run-time rather than loading, it's a run-time error, not a compile error. Knowing your output would help us. Commented Mar 11, 2013 at 20:16
  • There are no errors, because it's querying the database at this point. So it goes to the else part of that conditional statement. But I'm looking to use memcache and run through the if part of the statement. If the if tested true the code would throw a compile error saying invalid argument was supplied for the foreach. but it wdoesn't so the memcache isn't working. Commented Mar 11, 2013 at 20:21
  • While Matthijs van den Bos has supplied what looks the right answer (it's always a good idea to RTFM) next time you might consider building a test rig to see if your assumptions about the system are correct - in this case simply set an item and try to retrieve it before ans after expiry. BTW you can list at least some of the data in a memcache store using the 'stats items' command. Commented Mar 12, 2013 at 12:36

1 Answer 1

1

From the Memcache protocol description:

Keys

Data stored by memcached is identified with the help of a key. A key is a text string which should uniquely identify the data for clients that are interested in storing and retrieving it. Currently the length limit of a key is set at 250 characters (of course, normally clients wouldn't need to use such long keys); the key must not include control characters or whitespace.

Your key contains whitespace characters so it fails. You can check this because Memcache::set() returns FALSE on failure.

An easy way to prevent this: create an MD5 hash from your $key and use that as $key:

$key = md5("SELECT col FROM table WHERE value = " . $var . " ORDER BY timestamp DESC LIMIT 30");

Note: The Memcache value can't be larger that 1MB.

Sign up to request clarification or add additional context in comments.

2 Comments

I've never used memcached. If you hash your keys, do you actually have to worry about collisions? Or is that unreasonable?
Unless you're working for NASA, you don't have to worry about it, because it is extemely unlikely. Check this answer about that: stackoverflow.com/a/2444336/844313

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.