Exemple #1
0
 /**
  * 使用的缓存配置 默认为使用default_cache配置的参数
  *
  * @param bool|array $conf
  */
 public function __construct($conf = false)
 {
     $this->conf = $conf ? $conf : Config::get('default_cache');
     if (extension_loaded('Memcached')) {
         $this->memcache = new \Memcached('memcache_pool');
         $this->type = 1;
     } elseif (extension_loaded('Memcache')) {
         $this->memcache = new \Memcache();
         $this->type = 2;
     } else {
         \Foundation\throwException(Lang::get('_CACHE_EXTEND_NOT_INSTALL_', 'Memcached/Memcache'));
     }
     if (!$this->memcache) {
         \Foundation\throwException(Lang::get('_CACHE_NEW_INSTANCE_ERROR_', 'Memcache'));
     }
     if ($this->type == 2) {
         //memcache
         foreach ($this->conf['server'] as $val) {
             if (!$this->memcache->addServer($val['host'], $val['port'])) {
                 \Foundation\throwException(Lang::get('_CACHE_CONNECT_FAIL_', 'Memcache', $this->conf['host'] . ':' . $this->conf['port']));
             }
         }
         return;
     }
     if (md5(json_encode($this->conf['server'])) !== md5(json_encode($this->memcache->getServerList()))) {
         $this->memcache->quit();
         $this->memcache->resetServerList();
         $this->memcache->setOption(\Memcached::OPT_PREFIX_KEY, $this->conf['prefix']);
         \Memcached::HAVE_JSON && $this->memcache->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_JSON_ARRAY);
         if (!$this->memcache->addServers(array_values($this->conf['server']))) {
             \Foundation\throwException(Lang::get('_CACHE_CONNECT_FAIL_', 'Memcache', json_encode($this->conf['server'])));
         }
     }
 }
Exemple #2
0
 /**
  * URL组装 支持不同URL模式
  * eg: \Foundation\Http\Response::url('Home/Blog/cate/id/1')
  *
  * @param string $url URL表达式 路径/控制器/操作/参数1/参数1值/.....
  * @param int $echo 是否输出  1输出 0 return
  *
  * @return string
  */
 public static function url($url = '', $echo = 1)
 {
     $return = '';
     // 解析URL
     empty($url) && \Foundation\throwException(Lang::get('_ERROR_'));
     //'U方法参数出错'
     // URL组装
     $delimiter = Config::get('url_pathinfo_depr');
     $url = ltrim($url, '/');
     $url = implode($delimiter, explode('/', $url));
     if (Config::get('url_model') == 1) {
         $return = $_SERVER['SCRIPT_NAME'] . '/' . $url;
     } elseif (Config::get('url_model') == 2) {
         $return = Route::$urlParams['root'] . $url;
     } elseif (Config::get('url_model') == 3) {
         $return = $_SERVER['SCRIPT_NAME'] . '?' . Config::get('var_pathinfo') . '=/' . $url;
     }
     $return .= Config::get('url_model') == 2 ? Config::get('url_html_suffix') : '';
     $return = Secure::filterScript($return);
     if ($echo === 1) {
         echo $return;
     } else {
         return $return;
     }
     return '';
 }
Exemple #3
0
 /**
  * 使用的缓存配置 默认为使用default_cache配置的参数
  *
  * @param bool|array $conf
  */
 public function __construct($conf = false)
 {
     if (!function_exists('apc_cache_info')) {
         \Foundation\throwException(Lang::get('_CACHE_EXTENT_NOT_INSTALL_', 'Apc'));
     }
     $this->conf = $conf ? $conf : Config::get('default_cache');
 }
