本文整理汇总了PHP中Memcache::add方法的典型用法代码示例。如果您正苦于以下问题:PHP Memcache::add方法的具体用法?PHP Memcache::add怎么用?PHP Memcache::add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Memcache
的用法示例。
在下文中一共展示了Memcache::add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: set
/**
* Set/add something to the cache
*
* @param string $key Key for the value
* @param mixed $value Value
* @return boolean
*/
public function set($key, $value)
{
if ($this->cache->add($key, $value, $this->compression, $this->expire)) {
return true;
}
return $this->cache->replace($key, $value, $this->compression, $this->expire);
}
示例2: save
/**
* Save getting data to memory (RAM)
*/
public function save()
{
$this->storage_data['last_update'] = $this->last_update;
if ($this->memcache->replace('storage_data', $this->storage_data) === false) {
$this->memcache->add('storage_data', $this->storage_data);
}
}
示例3: read
/**
* @param string $id - session id, must be valid hash
* @return string
*/
public static function read($id)
{
if (!self::isConnected() || !self::isValidId($id)) {
return "";
}
$sid = self::getPrefix();
if (!self::$isReadOnly) {
$lockTimeout = 55;
//TODO: add setting
$lockWait = 59000000;
//micro seconds = 60 seconds TODO: add setting
$waitStep = 100;
while (!self::$connection->add($sid . $id . ".lock", 1, 0, $lockTimeout)) {
usleep($waitStep);
$lockWait -= $waitStep;
if ($lockWait < 0) {
CSecuritySession::triggerFatalError('Unable to get session lock within 60 seconds.');
}
if ($waitStep < 1000000) {
$waitStep *= 2;
}
}
}
self::$sessionId = $id;
$res = self::$connection->get($sid . $id);
if ($res === false) {
$res = "";
}
return $res;
}
示例4: add
function add($key, $value, $ttl = 0)
{
$key = str_replace('\\', '/', $key);
if (!$this->memcache->add($key, $value, false, $ttl)) {
$message = sprintf('Memcache::set() with key "%s" failed', $key);
\ManiaLib\Utils\Logger::error($message);
}
}
示例5: add
public function add($id, $value, $ttl = null)
{
$ttl = $ttl ?: 0;
if ($ttl > 0) {
$ttl = time() + $ttl;
}
return $this->memcache->add($id, $value, 0, $ttl);
}
示例6: exists
public function exists($id)
{
$success = $this->memcache->add($id, 0);
if (!$success) {
return true;
}
$this->memcache->delete($id);
return false;
}
示例7: lock
/**
* Lock the mutex
*
* @param string $mutexKey Identifier of the mutex.
* @param int $expiry How long in seconds to keep the mutex locked just in
* case the script dies. 0 = never expires.
*
* @throws LockFailedException
*/
public function lock($mutexKey, $expiry)
{
// Try and set the value. If it fails check to see if
// it already contains our ID
if (!$this->_memcache->add($mutexKey, $this->_getLockId(), null, $expiry)) {
if (!$this->isLocked($mutexKey)) {
throw new LockFailedException();
}
}
}
示例8: store
public function store($key, $value, $overwrite, $ttl = 0)
{
// always store as an UNIX timestamp
// (see description of the expire parameter in the PHP manunal)
$expire = $ttl > 0 ? time() + $ttl : 0;
if ($overwrite) {
return $this->memcache->set($key, $value, 0, $expire);
} else {
return $this->memcache->add($key, $value, 0, $expire);
}
}
示例9: store
/**
* 设置缓存
*
* @param string $key 要设置的缓存项目名称
* @param mixed $value 要设置的缓存项目内容
* @param int $time 要设置的缓存项目的过期时长,默认保存时间为 -1,永久保存为 0
*
* @return bool 保存是成功为true ,失败为false
*/
public function store($key, $value, $time = -1)
{
if ($time == -1) {
$time = $this->expire;
}
$sValue = serialize($value);
if (!$this->connection->add($key, $sValue, $this->compressed, $time)) {
return $this->connection->set($key, $sValue, $this->compressed, $time);
}
return true;
}
示例10: keyExists
function keyExists($key)
{
if (null === $this->memcache) {
$this->connect();
}
if ($this->memcache->add($key, null)) {
$this->memcache->delete($key);
return false;
} else {
return true;
}
}
示例11: read
/**
* @param string $id - session id, must be valid hash
* @return string
*/
public static function read($id)
{
if(!self::isConnected() || !self::isValidId($id))
return "";
$sid = self::getPrefix();
if (!self::$isReadOnly)
{
$lockTimeout = 55;//TODO: add setting
$lockWait = 59000000;//micro seconds = 60 seconds TODO: add setting
$waitStep = 100;
if (defined('BX_SECURITY_SESSION_MEMCACHE_EXLOCK') && BX_SECURITY_SESSION_MEMCACHE_EXLOCK)
$lock = Bitrix\Main\Context::getCurrent()->getRequest()->getRequestedPage();
else
$lock = 1;
while(!self::$connection->add($sid.$id.".lock", $lock, 0, $lockTimeout))
{
usleep($waitStep);
$lockWait -= $waitStep;
if($lockWait < 0)
{
$errorText = 'Unable to get session lock within 60 seconds.';
if ($lock !== 1)
{
$lockedUri = self::$connection->get($sid.$id.".lock");
if ($lockedUri && $lockedUri != 1)
$errorText .= sprintf(' Locked by "%s".', self::$connection->get($sid.$id.".lock"));
}
CSecuritySession::triggerFatalError($errorText);
}
if($waitStep < 1000000)
$waitStep *= 2;
}
}
self::$sessionId = $id;
self::$isSessionReady = true;
$res = self::$connection->get($sid.$id);
if($res === false)
$res = "";
return $res;
}
示例12: lock
/**
* Obtain lock on a key.
*
* @param string $key The key to lock.
*/
public function lock($key)
{
$i = 0;
while ($this->_memcache->add($this->_key($key . self::LOCK_SUFFIX), 1, 0, self::LOCK_TIMEOUT) === false) {
usleep(min(pow(2, $i++) * 10000, 100000));
}
/* Register a shutdown handler function here to catch cases where PHP
* suffers a fatal error. Must be done via shutdown function, since
* a destructor will not be called in this case.
* Only trigger on error, since we must assume that the code that
* locked will also handle unlocks (which may occur in the destruct
* phase, e.g. session handling).
* @todo: $this is not usable in closures until PHP 5.4+ */
if (empty($this->_locks)) {
$self = $this;
register_shutdown_function(function () use($self) {
$e = error_get_last();
if ($e['type'] & E_ERROR) {
/* Try to do cleanup at very end of shutdown methods. */
register_shutdown_function(array($self, 'shutdown'));
}
});
}
$this->_locks[$key] = true;
}
示例13: cache
function cache($op, $key, $val = '', $expiry = 604800)
{
static $memcache = false;
if ($memcache === false) {
$memcache = new Memcache();
$memcache->connect('localhost', 11211) or die('Fatal error - could not connect to Memcache');
}
$retval = true;
// Prefix the key to avoid collisions with other apps
$key = 'twitapps_' . $key;
switch ($op) {
case 'set':
$memcache->set($key, $val, false, $expiry) or die('Fatal error - could not store ' . htmlentities($key) . ' in Memcache');
break;
case 'get':
$retval = $memcache->get($key);
break;
case 'inc':
$retval = $memcache->increment($key);
break;
case 'add':
$retval = $memcache->add($key, $val, false, $expiry);
break;
}
return $retval;
}
示例14: lockindex
/**
* Lock cache index
*
* @return boolean True on success, false otherwise.
*
* @since 11.1
*/
protected function lockindex()
{
$looptime = 300;
$data_lock = self::$_db->add($this->_hash . '-index_lock', 1, false, 30);
if ($data_lock === false)
{
$lock_counter = 0;
// Loop until you find that the lock has been released. that implies that data get from other thread has finished
while ($data_lock === false)
{
if ($lock_counter > $looptime)
{
return false;
break;
}
usleep(100);
$data_lock = self::$_db->add($this->_hash . '-index_lock', 1, false, 30);
$lock_counter++;
}
}
return true;
}
示例15: mSetNX
/**
* 批量设置键值(当键名不存在时);<br>
* 只有当键值全部设置成功时,才返回true,否则返回false并尝试回滚
* @param array $sets 键值数组
* @return boolean 是否成功
*/
public function mSetNX($sets)
{
try {
$keys = [];
$status = true;
foreach ($sets as $key => $value) {
$value = self::setValue($value);
$status = $this->handler->add($key, $value, $this->compress($value));
if ($status) {
$keys[] = $key;
} else {
break;
}
}
//如果失败,尝试回滚,但不保证成功
if (!$status) {
foreach ($keys as $key) {
$this->handler->delete($key);
}
}
return $status;
} catch (Exception $ex) {
self::exception($ex);
//连接状态置为false
$this->isConnected = false;
}
return false;
}