/** * 可以获取Memcache的一个连接,并保持 * @author FM * @example LibMem\L($Id); * @param string $Id * @param bool $new_link 为TRUE时强制产生新的连接 * @param bool $persist_link 为TRUE时,获得持久化连接 * @param int $retryTimes 重连次数,默认不重连 * @param int $retry_interval 重试间隔秒数 * @internal 分布式的方法,采用Twemproxy * @return \Memcache 成功时返回Memcache对象,失败时返回FALSE */ function L($Id = 'default', $new_link = FALSE, $persist_link = FALSE, $retryTimes = 0, $retry_interval = 2) { $cfgId = $Id; static $_obj = array(); if ($persist_link) { $Id .= '|p'; } if (isset($_obj[$Id]) === FALSE) { $memConf = C('mchost_' . $cfgId); // 先获得$memId的conf if (empty($memConf) || !is_array($memConf)) { // 无配置错误 if (DEBUG) { throw new \Exception('no memconf ' . $cfgId . ' config.', 10002); } else { return FALSE; } } $_obj[$Id] = new \Memcache(); // host/port/timeout/ // timeout参数不需要 if ($persist_link) { $r = $_obj[$Id]->pconnect($memConf['host'][0], $memConf['host'][1]); } else { $r = $_obj[$Id]->connect($memConf['host'][0], $memConf['host'][1]); } // 如果第一次连接不成功,则重连 if (!$r) { $retry = 0; while ($retry < $retryTimes) { if ($persist_link) { $r = $_obj[$Id]->pconnect($memConf['host'][0], $memConf['host'][1]); } else { $r = $_obj[$Id]->connect($memConf['host'][0], $memConf['host'][1]); } if ($r) { break; } else { unset($_obj[$Id]); } // 重连,间隔2秒钟 $retry++; if ($retry_interval > 0) { sleep($retry_interval); } } } if (!$r || !isset($_obj[$Id]) || !$_obj[$Id]) { $code = 10002; $message = 'can not connect to memcached'; Log($code, $message, $memConf); if (DEBUG) { throw new \Exception($message, $code); } else { return FALSE; } } } return $_obj[$Id]; }
static function Dump($x, $message = NULL) { if (defined('LOG_VERBOSE')) { if ($message != NULL) { Log($message); } // Dump x ob_start(); var_dump($x); $contents = ob_get_contents(); ob_end_clean(); error_log($contents); if ($message != NULL) { Log("**************************************"); } } }
public function closeDay() { if ($cur_price = Stock::getCurPrice($this->sc_id)) { $this->day_close = $cur_price; $this->status = $this->day_open > $this->day_close ? ((double) $this->day_close / (double) $this->day_open - 1.0) * 100.0 : ((double) $this->day_close / (double) $this->day_open - 1.0) * 100.0; $database = new Database(); $query = "UPDATE " . self::$table; $query .= " SET day_close =" . $this->day_close . ", status=" . $this->status; $query .= " WHERE id=" . $this->id; echo $query; $run = $database->query($query); if ($run) { unset($database); return true; } else { Log("Couldnt execute StockHistory::closeday close manually for {$this->sc_id}", LOG_MANUAL); return false; } } else { Log("Couldnt execute StockHistory::closeday close manually for {$this->sc_id}", LOG_MANUAL); return false; } }
/** * 初始化函数 * @param string $name 连接名 * @param array $config 连接配置 * @throws Exception */ public function __construct($name, $config) { $host = $config['host']; $port = $config['port']; $db = isset($config['db']) ? $config['db'] : 0; $conn =& self::findServer($config); $this->db = $db; $this->name = $name; if (!$conn) { switch (strtolower($config['type'])) { case 'mysql': $user = $config['user']; $pwd = $config['pwd']; $timeout = 2; //秒 if (!empty($config['timeout'])) { $timeout = +$config['timeout']; } try { $conn = array('type' => $config['type'], 'host' => $host, 'port' => $port, 'db' => $db, 'name' => $name, 'ctime' => microtime(true), 'handle' => new PDO("mysql:host={$host};port={$port};dbname={$db};charset=utf8", "{$user}", "{$pwd}", array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC))); //php5.3.6以下版本会忽略DSN中的charset设定 这里手动指定字符集 $conn['handle']->query('set names utf8;'); //禁用prepared statements的仿真效果 防止SQL注入 $conn['handle']->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); Log("MysqlConnected: {$this->name} {$host}:{$port} {$db}"); self::$connections[] =& $conn; } catch (PDOException $e) { $message = $e->getMessage(); Log("MysqlError: {$message} at {$this->name} connect"); } break; default: //默认redis $conn = array('type' => $config['type'], 'host' => $host, 'port' => $port, 'db' => 0, 'name' => $name, 'handle' => new Redis()); $timeout = 0; if (!empty($config['timeout'])) { $timeout = +$config['timeout']; } try { $start_time = microtime(true); if (!$conn['handle']->connect($host, $port, $timeout)) { $took = microtime(true) - $start_time; throw new Exception("Connection failed, took {$took}s."); } $conn['ctime'] = microtime(true); Log("RedisConnected: {$this->name} {$host}:{$port}"); self::$connections[] =& $conn; } catch (Exception $e) { $message = $e->getMessage(); Log("RedisError: {$message} at {$this->name} connect"); } break; } } $this->connection =& $conn; }
<?php /** * Created by PhpStorm. * User: Seevali * Date: 2014-08-15 * Time: 09:33 */ define('_MEXEC', 'OK'); require_once "../../system/load.php"; $action = $_REQUEST['action']; switch ($action) { case "Log": Log(); break; } function Log() { }
/** * 可以获取Redis的一个连接,并保持 * @author FM * @example LibMy\L($Id); * @param string $Id * @param bool $new_link 为TRUE时强制产生新的连接 * @param bool $persist_link 为TRUE时,获得持久化连接 * @param int $retryTimes 重连次数,默认为0,不重连 * @param int $retry_interval 重连间隔时间,默认为2秒 * @internal 分布式的方法,采用Twemproxy * @return \Redis 成功时返回Redis对象,失败时返回FALSE */ function L($Id = 'default', $new_link = FALSE, $persist_link = FALSE, $options = array(), $retryTimes = 0, $retry_interval = 2) { $cfgId = $Id; static $_obj = array(); if ($persist_link) { $Id .= '|p'; } if (isset($_obj[$Id]) === FALSE || $new_link) { $rdConf = C('rdhost_' . $cfgId); // 先获得$rdId的conf if (empty($rdConf) || !is_array($rdConf)) { // 无配置错误 if (DEBUG) { throw new \Exception('no redis ' . $cfgId . ' config.', 10002); } else { return FALSE; } } $_obj[$Id] = new \Redis(); // host/port/timeout/reserved(should be NULL)/retry_interval // timeout/reserved/retry_interval 这几个参数基本无效 if ($persist_link) { $r = $_obj[$Id]->pconnect($rdConf['host'][0], $rdConf['host'][1]); } else { $r = $_obj[$Id]->connect($rdConf['host'][0], $rdConf['host'][1]); } if (!$r) { $retry = 0; while ($retry < $retryTimes) { $r = $_obj[$Id]->connect($rdConf['host'][0], $rdConf['host'][1]); if ($r) { break; } else { unset($_obj[$Id]); } // 重连,间隔2秒钟 $retry++; if ($retry_interval > 0) { sleep($retry_interval); } } } if (!$r || !isset($_obj[$Id]) || !$_obj[$Id]) { // 连接失败时,记录一条日志,报警监控 $code = 10003; $message = 'can not connect to redis'; Log($code, $message, $rdConf); if (DEBUG) { throw new \Exception($message, $code); } else { return FALSE; } } if ($options) { foreach ($options as $k => $v) { $_obj[$Id]->setOption($k, $v); } } } return $_obj[$Id]; }
FUNCTION CountStatBar($max, $value) { $res = 5; $der = 80; $size = 150; $width = Ceil($size*((Log($res + $der*$value/$max)-Log($res))/(Log($res+$der)-Log($res)))); RETURN $width; }