Skip to content

Commit

Permalink
Merge pull request #58 from johannesx75/patch-1
Browse files Browse the repository at this point in the history
Speed up que object selection
  • Loading branch information
dhensby committed Dec 19, 2016
2 parents afe00ff + 5d52526 commit adb3f6f
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions code/model/StaticPagesQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,38 +166,35 @@ protected static function get_queue_object($freshness, $interval = null, $sortOr
$filterQuery = array("Freshness" => $freshness);
if ($interval) $filterQuery["LastEdited:LessThan"] = $interval;

$query = self::get();
if ($query->Count() > 0) {
$offset = 0;
$filteredQuery = $query->filter($filterQuery)->sort($sortOrder);
$offset = 0;
$filteredQuery = self::get()->filter($filterQuery)->sort($sortOrder);

if ($filteredQuery->Count() > 0) {
if (!self::config()->disable_mysql_locks && DB::getConn() instanceof MySQLDatabase) { //locking currently only works on MySQL
$queueObject = $filteredQuery->first();
if ($queueObject) {
if (!self::config()->disable_mysql_locks && DB::getConn() instanceof MySQLDatabase) { //locking currently only works on MySQL

do {
$queueObject = $filteredQuery->limit(1, $offset)->first(); //get first item
do {
$queueObject = $filteredQuery->limit(1, $offset)->first(); //get first item

if ($queueObject) $lockName = md5($queueObject->URLSegment . $className);
//try to locking the item's URL, keep trying new URLs until we find one that is free to lock
$offset++;
} while($queueObject && !LockMySQL::isFreeToLock($lockName));

if ($queueObject) {
$lockSuccess = LockMySQL::getLock($lockName); //acquire a lock with the URL of the queue item we have just fetched
if ($lockSuccess) {
self::remove_duplicates($queueObject->ID); //remove any duplicates
self::mark_as_regenerating($queueObject); //mark as regenerating so nothing else grabs it
LockMySQL::releaseLock($lockName); //return the object and release the lock
}
}
} else {
$queueObject = $filteredQuery->first();
self::remove_duplicates($queueObject->ID);
self::mark_as_regenerating($queueObject);
}
}
}
if ($queueObject) $lockName = md5($queueObject->URLSegment . $className);
//try to locking the item's URL, keep trying new URLs until we find one that is free to lock
$offset++;
} while($queueObject && !LockMySQL::isFreeToLock($lockName));

if ($queueObject) {
$lockSuccess = LockMySQL::getLock($lockName); //acquire a lock with the URL of the queue item we have just fetched
if ($lockSuccess) {
self::remove_duplicates($queueObject->ID); //remove any duplicates
self::mark_as_regenerating($queueObject); //mark as regenerating so nothing else grabs it
LockMySQL::releaseLock($lockName); //return the object and release the lock
}
}
} else {
self::remove_duplicates($queueObject->ID);
self::mark_as_regenerating($queueObject);
}
}

return $queueObject; //return the object or null
}

Expand Down

0 comments on commit adb3f6f

Please sign in to comment.