Exemple #1
0
 function set($key, &$value)
 {
     if (!$this->_cache->set($key, $value, false, $this->_timeout)) {
         return false;
     }
     return true;
 }
Exemple #2
0
 /**
  * Load model
  *
  * @param string $model
  */
 public function model($model)
 {
     $file = DIR_BASE . 'application' . DIR_SEPARATOR . 'model' . DIR_SEPARATOR . $model . '.php';
     $class = 'Model' . preg_replace('/[^a-zA-Z0-9]/', '', $model);
     if (file_exists($file)) {
         include_once $file;
         $this->registry->set('model_' . str_replace(DIR_SEPARATOR, '_', $model), new $class($this->registry));
     } else {
         trigger_error('Error: Could not load model ' . $file . '!');
         exit;
     }
 }
 /**
  * @param $key
  * @param $value
  *
  * @return bool
  */
 function set($key, &$value)
 {
     if (!$this->_cache->set($this->_prefix . $key, $value, FALSE, $this->_timeout)) {
         return FALSE;
     }
     return TRUE;
 }
Exemple #4
0
	/**
	 * Store the data to memcache by id and group
	 *
	 * @access	public
	 * @param	string	$id		The cache data id
	 * @param	string	$group	The cache data group
	 * @param	string	$data	The data to store in cache
	 * @return	boolean	True on success, false otherwise
	 * @since	1.5
	 */
	function store($id, $group, $data)
	{
		$cache_id = $this->_getCacheId($id, $group);

		if (!$this->lockindex()) {
			return false;
		}

		$index = $this->_db->get($this->_hash.'-index');
		if ($index === false) {
			$index = array();
		}

		$tmparr = new stdClass;
		$tmparr->name = $cache_id;
		$tmparr->size = strlen($data);
		$index[] = $tmparr;
		
		$this->_db->replace($this->_hash.'-index', $index , 0, 0);
		$this->unlockindex();

		// prevent double writes, write only if it doesn't exist else replace
		if (!$this->_db->replace($cache_id, $data, $this->_compress, $this->_lifetime)) {
			$this->_db->set($cache_id, $data, $this->_compress, $this->_lifetime);
		}

		return true;
	}
 /**
  * Write
  *
  * Writes (create / update) session data
  *
  * @param	string	$session_id	Session ID
  * @param	string	$session_data	Serialized session data
  * @return	bool
  */
 public function write($session_id, $session_data)
 {
     if (!isset($this->_redis)) {
         return $this->_fail();
     } elseif ($session_id !== $this->_session_id) {
         if (!$this->_release_lock() or !$this->_get_lock($session_id)) {
             return $this->_fail();
         }
         $this->_key_exists = FALSE;
         $this->_session_id = $session_id;
     }
     if (isset($this->_lock_key)) {
         $this->_redis->setTimeout($this->_lock_key, 300);
         if ($this->_fingerprint !== ($fingerprint = md5($session_data)) or $this->_key_exists === FALSE) {
             if ($this->_redis->set($this->_key_prefix . $session_id, $session_data, $this->_config['expiration'])) {
                 $this->_fingerprint = $fingerprint;
                 $this->_key_exists = TRUE;
                 return $this->_success;
             }
             return $this->_fail();
         }
         return $this->_redis->setTimeout($this->_key_prefix . $session_id, $this->_config['expiration']) ? $this->_success : $this->_fail();
     }
     return $this->_fail();
 }
Exemple #6
0
 /**
  * @param $key
  * @param $value
  *
  * @return bool
  * @throws Exception
  */
 public function set($key, &$value)
 {
     if (!$this->_cache->set($this->_prefix . $key, serialize($value), $this->_timeout)) {
         CRM_Core_Error::debug('Result Code: ', $this->_cache->getResultMessage());
         CRM_Core_Error::fatal("Redis set failed, wondering why?, {$key}", $value);
         return FALSE;
     }
     return TRUE;
 }