Exemple #4
0
 /**
  * where 语句组装工厂
  *
  * @param string $column 如 id  user.id (这边的user为表别名如表pre_user as user 这边用user而非带前缀的原表名)
  * @param array|int|string $value 值
  * @param string $operator 操作符
  * @throws \Exception
  */
 public function conditionFactory($column, $value, $operator = '=')
 {
     $currentOrIndex = isset($this->sql['where']['$or']) ? count($this->sql['where']['$or']) - 1 : 0;
     if ($this->opIsAnd) {
         isset($this->sql['where'][$column]) && \Foundation\throwException('Mongodb Where Op key Is Exists[' . $column . $operator . ']');
     } else {
         if ($this->bracketsIsOpen) {
             isset($this->sql['where']['$or'][$currentOrIndex][$column]) && \Foundation\throwException('Mongodb Where Op key Is Exists[' . $column . $operator . ']');
         }
     }
     if ($operator == 'IN' || $operator == 'NOT IN') {
         empty($value) && ($value = array(0));
         //这边可直接跳过不组装sql,但是为了给用户提示无条件 便于调试还是加上where field in(0)
         if ($this->opIsAnd) {
             $this->sql['where'][$column] = $operator == 'IN' ? array('$in' => $value) : array('$nin' => $value);
         } else {
             if ($this->bracketsIsOpen) {
                 $this->sql['where']['$or'][$currentOrIndex][$column] = $operator == 'IN' ? array('$in' => $value) : array('$nin' => $value);
             } else {
                 $this->sql['where']['$or'][][$column] = $operator == 'IN' ? array('$in' => $value) : array('$nin' => $value);
             }
         }
     } elseif ($operator == 'BETWEEN' || $operator == 'NOT BETWEEN') {
         $this->bindParams[] = $value[0];
         $this->bindParams[] = $value[1];
         if ($this->opIsAnd) {
             $this->sql['where'][$column] = $operator == 'BETWEEN' ? array('$gt' => $value[0], '$lt' => $value[1]) : array('$lt' => $value[0], '$gt' => $value[1]);
         } else {
             if ($this->bracketsIsOpen) {
                 $this->sql['where']['$or'][$currentOrIndex][$column] = $operator == 'BETWEEN' ? array('$gt' => $value[0], '$lt' => $value[1]) : array('$lt' => $value[0], '$gt' => $value[1]);
             } else {
                 $this->sql['where']['$or'][][$column] = $operator == 'BETWEEN' ? array('$gt' => $value[0], '$lt' => $value[1]) : array('$lt' => $value[0], '$gt' => $value[1]);
             }
         }
     } else {
         if ($operator == 'IS NULL' || $operator == 'IS NOT NULL') {
             if ($this->opIsAnd) {
                 $this->sql['where'][$column] = $operator == 'IS NULL' ? array('$in' => array(null), '$exists' => true) : array('$ne' => null, '$exists' => true);
             } else {
                 if ($this->bracketsIsOpen) {
                     $this->sql['where']['$or'][$currentOrIndex][$column] = $operator == 'IS NULL' ? array('$in' => array(null), '$exists' => true) : array('$ne' => null, '$exists' => true);
                 } else {
                     $this->sql['where']['$or'][][$column] = $operator == 'IS NULL' ? array('$in' => array(null), '$exists' => true) : array('$ne' => null, '$exists' => true);
                 }
             }
         } else {
             if ($operator == '>' || $operator == '<') {
                 if ($this->opIsAnd) {
                     $this->sql['where'][$column] = $operator == '>' ? array('$gt' => $value) : array('$lt' => $value);
                 } else {
                     if ($this->bracketsIsOpen) {
                         $this->sql['where']['$or'][$currentOrIndex][$column] = $operator == '>' ? array('$gt' => $value) : array('$lt' => $value);
                     } else {
                         $this->sql['where']['$or'][][$column] = $operator == '>' ? array('$gt' => $value) : array('$lt' => $value);
                     }
                 }
             } else {
                 if ($operator == '>=' || $operator == '<=') {
                     if ($this->opIsAnd) {
                         $this->sql['where'][$column] = $operator == '>=' ? array('$gte' => $value) : array('$lte' => $value);
                     } else {
                         if ($this->bracketsIsOpen) {
                             $this->sql['where']['$or'][$currentOrIndex][$column] = $operator == '>=' ? array('$gte' => $value) : array('$lte' => $value);
                         } else {
                             $this->sql['where']['$or'][][$column] = $operator == '>=' ? array('$gte' => $value) : array('$lte' => $value);
                         }
                     }
                 } else {
                     if ($operator == 'LIKE' || $operator == 'NOT LIKE' || $operator == 'REGEXP') {
                         if ($this->opIsAnd) {
                             $this->sql['where'][$column] = '/' . $value . '/i';
                         } else {
                             if ($this->bracketsIsOpen) {
                                 $this->sql['where']['$or'][$currentOrIndex][$column] = '/' . $value . '/i';
                             } else {
                                 $this->sql['where']['$or'][][$column] = '/' . $value . '/i';
                             }
                         }
                     } else {
                         if ($operator == '!=') {
                             if ($this->opIsAnd) {
                                 $this->sql['where'][$column] = array('$ne' => $value);
                             } else {
                                 if ($this->bracketsIsOpen) {
                                     $this->sql['where']['$or'][$currentOrIndex][$column] = array('$ne' => $value);
                                 } else {
                                     $this->sql['where']['$or'][][$column] = array('$ne' => $value);
                                 }
                             }
                         } else {
                             if ($this->opIsAnd) {
                                 $this->sql['where'][$column] = $value;
                             } else {
                                 if ($this->bracketsIsOpen) {
                                     $this->sql['where']['$or'][$currentOrIndex][$column] = $value;
                                 } else {
                                     $this->sql['where']['$or'][][$column] = $value;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Exemple #5
0
 /**
  * SQL语句条件组装
  *
  * @param string $key eg: 'forum-fid-1-uid-2'
  * @param bool $and 多个条件之间是否为and  true为and false为or
  * @param bool $noCondition 是否为无条件操作  set/delete/update操作的时候 condition为空是正常的不报异常
  * @param bool $noTable 是否可以没有数据表 当delete/update等操作的时候已经执行了table() table为空是正常的
  *
  * @return array eg: array('forum', "`fid` = '1' AND `uid` = '2'")
  */
 protected function parseKey($key, $and = true, $noCondition = false, $noTable = false)
 {
     $condition = '';
     $arr = explode('-', $key);
     $len = count($arr);
     for ($i = 1; $i < $len; $i += 2) {
         isset($arr[$i + 1]) && ($condition .= ($condition ? $and ? ' AND ' : ' OR ' : '') . "`{$arr[$i]}` = %s");
         $this->bindParams[] = $arr[$i + 1];
     }
     $table = strtolower($arr[0]);
     empty($table) && !$noTable && \Foundation\throwException(Lang::get('_DB_PARAM_ERROR_PARSE_KEY_', $key, 'table'));
     empty($condition) && !$noCondition && \Foundation\throwException(Lang::get('_DB_PARAM_ERROR_PARSE_KEY_', $key, 'condition'));
     empty($condition) || ($condition = "({$condition})");
     return array($table, $condition);
 }
Exemple #6
0
 /**
  * 清洗已经存储的所有元素
  *
  */
 public function truncate()
 {
     foreach ($this->conf['server'] as $key => $val) {
         if (!isset($this->redis[$key]) || !is_object($this->redis[$key])) {
             $instance = new \Redis();
             if ($instance->pconnect($val['host'], $val['port'], 1.5)) {
                 $this->redis[$key] = $instance;
             } else {
                 \Foundation\throwException(Lang::get('_CACHE_NEW_INSTANCE_ERROR_', 'Redis'));
             }
         }
         $this->redis[$key]->flushDB();
     }
     return true;
 }
Exemple #7
0
 /**
  * 执行预处理语句
  *
  * @param object $stmt PDOStatement
  * @param bool $clearBindParams
  *
  * @return bool
  */
 private function execute($stmt, $clearBindParams = true)
 {
     //empty($param) && $param = $this->bindParams;
     $clearBindParams && ($this->bindParams = array());
     if (!$stmt->execute()) {
         $error = $stmt->errorInfo();
         \Foundation\throwException($error[2]);
     }
     return true;
 }