本文整理汇总了PHP中BagOStuff::get方法的典型用法代码示例。如果您正苦于以下问题:PHP BagOStuff::get方法的具体用法?PHP BagOStuff::get怎么用?PHP BagOStuff::get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BagOStuff
的用法示例。
在下文中一共展示了BagOStuff::get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: initPositions
/**
* Load in previous master positions for the client
*/
protected function initPositions()
{
if ($this->initialized) {
return;
}
$this->initialized = true;
if ($this->wait) {
// If there is an expectation to see master positions with a certain min
// timestamp, then block until they appear, or until a timeout is reached.
if ($this->waitForPosTime > 0.0) {
$data = null;
$loop = new WaitConditionLoop(function () use(&$data) {
$data = $this->store->get($this->key);
return self::minPosTime($data) >= $this->waitForPosTime ? WaitConditionLoop::CONDITION_REACHED : WaitConditionLoop::CONDITION_CONTINUE;
}, $this->waitForPosTimeout);
$result = $loop->invoke();
$waitedMs = $loop->getLastWaitTime() * 1000.0;
if ($result == $loop::CONDITION_REACHED) {
$msg = "expected and found pos time {$this->waitForPosTime} ({$waitedMs}ms)";
$this->logger->debug($msg);
} else {
$msg = "expected but missed pos time {$this->waitForPosTime} ({$waitedMs}ms)";
$this->logger->info($msg);
}
} else {
$data = $this->store->get($this->key);
}
$this->startupPositions = $data ? $data['positions'] : [];
$this->logger->info(__METHOD__ . ": key is {$this->key} (read)\n");
} else {
$this->startupPositions = [];
$this->logger->info(__METHOD__ . ": key is {$this->key} (unread)\n");
}
}
示例2: doGetAllReadyWikiQueues
/**
* @see JobQueueAggregator::doAllGetReadyWikiQueues()
*/
protected function doGetAllReadyWikiQueues()
{
$key = $this->getReadyQueueCacheKey();
// If the cache entry wasn't present, is stale, or in .1% of cases otherwise,
// regenerate the cache. Use any available stale cache if another process is
// currently regenerating the pending DB information.
$pendingDbInfo = $this->cache->get($key);
if (!is_array($pendingDbInfo) || time() - $pendingDbInfo['timestamp'] > $this->cacheTTL || mt_rand(0, 999) == 0) {
if ($this->cache->add("{$key}:rebuild", 1, 1800)) {
// lock
$pendingDbInfo = array('pendingDBs' => $this->findPendingWikiQueues(), 'timestamp' => time());
for ($attempts = 1; $attempts <= 25; ++$attempts) {
if ($this->cache->add("{$key}:lock", 1, 60)) {
// lock
$this->cache->set($key, $pendingDbInfo);
$this->cache->delete("{$key}:lock");
// unlock
break;
}
}
$this->cache->delete("{$key}:rebuild");
// unlock
}
}
return is_array($pendingDbInfo) ? $pendingDbInfo['pendingDBs'] : array();
// cache is both empty and locked
}
示例3: doPop
protected function doPop() {
$key = $this->getCacheKey( 'empty' );
$isEmpty = $this->cache->get( $key );
if ( $isEmpty === 'true' ) {
return false;
}
$partitionsTry = $this->partitionMap; // (partition => weight)
while ( count( $partitionsTry ) ) {
$partition = ArrayUtils::pickRandom( $partitionsTry );
if ( $partition === false ) {
break; // all partitions at 0 weight
}
$queue = $this->partitionQueues[$partition];
try {
$job = $queue->pop();
} catch ( JobQueueError $e ) {
$job = false;
MWExceptionHandler::logException( $e );
}
if ( $job ) {
$job->metadata['QueuePartition'] = $partition;
return $job;
} else {
unset( $partitionsTry[$partition] ); // blacklist partition
}
}
$this->cache->set( $key, 'true', JobQueueDB::CACHE_TTL_LONG );
return false;
}
示例4: testIncr
/**
* @covers BagOStuff::incr
*/
public function testIncr()
{
$key = wfMemcKey('test');
$this->cache->add($key, 0);
$this->cache->incr($key);
$expectedValue = 1;
$actualValue = $this->cache->get($key);
$this->assertEquals($expectedValue, $actualValue, 'Value should be 1 after incrementing');
}
示例5: claimRandom
/**
* Reserve a row with a single UPDATE without holding row locks over RTTs...
*
* @param string $uuid 32 char hex string
* @param $rand integer Random unsigned integer (31 bits)
* @param bool $gte Search for job_random >= $random (otherwise job_random <= $random)
* @return Row|false
*/
protected function claimRandom($uuid, $rand, $gte)
{
list($dbw, $scope) = $this->getMasterDB();
// Check cache to see if the queue has <= OFFSET items
$tinyQueue = $this->cache->get($this->getCacheKey('small'));
$row = false;
// the row acquired
$invertedDirection = false;
// whether one job_random direction was already scanned
// This uses a replication safe method for acquiring jobs. One could use UPDATE+LIMIT
// instead, but that either uses ORDER BY (in which case it deadlocks in MySQL) or is
// not replication safe. Due to http://bugs.mysql.com/bug.php?id=6980, subqueries cannot
// be used here with MySQL.
do {
if ($tinyQueue) {
// queue has <= MAX_OFFSET rows
// For small queues, using OFFSET will overshoot and return no rows more often.
// Instead, this uses job_random to pick a row (possibly checking both directions).
$ineq = $gte ? '>=' : '<=';
$dir = $gte ? 'ASC' : 'DESC';
$row = $dbw->selectRow('job', '*', array('job_cmd' => $this->type, 'job_token' => '', "job_random {$ineq} {$dbw->addQuotes($rand)}"), __METHOD__, array('ORDER BY' => "job_random {$dir}"));
if (!$row && !$invertedDirection) {
$gte = !$gte;
$invertedDirection = true;
continue;
// try the other direction
}
} else {
// table *may* have >= MAX_OFFSET rows
// Bug 42614: "ORDER BY job_random" with a job_random inequality causes high CPU
// in MySQL if there are many rows for some reason. This uses a small OFFSET
// instead of job_random for reducing excess claim retries.
$row = $dbw->selectRow('job', '*', array('job_cmd' => $this->type, 'job_token' => ''), __METHOD__, array('OFFSET' => mt_rand(0, self::MAX_OFFSET)));
if (!$row) {
$tinyQueue = true;
// we know the queue must have <= MAX_OFFSET rows
$this->cache->set($this->getCacheKey('small'), 1, 30);
continue;
// use job_random
}
}
if ($row) {
// claim the job
$dbw->update('job', array('job_token' => $uuid, 'job_token_timestamp' => $dbw->timestamp(), 'job_attempts = job_attempts+1'), array('job_cmd' => $this->type, 'job_id' => $row->job_id, 'job_token' => ''), __METHOD__);
// This might get raced out by another runner when claiming the previously
// selected row. The use of job_random should minimize this problem, however.
if (!$dbw->affectedRows()) {
$row = false;
// raced out
}
} else {
break;
// nothing to do
}
} while (!$row);
return $row;
}
示例6: getFileContentsHashInternal
/**
* Get a hash of a file's contents, either by retrieving a previously-
* computed hash from the cache, or by computing a hash from the file.
*
* @private
* @param string $filePath Full path to the file.
* @param string $algo Name of selected hashing algorithm.
* @return string|bool Hash of file contents, or false if the file could not be read.
*/
public function getFileContentsHashInternal($filePath, $algo = 'md4')
{
$mtime = MediaWiki\quietCall('filemtime', $filePath);
if ($mtime === false) {
return false;
}
$cacheKey = wfGlobalCacheKey(__CLASS__, $filePath, $mtime, $algo);
$hash = $this->cache->get($cacheKey);
if ($hash) {
return $hash;
}
$contents = MediaWiki\quietCall('file_get_contents', $filePath);
if ($contents === false) {
return false;
}
$hash = hash($algo, $contents);
$this->cache->set($cacheKey, $hash, 60 * 60 * 24);
// 24h
return $hash;
}
示例7: getSites
/**
* @see SiteStore::getSites
*
* @since 1.25
*
* @return SiteList
*/
public function getSites()
{
if ($this->sites === null) {
$this->sites = $this->cache->get($this->getCacheKey());
if (!is_object($this->sites)) {
$this->sites = $this->siteStore->getSites();
$this->cache->set($this->getCacheKey(), $this->sites, $this->cacheTimeout);
}
}
return $this->sites;
}
示例8: getFileContentsHashInternal
/**
* Get a hash of a file's contents, either by retrieving a previously-
* computed hash from the cache, or by computing a hash from the file.
*
* @private
* @param string $filePath Full path to the file.
* @param string $algo Name of selected hashing algorithm.
* @return string|bool Hash of file contents, or false if the file could not be read.
*/
public function getFileContentsHashInternal($filePath, $algo = 'md4')
{
$mtime = filemtime($filePath);
if ($mtime === false) {
return false;
}
$cacheKey = $this->cache->makeGlobalKey(__CLASS__, $filePath, $mtime, $algo);
$hash = $this->cache->get($cacheKey);
if ($hash) {
return $hash;
}
$contents = file_get_contents($filePath);
if ($contents === false) {
return false;
}
$hash = hash($algo, $contents);
$this->cache->set($cacheKey, $hash, 60 * 60 * 24);
// 24h
return $hash;
}
示例9: doIsRootJobOldDuplicate
/**
* @see JobQueue::isRootJobOldDuplicate()
* @param Job $job
* @return bool
*/
protected function doIsRootJobOldDuplicate(Job $job)
{
if (!$job->hasRootJobParams()) {
return false;
// job has no de-deplication info
}
$params = $job->getRootJobParams();
$key = $this->getRootJobCacheKey($params['rootJobSignature']);
// Get the last time this root job was enqueued
$timestamp = $this->dupCache->get($key);
// Check if a new root job was started at the location after this one's...
return $timestamp && $timestamp > $params['rootJobTimestamp'];
}
示例10: cacheRecordFailure
/**
* Log a lock request failure to the cache
*
* @param $lockDb string
* @return bool Success
*/
protected function cacheRecordFailure($lockDb)
{
if ($this->statusCache && $this->safeDelay > 0) {
$path = $this->getMissKey($lockDb);
$misses = $this->statusCache->get($path);
if ($misses) {
return $this->statusCache->incr($path);
} else {
return $this->statusCache->add($path, 1, $this->safeDelay);
}
}
return true;
}
示例11: loadFromDBWithLock
/**
* @param string $code
* @param array $where List of wfDebug() comments
* @param integer $mode Use MessageCache::FOR_UPDATE to use DB_MASTER
* @return bool|string True on success or one of ("cantacquire", "disabled")
*/
protected function loadFromDBWithLock($code, array &$where, $mode = null)
{
global $wgUseLocalMessageCache;
# If cache updates on all levels fail, give up on message overrides.
# This is to avoid easy site outages; see $saveSuccess comments below.
$statusKey = wfMemcKey('messages', $code, 'status');
$status = $this->mMemc->get($statusKey);
if ($status === 'error') {
$where[] = "could not load; method is still globally disabled";
return 'disabled';
}
# Now let's regenerate
$where[] = 'loading from database';
# Lock the cache to prevent conflicting writes.
# This lock is non-blocking so stale cache can quickly be used.
# Note that load() will call a blocking getReentrantScopedLock()
# after this if it really need to wait for any current thread.
$cacheKey = wfMemcKey('messages', $code);
$scopedLock = $this->getReentrantScopedLock($cacheKey, 0);
if (!$scopedLock) {
$where[] = 'could not acquire main lock';
return 'cantacquire';
}
$cache = $this->loadFromDB($code, $mode);
$this->mCache[$code] = $cache;
$saveSuccess = $this->saveToCaches($cache, 'all', $code);
if (!$saveSuccess) {
# Cache save has failed.
# There are two main scenarios where this could be a problem:
#
# - The cache is more than the maximum size (typically
# 1MB compressed).
#
# - Memcached has no space remaining in the relevant slab
# class. This is unlikely with recent versions of
# memcached.
#
# Either way, if there is a local cache, nothing bad will
# happen. If there is no local cache, disabling the message
# cache for all requests avoids incurring a loadFromDB()
# overhead on every request, and thus saves the wiki from
# complete downtime under moderate traffic conditions.
if (!$wgUseLocalMessageCache) {
$this->mMemc->set($statusKey, 'error', 60 * 5);
$where[] = 'could not save cache, disabled globally for 5 minutes';
} else {
$where[] = "could not save global cache";
}
}
return true;
}
示例12: getConnection
/**
* Get an authenticated connection handle to the Swift proxy
*
* @throws CloudFilesException
* @throws CloudFilesException|Exception
* @return CF_Connection|bool False on failure
*/
protected function getConnection()
{
if ($this->connException instanceof CloudFilesException) {
if (time() - $this->connErrorTime < 60) {
throw $this->connException;
// failed last attempt; don't bother
} else {
// actually retry this time
$this->connException = null;
$this->connErrorTime = 0;
}
}
// Session keys expire after a while, so we renew them periodically
$reAuth = time() - $this->sessionStarted > $this->authTTL;
// Authenticate with proxy and get a session key...
if (!$this->conn || $reAuth) {
$this->sessionStarted = 0;
$this->connContainerCache->clear();
$cacheKey = $this->getCredsCacheKey($this->auth->username);
$creds = $this->srvCache->get($cacheKey);
// credentials
if (is_array($creds)) {
// cache hit
$this->auth->load_cached_credentials($creds['auth_token'], $creds['storage_url'], $creds['cdnm_url']);
$this->sessionStarted = time() - ceil($this->authTTL / 2);
// skew for worst case
} else {
// cache miss
try {
$this->auth->authenticate();
$creds = $this->auth->export_credentials();
$this->srvCache->add($cacheKey, $creds, ceil($this->authTTL / 2));
// cache
$this->sessionStarted = time();
} catch (CloudFilesException $e) {
$this->connException = $e;
// don't keep re-trying
$this->connErrorTime = time();
throw $e;
// throw it back
}
}
if ($this->conn) {
// re-authorizing?
$this->conn->close();
// close active cURL handles in CF_Http object
}
$this->conn = new CF_Connection($this->auth);
}
return $this->conn;
}
示例13: doWait
/**
* Wait for a given slave to catch up to the master pos stored in $this
* @param int $index Server index
* @param bool $open Check the server even if a new connection has to be made
* @param int $timeout Max seconds to wait; default is mWaitTimeout
* @return bool
*/
protected function doWait($index, $open = false, $timeout = null)
{
$close = false;
// close the connection afterwards
// Check if we already know that the DB has reached this point
$server = $this->getServerName($index);
$key = $this->srvCache->makeGlobalKey(__CLASS__, 'last-known-pos', $server);
/** @var DBMasterPos $knownReachedPos */
$knownReachedPos = $this->srvCache->get($key);
if ($knownReachedPos && $knownReachedPos->hasReached($this->mWaitForPos)) {
wfDebugLog('replication', __METHOD__ . ": slave {$server} known to be caught up (pos >= {$knownReachedPos}).\n");
return true;
}
// Find a connection to wait on, creating one if needed and allowed
$conn = $this->getAnyOpenConnection($index);
if (!$conn) {
if (!$open) {
wfDebugLog('replication', __METHOD__ . ": no connection open for {$server}\n");
return false;
} else {
$conn = $this->openConnection($index, '');
if (!$conn) {
wfDebugLog('replication', __METHOD__ . ": failed to connect to {$server}\n");
return false;
}
// Avoid connection spam in waitForAll() when connections
// are made just for the sake of doing this lag check.
$close = true;
}
}
wfDebugLog('replication', __METHOD__ . ": Waiting for slave {$server} to catch up...\n");
$timeout = $timeout ?: $this->mWaitTimeout;
$result = $conn->masterPosWait($this->mWaitForPos, $timeout);
if ($result == -1 || is_null($result)) {
// Timed out waiting for slave, use master instead
$msg = __METHOD__ . ": Timed out waiting on {$server} pos {$this->mWaitForPos}";
wfDebugLog('replication', "{$msg}\n");
wfDebugLog('DBPerformance', "{$msg}:\n" . wfBacktrace(true));
$ok = false;
} else {
wfDebugLog('replication', __METHOD__ . ": Done\n");
$ok = true;
// Remember that the DB reached this point
$this->srvCache->set($key, $this->mWaitForPos, BagOStuff::TTL_DAY);
}
if ($close) {
$this->closeConnection($conn);
}
return $ok;
}
示例14: getAuthentication
/**
* @return array|null Credential map
*/
protected function getAuthentication()
{
if ($this->authErrorTimestamp !== null) {
if (time() - $this->authErrorTimestamp < 60) {
return null;
// failed last attempt; don't bother
} else {
// actually retry this time
$this->authErrorTimestamp = null;
}
}
// Session keys expire after a while, so we renew them periodically
$reAuth = time() - $this->authSessionTimestamp > $this->authTTL;
// Authenticate with proxy and get a session key...
if (!$this->authCreds || $reAuth) {
$this->authSessionTimestamp = 0;
$cacheKey = $this->getCredsCacheKey($this->swiftUser);
$creds = $this->srvCache->get($cacheKey);
// credentials
// Try to use the credential cache
if (isset($creds['auth_token']) && isset($creds['storage_url'])) {
$this->authCreds = $creds;
// Skew the timestamp for worst case to avoid using stale credentials
$this->authSessionTimestamp = time() - ceil($this->authTTL / 2);
} else {
// cache miss
list($rcode, $rdesc, $rhdrs, $rbody, $rerr) = $this->http->run(array('method' => 'GET', 'url' => "{$this->swiftAuthUrl}/v1.0", 'headers' => array('x-auth-user' => $this->swiftUser, 'x-auth-key' => $this->swiftKey)));
if ($rcode >= 200 && $rcode <= 299) {
// OK
$this->authCreds = array('auth_token' => $rhdrs['x-auth-token'], 'storage_url' => $rhdrs['x-storage-url']);
$this->srvCache->set($cacheKey, $this->authCreds, ceil($this->authTTL / 2));
$this->authSessionTimestamp = time();
} elseif ($rcode === 401) {
$this->onError(null, __METHOD__, array(), "Authentication failed.", $rcode);
$this->authErrorTimestamp = time();
return null;
} else {
$this->onError(null, __METHOD__, array(), "HTTP return code: {$rcode}", $rcode);
$this->authErrorTimestamp = time();
return null;
}
}
// Ceph RGW does not use <account> in URLs (OpenStack Swift uses "/v1/<account>")
if (substr($this->authCreds['storage_url'], -3) === '/v1') {
$this->isRGW = true;
// take advantage of strong consistency in Ceph
}
}
return $this->authCreds;
}
示例15: initPositions
/**
* Load in previous master positions for the client
*/
protected function initPositions()
{
if ($this->initialized) {
return;
}
$this->initialized = true;
if ($this->wait) {
$data = $this->store->get($this->key);
$this->startupPositions = $data ? $data['positions'] : array();
wfDebugLog('replication', __METHOD__ . ": key is {$this->key} (read)\n");
} else {
$this->startupPositions = array();
wfDebugLog('replication', __METHOD__ . ": key is {$this->key} (unread)\n");
}
}