Exemple #7
0
 /**
  * Writes session data to the memcache based session storage handler
  *
  * @param string $id The ID of the session
  * @param string $value The session data to store
  * @access protected
  */
 protected function _write($id, $value)
 {
     $id = 'sess_' . $id;
     if ($this->_conn->get($id)) {
         return $this->_conn->replace($id, $value, 0, $this->_life_time);
     } else {
         return $this->_conn->set($id, $value, 0, $this->_life_time);
     }
 }
 /**
  * @param $key
  * @param $value
  *
  * @return bool
  * @throws Exception
  */
 function set($key, &$value)
 {
     $key = $this->cleanKey($key);
     if (!$this->_cache->set($key, $value, $this->_timeout)) {
         CRM_Core_Error::debug('Result Code: ', $this->_cache->getResultMessage());
         CRM_Core_Error::fatal("memcached set failed, wondering why?, {$key}", $value);
         return FALSE;
     }
     return TRUE;
 }
 /**
  * Adds the $var data to the cache under the key $key. Returns true or
  * false depending on the success of the operation.
  *
  * @param string $key
  * @param mixed $var
  * @param int $expire
  * @return bool
  */
 public function store($key, $var, $expire = 0)
 {
     if (strlen($key) > self::MAX_KEY_LENGTH) {
         throw new ezcCacheInvalidKeyException($key, 'Length > ' . self::MAX_KEY_LENGTH . '.');
     }
     // protect our data by wrapping it in an object
     $data = new ezcCacheMemoryVarStruct($key, $var, $expire);
     $compressed = $this->options->compressed === true ? MEMCACHE_COMPRESSED : false;
     return $this->memcache->set($key, $data, $compressed, $expire);
 }
Exemple #10
0
 /**
  * 写入缓存,使用pecl-memcache
  *
  * @param array $set
  * @param array $policy
  * @access protected
  * @return boolean
  */
 protected function _setMemcache(array $set, array $policy = array())
 {
     $compressed = isset($policy['compressed']) ? $policy['compressed'] : $this->_default_policy['compressed'];
     $life_time = isset($policy['life_time']) ? $policy['life_time'] : $this->_default_policy['life_time'];
     while (list($key, $value) = each($set)) {
         if (!$this->_conn->set($key, $value, $compressed ? MEMCACHE_COMPRESSED : 0, $life_time)) {
             return false;
         }
     }
     return true;
 }
 /**
  * Set expire time on each call since memcache sets it on cache creation.
  *
  * @access private
  *
  * @param string  $key   Cache key to expire.
  * @param integer $lifetime  Lifetime of the data in seconds.
  */
 public function _setExpire($key)
 {
     $lifetime = $this->max_lifetime;
     $expire = $this->_db->get($key . '_expire');
     // set prune period
     if ($expire + $lifetime < time()) {
         $this->_db->delete($key);
         $this->_db->delete($key . '_expire');
     } else {
         $this->_db->set($key . '_expire', time(), 0);
     }
 }
Exemple #12
0
 /**
  * 增加缓存
  * @param string $key 缓存键
  * @param mixed $value 缓存值
  * @param int $time 缓存时间
  * @param bool $compress 是否启用压缩
  * @retrun bool
  */
 public function add($key, $value, $time = 0, $compress = MEMCACHE_COMPRESSED)
 {
     $key = DAGGER_MC_KEY_PREFIX . $key;
     defined('DAGGER_DEBUG') && BaseModelCommon::debug($value, "mc_add({$key}),ttl({$time})");
     if ($this->snowslide === true) {
         if (!$this->mc->add($key . self::CACHE_TIME_CTL, 1, $compress, $time)) {
             return false;
         }
         $ret = $this->mc->set($key, $value, $compress, $time + 86400);
     } else {
         return $this->mc->add($key, $value, $compress, $time);
     }
     return $ret;
 }
Exemple #13
0
 /**
  * Write session data to the SessionHandler backend.
  *
  * @param   string  $id            The session identifier.
  * @param   string  $session_data  The session data.
  *
  * @return  boolean  True on success, false otherwise.
  *
  * @since   11.1
  */
 public function write($id, $session_data)
 {
     $sess_id = 'sess_' . $id;
     if ($this->_db->get($sess_id . '_expire')) {
         $this->_db->replace($sess_id . '_expire', time());
     } else {
         $this->_db->set($sess_id . '_expire', time());
     }
     if ($this->_db->get($sess_id)) {
         $this->_db->replace($sess_id, $session_data);
     } else {
         $this->_db->set($sess_id, $session_data);
     }
     return;
 }
