use TraitCompareDelete;
/**
- * @var Memcached
+ * @var \Memcached
*/
private $memcached;
class RedisCacheDriver extends BaseObject implements IMemoryCacheDriver
{
/**
- * @var Redis
+ * @var \Redis
*/
private $redis;
<?php
-/**
- * @file src/Util/Lock.php
- */
+
namespace Friendica\Core;
/**
*
* @return boolean Was the lock successful?
*/
- public static function acquireLock($key, $timeout = 120)
+ public static function acquire($key, $timeout = 120)
{
- return self::getDriver()->acquire($key, $timeout);
+ return self::getDriver()->acquireLock($key, $timeout);
}
/**
* @brief Releases a lock if it was set by us
*
* @param string $key Name of the lock
- * @return mixed
+ * @return void
*/
- public static function releaseLock($key)
+ public static function release($key)
{
- return self::getDriver()->release($key);
+ self::getDriver()->releaseLock($key);
}
/**
*/
public function releaseAll() {
foreach ($this->acquiredLocks as $acquiredLock => $hasLock) {
- $this->release($acquiredLock);
+ $this->releaseLock($acquiredLock);
}
}
}
/**
* (@inheritdoc)
*/
- public function acquire($key, $timeout = 120)
+ public function acquireLock($key, $timeout = 120)
{
$got_lock = false;
$start = time();
/**
* (@inheritdoc)
*/
- public function release($key)
+ public function releaseLock($key)
{
$cachekey = self::getCacheKey($key);
/**
* (@inheritdoc)
*/
- public function acquire($key, $timeout = 120)
+ public function acquireLock($key, $timeout = 120)
{
$got_lock = false;
$start = time();
/**
* (@inheritdoc)
*/
- public function release($key)
+ public function releaseLock($key)
{
dba::delete('locks', ['name' => $key, 'pid' => getmypid()]);
*
* @return boolean Was the lock successful?
*/
- public function acquire($key, $timeout = 120);
+ public function acquireLock($key, $timeout = 120);
/**
* Releases a lock if it was set by us
*
* @return void
*/
- public function release($key);
+ public function releaseLock($key);
/**
* Releases all lock that were set by us
{
$temp = get_temppath();
- $file = $temp.'/'.$key.'.sem';
+ $file = $temp . '/' . $key . '.sem';
if (!file_exists($file)) {
file_put_contents($file, $key);
*
* (@inheritdoc)
*/
- public function acquire($key, $timeout = 120)
+ public function acquireLock($key, $timeout = 120)
{
self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
if (self::$semaphore[$key]) {
/**
* (@inheritdoc)
*/
- public function release($key)
+ public function releaseLock($key)
{
if (empty(self::$semaphore[$key])) {
return false;
*/
public function isLocked($key)
{
- return @sem_get(self::$semaphore[$key]) !== false;
+ return isset(self::$semaphore[$key]);
}
}
*/
namespace Friendica\Core;
-use Friendica\Core\Addon;
-use Friendica\Core\Config;
-use Friendica\Core\System;
-use Friendica\Core\Lock;
+use dba;
use Friendica\Database\DBM;
use Friendica\Model\Process;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Network;
-use dba;
require_once 'include/dba.php';
}
// If possible we will fetch new jobs for this worker
- if (!$refetched && Lock::acquireLock('worker_process', 0)) {
+ if (!$refetched && Lock::acquire('worker_process', 0)) {
$stamp = (float)microtime(true);
$refetched = self::findWorkerProcesses($passing_slow);
self::$db_duration += (microtime(true) - $stamp);
- Lock::releaseLock('worker_process');
+ Lock::release('worker_process');
}
}
// To avoid the quitting of multiple workers only one worker at a time will execute the check
- if (Lock::acquireLock('worker', 0)) {
+ if (Lock::acquire('worker', 0)) {
$stamp = (float)microtime(true);
// Count active workers and compare them with a maximum value that depends on the load
if (self::tooMuchWorkers()) {
logger('Memory limit reached, quitting.', LOGGER_DEBUG);
return;
}
- Lock::releaseLock('worker');
+ Lock::release('worker');
self::$db_duration += (microtime(true) - $stamp);
}
dba::close($r);
$stamp = (float)microtime(true);
- if (!Lock::acquireLock('worker_process')) {
+ if (!Lock::acquire('worker_process')) {
return false;
}
self::$lock_duration = (microtime(true) - $stamp);
$found = self::findWorkerProcesses($passing_slow);
self::$db_duration += (microtime(true) - $stamp);
- Lock::releaseLock('worker_process');
+ Lock::release('worker_process');
if ($found) {
$r = dba::select('workerqueue', [], ['pid' => getmypid(), 'done' => false]);
}
// If there is a lock then we don't have to check for too much worker
- if (!Lock::acquireLock('worker', 0)) {
+ if (!Lock::acquire('worker', 0)) {
return true;
}
// If there are already enough workers running, don't fork another one
$quit = self::tooMuchWorkers();
- Lock::releaseLock('worker');
+ Lock::release('worker');
if ($quit) {
return true;
namespace Friendica\Model;
+use dba;
use Friendica\BaseObject;
use Friendica\Content\Text;
use Friendica\Core\Addon;
use Friendica\Core\Config;
use Friendica\Core\L10n;
+use Friendica\Core\Lock;
use Friendica\Core\PConfig;
use Friendica\Core\System;
use Friendica\Core\Worker;
use Friendica\Database\DBM;
-use Friendica\Model\Contact;
-use Friendica\Model\Conversation;
-use Friendica\Model\Group;
-use Friendica\Model\Term;
use Friendica\Object\Image;
use Friendica\Protocol\Diaspora;
use Friendica\Protocol\OStatus;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\XML;
-use Friendica\Util\Lock;
-use dba;
use Text_LanguageDetect;
require_once 'boot.php';
}
// To avoid timing problems, we are using locks.
- $locked = Lock::set('item_insert_content');
+ $locked = Lock::acquire('item_insert_content');
if (!$locked) {
logger("Couldn't acquire lock for URI " . $item['uri'] . " - proceeding anyway.");
}
logger('Could not insert content for URI ' . $item['uri'] . ' - trying asynchronously');
}
if ($locked) {
- Lock::remove('item_insert_content');
+ Lock::release('item_insert_content');
}
}
*/
namespace Friendica\Protocol;
+use dba;
+use DOMDocument;
+use DOMXPath;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\HTML;
use Friendica\Core\Cache;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Network;
use Friendica\Util\XML;
-use dba;
-use DOMDocument;
-use DOMXPath;
require_once 'include/dba.php';
require_once 'include/items.php';
logger("Item with uri ".$item["uri"]." is from a blocked contact.", LOGGER_DEBUG);
} else {
// We are having duplicated entries. Hopefully this solves it.
- if (Lock::acquireLock('ostatus_process_item_insert')) {
+ if (Lock::acquire('ostatus_process_item_insert')) {
$ret = Item::insert($item);
- Lock::releaseLock('ostatus_process_item_insert');
+ Lock::release('ostatus_process_item_insert');
logger("Item with uri ".$item["uri"]." for user ".$importer["uid"].' stored. Return value: '.$ret);
} else {
$ret = Item::insert($item);
<?php
-namespace Friendica\Test\Core\Lock;
+namespace Friendica\Test\src\Core\Lock;
use Friendica\Core\Cache\ArrayCache;
<?php
-namespace Friendica\Test\Core\Lock;
+namespace Friendica\Test\src\Core\Lock;
use dba;
use Friendica\Core\Lock\DatabaseLockDriver;
<?php
-namespace Friendica\Test\Core\Lock;
+namespace Friendica\Test\src\Core\Lock;
use Friendica\App;
use Friendica\Core\Config;
}
public function testLock() {
- $this->instance->acquire('foo', 1);
+ $this->instance->acquireLock('foo', 1);
$this->assertTrue($this->instance->isLocked('foo'));
$this->assertFalse($this->instance->isLocked('bar'));
}
public function testDoubleLock() {
- $this->instance->acquire('foo', 1);
+ $this->instance->acquireLock('foo', 1);
$this->assertTrue($this->instance->isLocked('foo'));
// We already locked it
- $this->assertTrue($this->instance->acquire('foo', 1));
+ $this->assertTrue($this->instance->acquireLock('foo', 1));
}
public function testReleaseLock() {
- $this->instance->acquire('foo', 1);
+ $this->instance->acquireLock('foo', 1);
$this->assertTrue($this->instance->isLocked('foo'));
- $this->instance->release('foo');
+ $this->instance->releaseLock('foo');
$this->assertFalse($this->instance->isLocked('foo'));
}
public function testReleaseAll() {
- $this->instance->acquire('foo', 1);
- $this->instance->acquire('bar', 1);
- $this->instance->acquire('#/$%§', 1);
+ $this->instance->acquireLock('foo', 1);
+ $this->instance->acquireLock('bar', 1);
+ $this->instance->acquireLock('nice', 1);
$this->instance->releaseAll();
$this->assertFalse($this->instance->isLocked('foo'));
$this->assertFalse($this->instance->isLocked('bar'));
- $this->assertFalse($this->instance->isLocked('#/$%§'));
+ $this->assertFalse($this->instance->isLocked('nice'));
}
public function testReleaseAfterUnlock() {
- $this->instance->acquire('foo', 1);
- $this->instance->acquire('bar', 1);
- $this->instance->acquire('#/$%§', 1);
+ $this->instance->acquireLock('foo', 1);
+ $this->instance->acquireLock('bar', 1);
+ $this->instance->acquireLock('nice', 1);
- $this->instance->release('foo');
+ $this->instance->releaseLock('foo');
$this->instance->releaseAll();
<?php
-namespace Friendica\Test\Core\Lock;
+namespace Friendica\Test\src\Core\Lock;
use Friendica\Core\Lock\SemaphoreLockDriver;
class SemaphoreLockDriverTest extends LockTest
{
+ /**
+ * @var \Friendica\Core\Lock\SemaphoreLockDriver
+ */
+ private $semaphoreLockDriver;
+
protected function getInstance()
{
- return new SemaphoreLockDriver();
+ $this->semaphoreLockDriver = new SemaphoreLockDriver();
+ return $this->semaphoreLockDriver;
+ }
+
+ public function tearDown()
+ {
+ $this->semaphoreLockDriver->releaseAll();
+ parent::tearDown();
}
}
\ No newline at end of file