Skip to content

Instantly share code, notes, and snippets.

@TrafeX
Created December 29, 2010 08:57
Show Gist options
  • Save TrafeX/758347 to your computer and use it in GitHub Desktop.
Save TrafeX/758347 to your computer and use it in GitHub Desktop.
An example how to use memcacheq with Zend_Queue
<?php
// MemcacheQ config
$queueOptions = array(
'name' => 'example-queue',
'driverOptions' => array(
'host' => '127.0.0.1',
'port' => 22201
)
);
// Instantiate Zend_Queue
$queue = new Zend_Queue('MemcacheQ', $queueOptions);
// Find out if the queue exists
if (!$queue->getAdapter()->isExists($queueOptions['name']))
{
// If not, create it
$queue->createQueue($queueOptions['name']);
}
// Build a query string (key=val&key=val) because we need a scalar value
$params = http_build_query(
array(
'timestamp' => $timestamp,
'page' => $page
)
);
// Send the data to the queue
$queue->send($params);
<?php
// MemcacheQ config
$queueOptions = array(
'name' => 'example-queue',
'driverOptions' => array(
'host' => '127.0.0.1',
'port' => 22201
)
);
// Instantiate Zend_Queue
$queue = new Zend_Queue('MemcacheQ', $queueOptions);
// Retrieve 5 items from the queue
$messages = $queue->receive(5);
// $message is now a instance of Zend_Queue_Message_Iterator
// @TODO: Use a nice FilterIterator ;)
foreach ($messages as $job)
{
if ('creating queue' == $job->body || false === $job->body)
{
// Skip message
continue;
}
// Parse the query string to a array
parse_str($job->body, $data);
// Execute the heavy process
$this->registerHit($data['timestamp'], $data['page']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment