1

I am adding memcache into my site and the problem I am having is that I am caching the object so its easy to use inside other objects or pages. The problem is that I am caching the object when I load it, so if I made a change to a variable in the object, its not passed onto the other pages. Am I missing something or do I really have to save the cached object after all processing?

// ON THE PAGE I FIRST CALL
$objState = $gb->objCache->State('1');
echo "Name:" . $objState->get_Name() . "<br>";
$objState->set_Name('New Name');

// ON ANOTHER PAGE I CALL IT AGAIN
$objState = $gb->objCache->State('1');
echo "Name:" . $objState->get_Name() . "<br>";
$objState->set_Name('New Name 2');

// THE FUNCTION THAT I CALL TO GET THE STATE CLASS/OBJECT
function State($id, $reload=false) {
  $objState = null;

  $cached = false;
  if (is_object($this->gb->memcache))
    $cached = $this->gb->memcache->get('objState_' . $id . '_' .  getCompanyUniqueID());
  if ($cached !== FALSE && is_object($cached) && !$reload) {
    $objState = $cached;
  } else    {               
        $objState = new State($id);

      // Store generated XML for 5 minutes (300 seconds)
    if (is_object($this->gb->memcache))
        $this->gb->memcache->set('objState_' . $id . '_' .  getCompanyUniqueID(), $objState, 0, 300); // Don't worry about third argument, it controls compression
    }

  return $objState;
}
1
  • You have to write any changes to memcache if you want it to persist on memcache. Commented Feb 22, 2012 at 7:14

1 Answer 1

3

Memcached stores the data as a serialized entity so any changes will need to be committed. You could try writing an object to commit your data when the script finishes executing with the PHP register_shutdown function.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.