PhpRedis.php in Redis 7.2
File
lib/Redis/Queue/PhpRedis.php
View source
<?php
class Redis_Queue_PhpRedis extends Redis_Queue_Base {
public function createItem($data) {
$client = $this
->getClient();
$dKey = $this
->getKeyForData();
$qKey = $this
->getKeyForQueue();
$id = $client
->hincrby($dKey, self::QUEUE_HKEY_SEQ, 1);
$record = new stdClass();
$record->qid = $id;
$record->data = $data;
$record->timestamp = time();
$pipe = $client
->multi(Redis::PIPELINE);
$pipe
->hsetnx($dKey, $id, serialize($record));
$pipe
->llen($qKey);
$pipe
->lpush($qKey, $id);
$ret = $pipe
->exec();
if (!($success = $ret[0] && $ret[1] < $ret[2])) {
if ($ret[0]) {
$client
->hdel($dKey, $id);
}
}
return $success;
}
public function numberOfItems() {
return $this
->getClient()
->llen($this
->getKeyForQueue());
}
public function claimItem($lease_time = 30) {
$client = $this
->getClient();
$id = $client
->rpoplpush($this
->getKeyForQueue(), $this
->getKeyForClaimed());
if ($id) {
if ($item = $client
->hget($this
->getKeyForData(), $id)) {
if ($item = unserialize($item)) {
return $item;
}
}
}
return false;
}
public function deleteItem($item) {
$pipe = $this
->getClient()
->multi(Redis::PIPELINE);
$pipe
->lrem($this
->getKeyForQueue(), $item->qid);
$pipe
->lrem($this
->getKeyForClaimed(), $item->qid);
$pipe
->hdel($this
->getKeyForData(), $item->qid);
$pipe
->exec();
}
public function releaseItem($item) {
$pipe = $this
->getClient()
->multi(Redis::PIPELINE);
$pipe
->lrem($this
->getKeyForClaimed(), $item->qid, -1);
$pipe
->lpush($this
->getKeyForQueue(), $item->qid);
$ret = $pipe
->exec();
return $ret[0] && $ret[1];
}
public function createQueue() {
}
public function deleteQueue() {
$this
->getClient()
->del($this
->getKeyForQueue(), $this
->getKeyForClaimed(), $this
->getKeyForData());
}
}
Classes
Name |
Description |
Redis_Queue_PhpRedis |
@todo
Set high expire value to the hash for rotation when memory is empty
React upon cache clear all and rebuild path list? |