当前位置: 首页>>代码示例>>PHP>>正文


PHP Memcache::add方法代码示例

本文整理汇总了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);
 }
开发者ID:phapi,项目名称:cache-memcache,代码行数:14,代码来源:Memcache.php

示例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);
     }
 }
开发者ID:janci,项目名称:MHDv2,代码行数:10,代码来源:memory_cache.php

示例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;
 }
开发者ID:mrdeadmouse,项目名称:u136006,代码行数:34,代码来源:session_mc.php

示例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);
     }
 }
开发者ID:kremsy,项目名称:manialib,代码行数:8,代码来源:Memcache.php

示例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);
 }
开发者ID:maximebf,项目名称:cachecache,代码行数:8,代码来源:Memcache.php

示例6: exists

 public function exists($id)
 {
     $success = $this->memcache->add($id, 0);
     if (!$success) {
         return true;
     }
     $this->memcache->delete($id);
     return false;
 }
开发者ID:emaphp,项目名称:simplecache,代码行数:9,代码来源:MemcacheProvider.php

示例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();
         }
     }
 }
开发者ID:packaged,项目名称:mutex,代码行数:19,代码来源:MemcacheMutexProvider.php

示例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);
     }
 }
开发者ID:kuria,项目名称:cache,代码行数:11,代码来源:MemcacheDriver.php

示例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;
 }
开发者ID:chaoyanjie,项目名称:MonkeyPHP,代码行数:20,代码来源:Memcache.php

示例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;
     }
 }
开发者ID:php-yaoi,项目名称:php-yaoi,代码行数:12,代码来源:Memcache.php

示例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;
	}
开发者ID:nycmic,项目名称:bittest,代码行数:52,代码来源:session_mc.php

示例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;
 }
开发者ID:raz0rsdge,项目名称:horde,代码行数:30,代码来源:Memcache.php

示例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;
}
开发者ID:ntulip,项目名称:TwitApps,代码行数:26,代码来源:shared.php

示例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;
	}
开发者ID:realityking,项目名称:joomla-platform,代码行数:34,代码来源:memcache.php

示例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;
 }
开发者ID:dongnan,项目名称:linkcache,代码行数:34,代码来源:Memcache.php


注:本文中的Memcache::add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。