Exemple #14
0
 /**
  * 增加缓存
  * @param string $key 缓存键
  * @param mixed $value 缓存值
  * @param int $time 缓存时间
  * @retrun bool
  */
 public function add($key, $value, $time = 0)
 {
     $this->startRunTime = microtime(true);
     $calls = 1;
     if ($this->native) {
         $ret = $this->mcd->add($key, $value, $time);
     } else {
         if ($ret = $this->mcd->add($this->_buildKey($key, 'TIME_CTL'), $value, $time)) {
             $ret = $this->mcd->set($key, $value, $time + 86400);
             $calls++;
         }
         $this->lastResultCode = $this->mcd->getResultCode();
         $this->lastResultMessage = $this->mcd->getResultMessage();
     }
     $this->_checkStats(__FUNCTION__, $calls);
     defined('DAGGER_DEBUG') && BaseModelCommon::debug($value, "mcd_add({$key}),ttl({$time})");
     return $ret;
 }
 /**
  * 打开到服务端的连接
  * @return void
  */
 protected function openConnection()
 {
     $address = self::$addressArray[array_rand(self::$addressArray)];
     if (self::$useSwoole) {
         $address = explode(':', $address);
         $this->swooleClient = new swoole_client(SWOOLE_SOCK_TCP);
         $this->swooleClient->set($this->swooleClientSets);
         if (!$this->swooleClient->connect($address[0], $address[1], self::TIME_OUT)) {
             exit("connect failed. Error: {$this->swooleClient->errCode}\n");
         }
     } else {
         $this->connection = stream_socket_client($address, $err_no, $err_msg);
         if (!$this->connection) {
             throw new Exception("can not connect to {$address} , {$err_no}:{$err_msg}");
         }
         stream_set_blocking($this->connection, true);
         stream_set_timeout($this->connection, self::TIME_OUT);
     }
 }
 /**
  * Write
  *
  * Writes (create / update) session data
  *
  * @param	string	$session_id	Session ID
  * @param	string	$session_data	Serialized session data
  * @return	bool
  */
 public function write($session_id, $session_data)
 {
     if (!isset($this->redis)) {
         return FALSE;
     } elseif ($session_id !== $this->sessionID) {
         if (!$this->releaseLock() || !$this->lockSession($session_id)) {
             return FALSE;
         }
         $this->_fingerprint = md5('');
         $this->_session_id = $session_id;
     }
     if (isset($this->lockKey)) {
         $this->redis->setTimeout($this->lockKey, 300);
         if ($this->fingerprint !== ($fingerprint = md5($session_data))) {
             if ($this->redis->set($this->keyPrefix . $session_id, $session_data, $this->sessionExpiration)) {
                 $this->_fingerprint = $fingerprint;
                 return TRUE;
             }
             return FALSE;
         }
         return $this->redis->setTimeout($this->keyPrefix . $session_id, $this->sessionExpiration);
     }
     return FALSE;
 }
 /**
  * Write
  *
  * Writes (create / update) session data
  *
  * @param	string	$session_id	Session ID
  * @param	string	$session_data	Serialized session data
  * @return	bool
  */
 public function write($session_id, $session_data)
 {
     if (!isset($this->_redis)) {
         return FALSE;
     } elseif ($session_id !== $this->_session_id) {
         if (!$this->_release_lock() or !$this->_get_lock($session_id)) {
             return FALSE;
         }
         $this->_fingerprint = md5('');
         $this->_session_id = $session_id;
     }
     if (isset($this->_lock_key)) {
         $this->_redis->setTimeout($this->_lock_key, 5);
         if ($this->_fingerprint !== ($fingerprint = md5($session_data))) {
             if ($this->_redis->set($this->_key_prefix . $session_id, $session_data, $this->_config['expiration'])) {
                 $this->_fingerprint = $fingerprint;
                 return TRUE;
             }
             return FALSE;
         }
         return $this->_redis->setTimeout($this->_key_prefix . $session_id, $this->_config['expiration']);
     }
     return FALSE;
 }
Exemple #18
0
 /**
  * Store the data to memcache by id and group
  *
  * @access	public
  * @param	string	$id		The cache data id
  * @param	string	$group	The cache data group
  * @param	string	$data	The data to store in cache
  * @return	boolean	True on success, false otherwise
  * @since	1.5
  */
 function store($id, $group, $data)
 {
     $cache_id = $this->_getCacheId($id, $group);
     $this->_db->set($cache_id . '_expire', time(), 0, 0);
     return $this->_db->set($cache_id, $data, $this->_compress, 0);
 }
Exemple #19
0
 /**
  * Magic add item to registry
  *
  * @param mixed $key
  * @param mixed $value
  */
 public function __set($key, $value)
 {
     $this->registry->set($key, $value);
 }
Exemple #20
0
 /**
  * 写入缓存
  *
  * @param string $id
  * @param mixed $data
  * @param array $policy
  * @return boolean
  */
 function set($id, $data, array $policy = null)
 {
     $compressed = isset($policy['compressed']) ? $policy['compressed'] : $this->_default_policy['compressed'];
     $life_time = isset($policy['life_time']) ? $policy['life_time'] : $this->_default_policy['life_time'];
     $this->_conn->set($id, $data, $compressed ? MEMCACHE_COMPRESSED : 0, $life_time);
 }
Exemple #21
0
 /**
  * Выполняет sql запрос. Плейсхолдеры НЕ обрабатывает.
  *
  * @param  string  $sql  sql запрос или пустая строка если необходимо выполнить отложенные запросы
  * @param  string  $orig_sql  исходная версия sql-запрос (с нераспарсенными плейсхолдерами).
  * @return mixed         при sql запросе - ресурс выполненного запроса
  *                       при использовании memcache - двумерный массив с результатом запроса
  *                       при отложенном запросе - TRUE
  *                       при запросе текста запроса - строка с запросом
  *                       при ошибке - FALSE
  */
 public function &squery($sql = '', $orig_sql = NULL)
 {
     $this->error = '';
     $this->res = NULL;
     $this->oRes = NULL;
     $sto = '';
     if (isset($this->_stby_log_row)) {
         unset($this->_stby_log_row);
     }
     if ($sql === '') {
         $this->sql = $this->sqls;
         $this->sqls = '';
     } else {
         $this->sql = $sql;
     }
     $this->origSql = $orig_sql ? $orig_sql : $this->sql;
     if (self::$timeout >= 0) {
         $sto = 'SET statement_timeout = ' . self::$timeout . ';';
         if (self::$timeout == self::DEFAULT_TIMEOUT) {
             $this->setTimeout(-1);
             // т.к. сбросили в умолчание, в след. раз не нужно устанавливать.
         }
     }
     if ($this->mode >= 0) {
         $exec = TRUE;
         $this->time = microtime(TRUE);
         if ($this->mode == 1) {
             $md5 = md5($this->sql);
             $this->res = $this->mem->get($md5);
             if (!empty($this->res)) {
                 $exec = FALSE;
             }
         }
         if ($this->mode == 2) {
             // stats temp
             $trace = debug_backtrace();
             $line = $trace[$this->traceLevel - 1]['line'];
             $stats = $this->memcached->get(self::MPREFIX . 'S');
             if (empty($stats)) {
                 $stats = array();
             }
             // stats temp
             $hash = self::MPREFIX . 'D_' . md5($this->sql);
             $keys = array();
             $exec = TRUE;
             $version = (int) ($this->time * 100);
             if ($this->keys) {
                 $this->res = $this->memcached->getMulti(array_merge($this->keys, array($hash)));
                 $exec = !(bool) $this->res[$hash];
                 foreach ($this->keys as $v) {
                     if (empty($this->res[$v])) {
                         $keys[$v] = $version;
                         $exec = TRUE;
                     } else {
                         if (!$rebuild && $this->res[$v] > $this->res[$hash]['ver']) {
                             $exec = TRUE;
                         }
                     }
                 }
             }
             if (!$exec) {
                 $this->res =& $this->res[$hash]['data'];
                 // stats temp
                 $stats[$line]['mem'] = (int) $stats[$line]['mem'] + 1;
                 $this->memcached->set(self::MPREFIX . 'S', $stats);
                 // stats temp
             }
         }
         if ($exec) {
             for ($i = 0, $only_master = false; $i < 2; $i++) {
                 $ts = microtime(true);
                 $cn = $this->connect(false, $only_master);
                 pg_set_client_encoding($cn, 'WIN1251');
                 $this->res = @pg_query($cn, $sto . $this->sql);
                 $tt = microtime(true) - $ts;
                 if (!$this->res) {
                     $tt = 0;
                     $this->err(pg_last_error($cn), $this->sql, $this->traceLevel, $pgcode);
                     if (!$i && ($pgcode == DB::PG_ERR_READ_ONLY || $pgcode == DB::PG_ERR_RECOVERY_CONFLICT)) {
                         $only_master = true;
                         continue;
                     }
                 }
                 if (isset($this->_stby_log_row)) {
                     $pfx = strpos($this->realAlias, DB::STBY_SUFFIX) ? 'standby_' : 'master_';
                     $this->_stby_log_row[$pfx . 'time'] += $tt;
                     $this->_stby_log_row[$pfx . 'cnt']++;
                     $this->_stby_log_row['ro_errors_cnt'] += (int) $only_master;
                 }
                 break;
             }
             if ($this->mode == 1 && $this->res !== FALSE) {
                 $this->oRes = $this->res;
                 $this->res = pg_fetch_all($this->res);
                 $this->mem->set($md5, $this->res, $this->ttl, $this->mgkey);
             }
             if ($this->mode == 2 && $this->res !== FALSE) {
                 $this->oRes = $this->res;
                 $this->res = pg_fetch_all($this->res);
                 if ($keys) {
                     $this->memcached->setMulti($keys);
                 }
                 $this->memcached->set($hash, array('ver' => $version, 'data' => $this->res), $this->ttl);
                 // stats temp
                 $stats[$line]['db'] = (int) $stats[$line]['db'] + 1;
                 $this->memcached->set(self::MPREFIX . 'S', $stats);
                 // stats temp
             }
         }
         $this->time = microtime(TRUE) - $this->time;
         if ($this->debug) {
             $c = count($this->debugLog);
             $trace = debug_backtrace();
             $this->debugLog[$c]['file'] = $this->debug;
             $this->debugLog[$c]['text'] .= "\n-- [" . date("Y-m-d H:i:s") . "] ";
             $this->debugLog[$c]['text'] .= "------------------------------------------------------------------\n";
             $this->debugLog[$c]['text'] .= "FILE: {$trace[$this->traceLevel - 1]['file']}\n";
             $this->debugLog[$c]['text'] .= "LINE: {$trace[$this->traceLevel - 1]['line']}\n";
             $this->debugLog[$c]['text'] .= "TIME: {$this->time} sec\n";
             $this->debugLog[$c]['text'] .= "MODE: {$this->mode}\n";
             $this->debugLog[$c]['text'] .= "SOURCE: " . ($exec ? "DB" : "CACHE") . "\n";
             if ($this->mode == 2) {
                 $this->debugLog[$c]['text'] .= "KEYS: " . var_export($this->keys, TRUE) . "\n";
             }
             $this->debugLog[$c]['text'] .= "RESULT: " . ($this->res !== FALSE ? "OK" : "ERROR") . "\n";
             $this->debugLog[$c]['text'] .= "{$this->sql}\n";
         }
         $this->keys = array();
         $this->traceLevel = 1;
         $this->mode = 0;
     } else {
         if ($this->mode == -1) {
             $this->sqls .= preg_replace("/;\\s*\$/", "", $this->sql) . "; ";
             $this->mode = 0;
             $this->res = TRUE;
         } else {
             if ($this->mode == -2) {
                 $this->mode = 0;
                 $this->res = $this->sql;
             }
         }
     }
     $this->_sqlType = 0;
     return $this->res;
 }
 /**
  * Store the data to memcache by id and group
  *
  * @access	public
  * @param	string	$id		The cache data id
  * @param	string	$group	The cache data group
  * @param	string	$data	The data to store in cache
  * @return	boolean	True on success, false otherwise
  * @since	1.5
  */
 function store($id, $group, $data)
 {
     $cache_id = $this->_getCacheId($id, $group);
     return $this->_db->set($cache_id, $data, $this->_compress, $this->_lifetime);
 }
Exemple #23
0
 /**
  * Stores data as key in Redis server.
  * @see cecilia\core.StorageAdapter::set()
  */
 public function set($key, $value)
 {
     return $this->_conn->set($this->prefix . $key, $value) ? true : false;
 }