GO语言基础-hello-world

程序

package main

import "fmt"

func main() {
    fmt.Printf("Hello, world \n")
}

简单说明

首先我们要了解一个概念,Go程序是通过package来组织的

package <name> 这一行告诉我们当前文件属于哪个包,而包名main则告诉我们它是一个可独立运行的包,它在编译后会产生可执行文件。
除了main包之外,其它的包最后都会生成*.a文件(也就是包文件)并放置在$GOPATH/pkg/$GOOS_$GOARCH中。
每一个可独立运行的Go程序,必定包含一个package main,在这个main包中必定包含一个入口函数main,而这个函数既没有参数,也没有返回值。

为了打印Hello, world…,我们调用了一个函数Printf,这个函数来自于fmt包,所以我们在程序中导入了系统级别的fmt包:import “fmt”。

我们通过关键字func定义了一个main函数,函数体被放在{}(大括号)中,就像我们平时写C、C++或Java时一样。

column and line shapes
snooki weight loss The Rolex Explorer II Vs the Rolex Gmt

information of an irresistible social film sweepstakes marketing
quick weight lossHow to Winterize a Radiant Heat System
free hd porn
发表在 网站开发 | 标签为 | 评论关闭

我的第一个开源小项目

我的第一个开源小项目,半成品。

欢迎大家拍板。访问地址:httpserver

Many little girls love to dress up in their mother’s clothing
gay porn How to Make a Cotton Hoodie Last Longer

95 get me every time I walk into that place
quick weight lossFile Sharing Service RapidShare Changes Traffic Model to Deter Piracy
free porn sites
发表在 个人感悟, 好文推荐 | 一条评论

php利用socket扩展写一个简单的多进程并发http服务3

接着上文说

上一篇文章只是实现了其并发功能,前一个请求的处理不会阻碍下一个请求的处理,但是是每次请求的时候产生一个新的进程,也不是很好,并且并发的效果并不好,因为只有一个进程在工作。

这次的改进是,将生成多个工作进程,提高并发性能。

贴代码:

#!/usr/local/php5.3/bin/php
<?php

//多进程--预先生成固定进程的数量来处理请求。
error_reporting(0);

set_time_limit(0);
ob_implicit_flush();
date_default_timezone_set("Asia/Shanghai"); 


$debug = false;

$user='';
$worker_processes=0;
$listen_ip = "127.0.0.1";
$listen_port  = 80;
$vhosti = array();

//解析配置文件
function pare_conf_file(){
	global $user,$worker_processes,$listen_ip,$listen_port,$vhost;
	$ini_array = parse_ini_file("./http.ini", true);
	$user=$ini_array['user'];
	$worker_processes=$ini_array['worker_processes'];
	$listen_ip = $ini_array['listen_ip'];
	$listen_port = $ini_array['listen_port'];
	$vhost = $ini_array['vhost'];
}

pare_conf_file();

//不存在的http方法响应
function response_method_404(){
	$result = "";
	$result .="HTTP/1.1 404\r\n";
	$result .="Content-Length: 14\r\n";
	$result .= "Content-Type: text/html\r\n";
	$result .="\r\nserver is not \r\n";
	return $result;
}

//不存在的文件响应
function response_file_404(){
	$result = "";
	$result .="HTTP/1.1 404\r\n";
	$result .="Content-Length:0\r\n";
	$result .= "Content-Type: text/html\r\n";
	$result .="\r\n\r\n";
	return $result;
}

//不支持文件的类型响应
function response_file_type_404(){
	$result = "";
	$result .="HTTP/1.1 404\r\n";
	$result .="Content-Length:11\r\n";
	$result .= "Content-Type: text/html\r\n";
	$result .="\r\ntype is not\r\n";
	return $result;
}

//图片类型响应
function response_file_img($fileurl){
	$type = mime_content_type($fileurl);
	$body = file_get_contents($fileurl);
	$len = strlen($body);
	$result="";
	$result .="HTTP/1.1 200\r\n";
	$result .="Content-Length:$len\r\n";
	$result .= "Content-Type: {$type}\r\n";
	$result .="\r\n$body\r\n";
	return $result;
}

//JS类型响应
function response_file_js($fileurl){
	global $request;
	$body = file_get_contents($fileurl);
	$len = strlen($body);
	$date = date('D, d M Y G:i:s ').'GMT';
	clearstatcache();
	$last_modified = date('D, d M Y G:i:s ',filemtime($fileurl)).'GMT';
	$result="";

	$result .="HTTP/1.1 200\r\n";
	$result .="Cache-Control: public\r\n";
	$result .="Date:$date\r\n";
	$result .="Last-Modified:$last_modified\r\n";
	$result .="Expires:Fri, 24 Jan 2016 10:06:05 GMT\r\n";
	$result .="Content-Length:$len\r\n";
	$result .= "Content-Type: application/x-javascript\r\n";
	$result .="\r\n$body\r\n";

	return $result;
}

//HTML类型响应
function response_file_html($fileurl){
	$body = file_get_contents($fileurl);
	$len = strlen($body);
	$result = "";
	$result .="HTTP/1.1 200\r\n";
	$result .="Content-Length:$len\r\n";
	$result .= "Content-Type: text/html;charset=utf-8\r\n";
	$result .="\r\n$body\r\n";
	return $result;
}

//解析http请求
function pare_request($str){
	clearstatcache();
	global $web_root,$request;
	$arr = explode("\r\n",$str);
	foreach( $arr as $r){
		$pos = stripos($r,":");
		if ( $pos ){
			$key = trim(substr($r,0,$pos));
			$val = trim(substr($r,$pos+1));
			$request[$key] = $val;
		}

	}
	writelog(var_export($request,true));	
	$line0 = explode(" ",$arr[0]);
	if ( $line0[0] == "GET" ){
		$fileurl = $web_root.$line0[1];
		if ( file_exists($fileurl) ){
			$pathinfo = pathinfo($fileurl);
			if ( $pathinfo['extension'] == "js" ){
				$result = response_file_js($fileurl);
			}else if ( $pathinfo['extension'] == "png" || $pathinfo['extension'] == "jpg" || $pathinfo['extension'] == "gif" ){
				$result = response_file_img($fileurl);
			}else if ( $pathinfo['extension'] == "html" ){
				$result = response_file_html($fileurl);
			}else{
				$result = response_file_type_404();
			}
		}else{
			$result = response_file_404();
		}
	}else{
		$result = response_method_404();
	}
	return $result;
}

//信号处理
function sig_handler($signo) 
{

	switch ($signo) {
		case SIGTERM:// 处理中断信号
			echo "SIGTERM\r\n";
			exit;
			break;
		case SIGHUP:
			// 处理重启信号
			echo "SIGHUP\r\n";
			exit;
			break;
		default:
			echo $signo."DEFAULT\r\n";
			exit;
			// 处理所有其他信号
	}

}

//安装信号处理器
function register_sig_handler(){
	pcntl_signal(SIGTERM, "sig_handler");
	pcntl_signal(SIGHUP, "sig_handler");
	pcntl_signal(IGCHLD, "sig_handler" );
}

//日志
function writelog($msg){
	global $debug;
	if ( $debug ){
		echo $msg;
	}else{
		file_put_contents('./log.txt',$msg,FILE_APPEND | LOCK_EX);
	}
}

//切换用户
if ( $user ){
	$user_info = posix_getpwnam($user);
	$uid = $user_info['uid'];
}else{
	$uid = posix_getuid();
}
posix_setuid($uid);

//调试模式判断
if ( !$debug ){
	//产生子进程分支
	$pid = pcntl_fork();
	if ($pid == -1) {
		writelog("could not fork\r\n");
		die("could not fork"); //pcntl_fork返回-1标明创建子进程失败
	} else if ($pid) {
		exit(); //父进程中pcntl_fork返回创建的子进程进程号
	} else {
		// 子进程pcntl_fork返回的时0
	}

	// 从当前终端分离
	if (posix_setsid() == -1) {
		writelog("could not detach form terminal \r\n");
		die("could not detach from terminal");
	}

	register_sig_handler();

}


$address = '127.0.0.1';
$port = 1215;
$web_root = "/home/xtgxiso/phpsource/socket";
$request = array();
$sock = false;

//创建监听socket
function listen_server_socket(){
	global $sock,$address,$port;
	if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
		writelog("socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n");
		exit;
	}

	socket_set_nonblock($sock);


	if (socket_bind($sock, $address, $port) === false) {
		writelog("socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n");
		exit;
	}else{
		writelog('Socket ' . $address . ':' . $port . " has been opened\n");
	}

	if (socket_listen($sock, 5) === false) {
		writelog("socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n");
		exit;
	}else{
		writelog("Listening for new clients..\n");
	}
}


listen_server_socket();

//创建工作进程
function start_work_process(){
	global $sock,$client_id;
	$pid = pcntl_fork();
	if ( $pid == 0 ){
		do{
			if ( ($msgsock = socket_accept($sock)) === false ) {
				writelog("socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n");
				usleep(5000);
				continue;
			} else {
				$client_id += 1;
				writelog(date('Y-m-d G:i:s')."--Client #" .$client_id .": Connect\n");
			}
			socket_close($sock);
			$cur_buf = '';
			do {
				if (false === ($buf = socket_read($msgsock, 2048))) {
					writelog("socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n");
					break;
				}
				writelog("read start:\r\n");
				writelog($buf);
				writelog("read end:\r\n\r\n");
				$talkback = pare_request($buf);
				writelog("\r\nwrite start:\r\n");
				socket_write($msgsock, $talkback, strlen($talkback));
				//writelog($talkback);
				writelog("write end:\r\n\r\n\r\n");
				break;
			} while (true);
			socket_close($msgsock);
		}while(true);
	}else if ( $pid > 0 ){
		socket_close($sock);
	}

}


//创建工作进程并处理信号
$client_id = 0;

for( $i = 1; $i <= $worker_processes;$i++){
	start_work_process();
}

while(1){
	sleep(1);
}

writelog("socket_close \r\n");
socket_close($sock);

这样就可以提前生成比如:10个进程。一下子能力提高了10倍 。这个程序并没有将工作进程中的每个请求创建新的进程来处理(性能不太好),要是能够用线程来解决就好了。

The point of the bill is so that by 2020
christina aguilera weight loss Top 7 Formulas for Writing Articles That Get Read

negative aspects at dress for youths
miranda lambert weight lossAdvice on What to Wear at a 70s Party
gay porn
发表在 好文推荐, 网站开发, 网站架构 | 标签为 , | 一条评论

php利用socket扩展写一个简单的单进程并发http服务2

接着上文说

上一篇文章只是实现了其最基本的功能,由于是顺序执行的。一次只能处理一个请求,如果多个的话,肯定会有很严重的问题。这次实现了并发。每次请求有一个新的进程来处理,同时继续接受新的请求。

贴代码:

#!/usr/local/php5.3/bin/php
<?php
	//单进程并发执行	
	error_reporting(E_ALL);
	set_time_limit(0);
	ob_implicit_flush();
	date_default_timezone_set("Asia/Shanghai"); 


	$debug = true;
	
	$user='';
	$worker_processes=0;
	$listen_ip = "127.0.0.1";
	$listen_port  = 80;
	$vhosti = array();
        
        //解析配置文件
	function pare_conf_file(){
		global $user,$worker_processes,$listen_ip,$listen_port,$vhost;
		$ini_array = parse_ini_file("./http.ini", true);
		$user=$ini_array['user'];
		$worker_processes=$ini_array['worker_processes'];
		$listen_ip = $ini_array['listen_ip'];
		$listen_port = $ini_array['listen_port'];
		$vhost = $ini_array['vhost'];
	}

	pare_conf_file();

	function response_method_404(){
		$result = "";
		$result .="HTTP/1.1 404\r\n";
		$result .="Content-Length: 14\r\n";
		$result .= "Content-Type: text/html\r\n";
		$result .="\r\nserver is not \r\n";
		return $result;
	}
	
	function response_file_404(){
		$result = "";
		$result .="HTTP/1.1 404\r\n";
		$result .="Content-Length:0\r\n";
		$result .= "Content-Type: text/html\r\n";
		$result .="\r\n\r\n";
		return $result;
	}
	
	function response_file_type_404(){
		$result = "";
		$result .="HTTP/1.1 404\r\n";
		$result .="Content-Length:11\r\n";
		$result .= "Content-Type: text/html\r\n";
		$result .="\r\ntype is not\r\n";
		return $result;
	}
	
	function response_file_img($fileurl){
		$type = mime_content_type($fileurl);
		$body = file_get_contents($fileurl);
		$len = strlen($body);
		$result="";
		$result .="HTTP/1.1 200\r\n";
		$result .="Content-Length:$len\r\n";
		$result .= "Content-Type: {$type}\r\n";
		$result .="\r\n$body\r\n";
		return $result;
	}
	
	
	function response_file_js($fileurl){
		global $request;
		$body = file_get_contents($fileurl);
		$len = strlen($body);
		$date = date('D, d M Y G:i:s ').'GMT';
		clearstatcache();
		$last_modified = date('D, d M Y G:i:s ',filemtime($fileurl)).'GMT';
		$result="";

		$result .="HTTP/1.1 200\r\n";
		$result .="Cache-Control: public\r\n";
		$result .="Date:$date\r\n";
		$result .="Last-Modified:$last_modified\r\n";
		$result .="Expires:Fri, 24 Jan 2016 10:06:05 GMT\r\n";
		$result .="Content-Length:$len\r\n";
		$result .= "Content-Type: application/x-javascript\r\n";
		$result .="\r\n$body\r\n";
	
		return $result;
	}
	
	
	function response_file_html($fileurl){
		$body = file_get_contents($fileurl);
		$len = strlen($body);
		$result = "";
		$result .="HTTP/1.1 200\r\n";
		$result .="Content-Length:$len\r\n";
		$result .= "Content-Type: text/html;charset=utf-8\r\n";
		$result .="\r\n$body\r\n";
		return $result;
	}
	
	function pare_request($str){
		clearstatcache();
		global $web_root,$request;
		$arr = explode("\r\n",$str);
		foreach( $arr as $r){
			$pos = stripos($r,":");
			if ( $pos ){
				$key = trim(substr($r,0,$pos));
				$val = trim(substr($r,$pos+1));
				$request[$key] = $val;
			}

		}
		writelog(var_export($request,true));	
		$line0 = explode(" ",$arr[0]);
		if ( $line0[0] == "GET" ){
			$fileurl = $web_root.$line0[1];
			if ( file_exists($fileurl) ){
				$pathinfo = pathinfo($fileurl);
				if ( $pathinfo['extension'] == "js" ){
					$result = response_file_js($fileurl);
				}else if ( $pathinfo['extension'] == "png" || $pathinfo['extension'] == "jpg" || $pathinfo['extension'] == "gif" ){
					$result = response_file_img($fileurl);
				}else if ( $pathinfo['extension'] == "html" ){
					$result = response_file_html($fileurl);
				}else{
					$result = response_file_type_404();
				}
			}else{
				$result = response_file_404();
			}
		}else{
			$result = response_method_404();
		}
		return $result;
	}
	
	function sig_handler($signo) 
	{

	     switch ($signo) {
			 case SIGTERM:// 处理中断信号
				echo "SIGTERM\r\n";
			    exit;
			    break;
			 case SIGHUP:
			    // 处理重启信号
				echo "SIGHUP\r\n";
				exit;
			    break;
			default:
				echo "DEFAULT\r\n";
				exit;
			    // 处理所有其他信号
			}

	}


	function writelog($msg){
		global $debug;
		if ( $debug ){
			echo $msg;
		}else{
			file_put_contents('./log.txt',$msg,FILE_APPEND | LOCK_EX);
		}
	}
	
	if ( $user ){
	    $user_info = posix_getpwnam($user);
	    $uid = $user_info['uid'];
	}else{
	    $uid = posix_getuid();
	}
	posix_setuid($uid);
	
	
	if ( !$debug ){
		//产生子进程分支
		$pid = pcntl_fork();
		if ($pid == -1) {
			 writelog("could not fork\r\n");
			die("could not fork"); //pcntl_fork返回-1标明创建子进程失败
		} else if ($pid) {
			 exit(); //父进程中pcntl_fork返回创建的子进程进程号
		} else {
			 // 子进程pcntl_fork返回的时0
		}

		// 从当前终端分离
		if (posix_setsid() == -1) {
			writelog("could not detach form terminal \r\n");
			die("could not detach from terminal");
		}
		

		// 安装信号处理器
		pcntl_signal(SIGTERM, "sig_handler");
		pcntl_signal(SIGHUP, "sig_handler");

	}
	
	
	$address = '127.0.0.1';
	$port = 1215;
	$web_root = "/home/xtgxiso/phpsource/socket";
	$request = array();

	if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
		writelog("socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n");
		exit;
	}
	
	if (socket_bind($sock, $address, $port) === false) {
	    writelog("socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n");
		exit;
	}else{
		writelog('Socket ' . $address . ':' . $port . " has been opened\n");
	}
	
	if (socket_listen($sock, 5) === false) {
	    writelog("socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n");
		exit;
	}else{
		writelog("Listening for new clients..\n");
	}
	
	$client_id = 0;

	do{
		
		if ( ($msgsock = socket_accept($sock)) === false ) {
	        writelog("socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n");
	        break;
	    } else {
		    $client_id += 1;
			writelog(date('Y-m-d G:i:s')."--Client #" .$client_id .": Connect\n");
		}
		
		$ppid = pcntl_fork();
		if ( $ppid == 0 ){
			socket_close($sock);
			$cur_buf = '';
			sleep(5);
			do {
				if (false === ($buf = socket_read($msgsock, 2048))) {
					writelog("socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n");
					break;
				}
				writelog("read start:\r\n");
				writelog($buf);
				writelog("read end:\r\n\r\n");
				$talkback = pare_request($buf);
				writelog("\r\nwrite start:\r\n");
				socket_write($msgsock, $talkback, strlen($talkback));
				//writelog($talkback);
				writelog("write end:\r\n\r\n\r\n");
				break;
			} while (true);
			socket_close($msgsock);
			exit(0);
		}else if ( $ppid > 0 ){
			socket_close($msgsock);
		}

	}while(true);

	writelog("socket_close \r\n");
	socket_close($sock);

支持配置文件,格式如下:

;user
user=roo
worker_processes=2

又进步了一点点,哈哈

继续阅读

发表在 好文推荐, 网站开发 | 标签为 , | 2 条评论

php不常用但有用方法总结

setproctitle("myscript")         //设置当前进程标题  setthreadtitle("myscript");     //设置当前线程标题 

mime_content_type('1.jpg')     //返回mime_type image/jpeg

parse_ini_file("sample.ini");     //解析一个ini文件并返回数组

parse_ini_string()                   //解析一个ini字符串并返回数组

iconv("gbk", "UTF-8", $text)          //$tetxt变量gbk转换为utf8

imageinterlace('1.jpg',1)             //图像被创建为渐进式 JPEG 

imagealphablending( resource $image , bool $blendmode ) //允许在真彩色图像上使用两种不同的绘画模式,在加水印的时候png不透明有用

time_sleep_until( float $timestamp )    //使脚本睡眠到指定的时间为止


Jennifer Lopez went the futuristic route
miranda lambert weight loss Usefulness of Wrought Iron In Stair Rails

purses and other accessories by visiting their website
weight loss tipsHow to Take Accredited Online Courses
youjizz
发表在 网站开发 | 一条评论

php利用socket开发一个memcached的客户端api

由于个人电脑的原因,memcache.dll无法使用。这样我就无法操作memcached服务了。

思来想去,就自己写一个API吧。顺便也了解一下memcached协议。

贴代码

<?php

/**
 * 
 * memcached连接客户端
 * 
 */
 
class my_memcache{
	
	
	protected $socket = false;
	protected $server_list = array();
	protected $host = false;
	protected $port = false;
	
	protected $threshold = 0;
	protected $min_saving = 0;
	
	
	function __construct() {
		$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
   	}

	
	function __destruct() {
    	socket_close($this->socket);
	}

	
   /**
    * 完成 
	*/
	
	public function add($key,$var,$flag=0,$expire=0){
		$this->selectconnect($key);
		if ( is_array($var) ){
			$var = serialize($var);
		}
		$len = strlen($var);
		if ( $len > $this->threshold && $this->threshold > 0 ){
			$flag=1;
			$var = $this->gz_ungz_compress($var,1);
			$len = strlen($var);
		}
		$in = "add $key $flag $expire $len\r\n";
		$in .= $var."\r\n";
		socket_write($this->socket, $in, strlen($in));
		$out = socket_read($this->socket,1024);
		$out = trim(str_replace("\r\n","",$out));
		if ( strtoupper($out) == "STORED" ){
			return true;
		}else{
			return false;	
		}
	}
	
   /**
    * 完成 
	*/
	
	public function addServer($host,$port,$persistent=true,$weight=1,$timeout=1,$retry_interval=15,$status=1,$failure_callback = NULL ,$timeoutms = 0){
		if ( empty($host) || empty($port) ){
			return false;
		}
		$this->server_list[] = array('host' => $host , 'port' => $port , 'persistent' => true, 'weight' => $weight , 'timeout' => $timeout , 'retry_interval' => $retry_interval , 'status' => $status , 'failure_callback' => $failure_callback );
	}
	
   /**
    * 完成 
	*/
	public function close(){
		return socket_close($this->socket);
	}
	
   /**
    * 完成 
	*/
	public function connect($host,$port=11211,$timeout=1){
		$this->host = $host;
		$this->port = $port;
		return socket_connect($this->socket,$host,$port);
	}
	
	
   /**
    * 完成 
	*/
	protected function selectconnect($key){
		if ( empty($key) || empty($this->server_list) ){
			return false;
		}
		$len = count($this->server_list)-1;
		$first = ord(substr($key,0,1));
		$key = $first%$len;
		$server = $this->server_list[$key];
		if ( $server['persistent'] ){
			socket_get_option ($this->socket,1,SO_KEEPALIVE);
		}
		return socket_connect($this->socket,$server['host'],$server['port']);
	}
	
	
	
   /**
    * 完成 
	*/
	public function decrement($key,$value=1){
		if ( empty($key) ){
			return false;
		} 
		$val = intval($value);
		$this->selectconnect($key);
		$in = "decr $key $val\r\n";
		socket_write($this->socket, $in, strlen($in));
		$out = socket_read($this->socket,1024);
		$out = trim(str_replace("\r\n","",$out));
		return $out;
	}
	
   /**
    * 完成 
	*/
	public function delete($key,$timeout =0){
		$this->selectconnect($key);
		$in = "delete $key\r\n";
		socket_write($this->socket, $in, strlen($in));
		$out = socket_read($this->socket,1024);
		$out = trim(str_replace("\r\n","",$out));
		if ( $out == 'DELETED' ){
			return true;
		}else if ( $out == 'NOT_FOUND' ){
			return false;
		}else{
			return false;
		}
	}

	
   /**
    * 完成 
	*/
	public function get($key,$flag=0){
		if ( empty($key) ){
			return false;
		}
		$this->selectconnect($key);
		$in = "get $key $flag\r\n";
		socket_write($this->socket, $in, strlen($in));
		$out = socket_read($this->socket,90240);
		$out_array = explode("\r\n",$out);
		$l1 = $out_array[0];
		$l1_array = explode(" ",$l1);
		$content = str_replace(array("\r","\n"),'',$out_array[1]);
		if ( $l1_array[2] == 1 ){
			$content = $this->gz_ungz_compress($content,2);
		}
		if ( empty($content) ){
			return false;	
		}else{
			return $content;
		}
		
	}
	
	/**
    * 完成 
	*/
	public function getServerStatus($host,$port=11211){
		if ( empty($host) ){
			return false;
		}
		$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
		if ( socket_connect($socket,$host,$port) ){
			socket_close($socket);
			return 1;
		}else{
			return 0;
		}
	}
	
	
	/**
    * 完成 
	*/
	public function increment($key,$value=1){
		if ( empty($key) ){
			return false;
		} 
		$val = intval($value);
		$this->selectconnect($key);
		$in = "incr $key $val\r\n";
		socket_write($this->socket, $in, strlen($in));
		$out = socket_read($this->socket,1024);
		$out = trim(str_replace("\r\n","",$out));
		return $out;
	}
	
	/**
    * 完成 
	*/
	public function pconnect($host,$port=11211,$timeout=1){
		
		if ( !socket_set_option($this->socket, SOL_SOCKET, SO_KEEPALIVE, 1) ) {
			return false;
		}
		
		return socket_connect($this->socket,$host,$port);
		
	}
	
	/**
    * 完成 
	*/
	public function replace($key,$var,$flag=0,$expire=0){
		$len = strlen($var);
		if ( $len > $this->threshold && $this->threshold > 0 ){
			$flag=1;
			$var = $this->gz_ungz_compress($var,1);
			$len = strlen($var);
		}
		$in = "replace $key $flag $expire $len\r\n";
		$in .= $var."\r\n";
		socket_write($this->socket, $in, strlen($in));
		$out = socket_read($this->socket,1024);
		$out = trim(str_replace("\r\n","",$out));
		if ( strtoupper($out) == "STORED" ){
			return true;
		}else{
			return false;	
		}
	}
	
	
   /**
    * 完成 
	*/
	public function set($key,$var,$flag=0,$expire=0){
		$this->selectconnect($key);
		if ( is_array($var) ){
			$var = serialize($var);
		}
		$len = strlen($var);
		if ( $len > $this->threshold && $this->threshold > 0 ){
			$flag=1;
			$var = $this->gz_ungz_compress($var,1);
			$len = strlen($var);
		}
		$in = "set $key $flag $expire $len\r\n";
		$in .= $var."\r\n";
		socket_write($this->socket, $in, strlen($in));
		$out = socket_read($this->socket,1024);
		$out = trim(str_replace("\r\n","",$out));
		if ( strtoupper($out) == "STORED" ){
			return true;
		}else{
			return false;	
		}
	}
	
	
	/**
    * 完成 
	*/
	public function getVersion(){
		$result = array();
		if ( $this->server_list ){
			foreach ( $this->server_list as $s ) {
				$key = $s['host'].":".$s['port'];
				$in = "version\r\n";
				$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
				socket_connect($socket,$s['host'],$s['port']);
				socket_write($socket, $in, strlen($in));
				$out = socket_read($socket,1024);
				$out = str_replace(array("\r","\n"),"",$out);
				$out = str_replace("VERSION ","",$out);
				$result[$key]= $out;
			}
			return $result;
		}else{
			$in = "version\r\n";
			socket_write($this->socket, $in, strlen($in));
			$out = socket_read($this->socket,1024);
			$out = str_replace(array("\r","\n"),"",$out);
			$out = str_replace("VERSION ","",$out);
			$key = $this->host.":".$this->port;
			$result[$key]= $out;
			return $result;
		}
		
	}
	
	
	/**
    * 完成 
	*/
	public function flush(){
		$result = array();
		if ( $this->server_list ){
			foreach ( $this->server_list as $s ) {
				$key = $s['host'].":".$s['port'];
				$in = "flush_all\r\n";
				$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
				socket_connect($socket,$s['host'],$s['port']);
				socket_write($socket, $in, strlen($in));
				$out = socket_read($socket,1024);
				$out = str_replace(array("\r","\n"),"",$out);
				if ( $out == "OK" ){
					$result[$key]= true;
				}else{
					$result[$key]= false;
				}
			}
			return $result;
		}else{
			$in = "flush_all\r\n";
			socket_write($this->socket, $in, strlen($in));
			$out = socket_read($this->socket,1024);
			$out = str_replace(array("\r","\n"),"",$out);
			$key = $this->host.":".$this->port;
			if ( $out == "OK" ){
				$result[$key]= true;
			}else{
				$result[$key]= false;
			}
			
			return $result;
		}
	}
	
	/**
    * 完成 
	*/	
	public function getStats($type=0,$slabid=0,$limit=0){
		$this->getExtendedStats($type,$slabid,$limit);
	}
	
	
	/**
    *  stats reset 							清空统计数据=>返回reset
    *  stats slabs							显示各个slab的信息,包括chunk的大小、数目、使用情况等
	*  stats cachedump slab_id limit_num	显示某个slab中的前limit_num个key列表
	*  stats items
	*  stats detail [on|off|dump]			设置或者显示详细操作记录
	*  stats malloc 						显示内存分配数据    
	*/	
	public function getExtendedStats($type=0,$slabid=0,$limit=0){
		$type_list = array('reset','malloc','maps','cachedump','slabs','items','sizes');
		if ( $type ){
			if ( !in_array($type,$type_list) ){
				$type = '';
			}
		}
		$slabid = intval($slabid);
		$limit = intval($limit);
		$result = array();
		if ( $this->server_list ){
			foreach ( $this->server_list as $s ) {
				$key = $s['host'].":".$s['port'];
				$in = "stats\r\n";
				$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
				socket_connect($socket,$s['host'],$s['port']);
				socket_write($socket, $in, strlen($in));
				$out = socket_read($socket,10240);
				$out = explode("\r\n",$out);
				foreach ($out as $s) {
					if ( $s ){
						$temp = explode(" ",$s);
						if ( count($temp) > 2 ){
							$result[$key][$temp[1]] = $temp[2];
						}
					}
				} 
			}
			return $result;
		}else{
			$in = "stats\r\n";
			socket_write($this->socket, $in, strlen($in));
			$out = socket_read($this->socket,10240);
			if ( empty($out) ){
				return false;	
			}else{
				$key = $this->host.":".$this->port;
				$out = explode("\r\n",$out);
				foreach ($out as $s) {
					if ( $s ){
						$temp = explode(" ",$s);
						if ( count($temp) > 2 ){
							$result[$key][$temp[1]] = $temp[2];
						}
					}
				} 
				return $result;
			}
		}
	}
	
	//开启大值自动压缩(完成)
	public function setCompressThreshold($threshold,$min_saving){
		$threshold = intval($threshold);
		$min_saving = floatval($min_saving);
		if ( $threshold <= 0 || $min_saving <= 0 || $min_saving >= 1 ){
			return false;
		}
		$this->threshold = $threshold;
		$this->min_saving = $min_saving*10;
		return true;
	}
	
	//压解--压缩(完成) 默认压缩,否则解压
	protected function gz_ungz_compress($string,$type=1){
		if ( $type == 1 ){
			return gzcompress($string,$this->min_saving);
		}else{
			return gzuncompress($string);
		}
	}
	
	
	//运行时修改服务器参数和状态(完成)
	public function setServerParams($host,$port,$timeout=1,$retry_interval=15,$status=1,$failure_callback = NULL){
		if ( empty($host) || empty($port) ){
			return false;
		}
		foreach ($this->server_list as $key => $row ) {
			if ( $row['host'] == $host && $row['port'] == $port ){
				$row['timeout'] = $timeout;
				$row['retry_interval'] = $retry_interval;
				$row['status'] = $status;
				$row['failure_callback'] = $failure_callback;
				$this->server_list[$key] = $row;
				return true;
			}
		}
		return false; 
		
	}
	
}

跟使用php的memcache扩展是一样的。这次更深入了解他的内部机制了.

extra clothes for the 3 year old and all the baby gear
porno Where Does Your Power Come From

sometimes pumps if I need to be super dressy
snooki weight lossMETTLER TOLEDO Offers Free System Handbook To Integrate Weigh Modules
how to lose weight fast
发表在 好文推荐, 网站开发, 网站架构 | 标签为 , | 2 条评论

php利用socket扩展写一个简单的单进程http服务1

废话少说,直接贴代码了。

<?php
	
	error_reporting(E_ALL);
	set_time_limit(0);
	ob_implicit_flush();
	date_default_timezone_set("Asia/Shanghai"); 


	$debug = true;
	
	function response_method_404(){
		$result = "";
		$result .="HTTP/1.1 404\r\n";
		$result .="Content-Length: 14\r\n";
		$result .= "Content-Type: text/html\r\n";
		$result .="\r\nserver is not \r\n";
		return $result;
	}
	
	function response_file_404(){
		$result = "";
		$result .="HTTP/1.1 404\r\n";
		$result .="Content-Length:0\r\n";
		$result .= "Content-Type: text/html\r\n";
		$result .="\r\n\r\n";
		return $result;
	}
	
	function response_file_type_404(){
		$result = "";
		$result .="HTTP/1.1 404\r\n";
		$result .="Content-Length:11\r\n";
		$result .= "Content-Type: text/html\r\n";
		$result .="\r\ntype is not\r\n";
		return $result;
	}
	
	function response_file_img($fileurl){
		$body = file_get_contents($fileurl);
		$len = strlen($body);
		$result="";
		$result .="HTTP/1.1 200\r\n";
		$result .="Content-Length:$len\r\n";
		$result .= "Content-Type: {$size['mime']}\r\n";
		$result .="\r\n$body\r\n";
		return $result;
	}
	
	
	function response_file_js($fileurl){
		global $request;
		$body = file_get_contents($fileurl);
		$len = strlen($body);
		$date = date('D, d M Y G:i:s ').'GMT';
		clearstatcache();
		$last_modified = date('D, d M Y G:i:s ',filemtime($fileurl)).'GMT';
		$result="";

		if ( $request['If-Modified-Since'] && $last_modified <= $request['If-Modified-Since'] ){
			$result .="HTTP/1.1 304\r\n";
		}else{
			$result .="HTTP/1.1 200\r\n";
			$result .="Cache-Control: public\r\n";
			$result .="Date:$date\r\n";
			$result .="Last-Modified:$last_modified\r\n";
			$result .="Expires:Fri, 24 Jan 2016 10:06:05 GMT\r\n";
			$result .="Content-Length:$len\r\n";
			$result .= "Content-Type: application/x-javascript\r\n";
			$result .="\r\n$body\r\n";
		}
		return $result;
	}
	
	
	function response_file_html($fileurl){
		$body = file_get_contents($fileurl);
		$len = strlen($body);
		$result = "";
		$result .="HTTP/1.1 200\r\n";
		$result .="Content-Length:$len\r\n";
		$result .= "Content-Type: text/html;charset=utf-8\r\n";
		$result .="\r\n$body\r\n";
		return $result;
	}
	
	function pare_request($str){
		global $web_root,$request;
		$arr = explode("\r\n",$str);
		foreach( $arr as $r){
			$pos = stripos($r,":");
			if ( $pos ){
				$key = trim(substr($r,0,$pos));
				$val = trim(substr($r,$pos+1));
				$request[$key] = $val;
			}

		}
		writelog(var_export($request,true));	
		$line0 = explode(" ",$arr[0]);
		if ( $line0[0] == "GET" ){
			$fileurl = $web_root.$line0[1];
			if ( file_exists($fileurl) ){
				$pathinfo = pathinfo($fileurl);
				if ( $pathinfo['extension'] == "js" ){
					$result = response_file_js($fileurl);
				}else if ( $pathinfo['extension'] == "png" || $pathinfo['extension'] == "jpg" || $pathinfo['extension'] == "gif" ){
					$result = response_file_img($fileurl);
				}else if ( $pathinfo['extension'] == "html" ){
					$result = response_file_html($fileurl);
				}else{
					$result = response_file_type_404();
				}
			}else{
				$result = response_file_404();
			}
		}else{
			$result = response_method_404();
		}
		return $result;
	}
	
	function sig_handler($signo) 
	{

	     switch ($signo) {
			 case SIGTERM:
				// 处理中断信号
			    exit;
			    break;
			 case SIGHUP:
			    // 处理重启信号
			    break;
			default:
			    // 处理所有其他信号
			}

	}


	function writelog($msg){
		global $debug;
		if ( $debug ){
			echo $msg;
		}else{
			file_put_contents('./log.txt',$msg,FILE_APPEND | LOCK_EX);
		}
	}
	
	if ( !$debug ){
		//产生子进程分支
		$pid = pcntl_fork();
		if ($pid == -1) {
			 writelog("could not fork\r\n");
			die("could not fork"); //pcntl_fork返回-1标明创建子进程失败
		} else if ($pid) {
			 exit(); //父进程中pcntl_fork返回创建的子进程进程号
		} else {
			 // 子进程pcntl_fork返回的时0
		}

		// 从当前终端分离
		if (posix_setsid() == -1) {
			writelog("could not detach form terminal \r\n");
			die("could not detach from terminal");
		}

		// 安装信号处理器
		pcntl_signal(SIGTERM, "sig_handler");
		pcntl_signal(SIGHUP, "sig_handler");

	}

	$address = '127.0.0.1';
	$port = 1215;
	$web_root = "/home/xtgxiso";
	$request = array();

	if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
		writelog("socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n");
		exit;
	}
	
	if (socket_bind($sock, $address, $port) === false) {
	    writelog("socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n");
		exit;
	}else{
		writelog('Socket ' . $address . ':' . $port . " has been opened\n");
	}
	
	if (socket_listen($sock, 5) === false) {
	    writelog("socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n");
		exit;
	}else{
		writelog("Listening for new clients..\n");
	}
	
	$client_id = 0;

	do{
		
		if ( ($msgsock = socket_accept($sock)) === false ) {
	        writelog("socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n");
	        break;
	    } else {
		    $client_id += 1;
			writelog("Client #" .$client_id .": Connect\n");
		}

		
		$cur_buf = '';
		do {
			if (false === ($buf = socket_read($msgsock, 2048))) {
				 writelog("socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n");
				 break;
			}
			writelog("read start:\r\n");
			writelog($buf);
			writelog("read end:\r\n\r\n");
			$talkback = pare_request($buf);
			writelog("write start:\r\n");
			socket_write($msgsock, $talkback, strlen($talkback));
			//writelog($talkback);
			writelog("write end:\r\n\r\n\r\n");
			break;
		} while (true);

		socket_close($msgsock);


	}while(true);

	writelog("socket_close \r\n");
	socket_close($sock);

实现了解析html,js,png,jpg,gif这几种格式的支持

这只是简单说明了一下思路,非常有利于大家了解http协议和优化其他http服务

as the women on the judging panel Guilana Rancik
snooki weight loss Oreimo Gets North American Simulcast Deal

and once soft
pornoRachel McAdams love triangle and baby bump rumors
cartoon porn
发表在 好文推荐, 网站开发, 网站架构 | 标签为 , | 2 条评论

关于MySQL LEFT JOIN 你需要了解的

即使你认为自己已对 MySQL 的 LEFT JOIN 理解深刻,这篇文章肯定能让你学会点东西!

ON 子句与 WHERE 子句的不同
一种更好地理解带有 WHERE … IS NULL 子句的复杂匹配条件的简单方法
Matching-Conditions 与 Where-conditions 的不同

关于 “A LEFT JOIN B ON 条件表达式” 的一点提醒
ON 条件(“A LEFT JOIN B ON 条件表达式”中的ON)用来决定如何从 B 表中检索数据行。
如果 B 表中没有任何一行数据匹配 ON 的条件,将会额外生成一行所有列为 NULL 的数据
在匹配阶段 WHERE 子句的条件都不会被使用。仅在匹配阶段完成以后,WHERE 子句条件才会被使用。它将从匹配阶段产生的数据中检索过滤。

让我们看一个 LFET JOIN 示例

mysql> CREATE TABLE `product` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `amount` int(10) unsigned default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1
 
mysql> CREATE TABLE `product_details` (
  `id` int(10) unsigned NOT NULL,
  `weight` int(10) unsigned default NULL,
  `exist` int(10) unsigned default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
 
mysql> INSERT INTO product (id,amount)
       VALUES (1,100),(2,200),(3,300),(4,400);
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0
 
mysql> INSERT INTO product_details (id,weight,exist)
       VALUES (2,22,0),(4,44,1),(5,55,0),(6,66,1);
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0
 
mysql> SELECT * FROM product;
+----+--------+
| id | amount |
+----+--------+
|  1 |    100 |
|  2 |    200 |
|  3 |    300 |
|  4 |    400 |
+----+--------+
4 rows in set (0.00 sec)
 
mysql> SELECT * FROM product_details;
+----+--------+-------+
| id | weight | exist |
+----+--------+-------+
|  2 |     22 |     0 |
|  4 |     44 |     1 |
|  5 |     55 |     0 |
|  6 |     66 |     1 |
+----+--------+-------+
4 rows in set (0.00 sec)
 
mysql> SELECT * FROM product LEFT JOIN product_details
       ON (product.id = product_details.id);
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist |
+----+--------+------+--------+-------+
|  1 |    100 | NULL |   NULL |  NULL |
|  2 |    200 |    2 |     22 |     0 |
|  3 |    300 | NULL |   NULL |  NULL |
|  4 |    400 |    4 |     44 |     1 |
+----+--------+------+--------+-------+
4 rows in set (0.00 sec)

ON 子句和 WHERE 子句有什么不同?
一个问题:下面两个查询的结果集有什么不同么?

用例子来理解最好不过了

mysql> SELECT * FROM product LEFT JOIN product_details
       ON (product.id = product_details.id)
       AND product_details.id=2;
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist |
+----+--------+------+--------+-------+
|  1 |    100 | NULL |   NULL |  NULL |
|  2 |    200 |    2 |     22 |     0 |
|  3 |    300 | NULL |   NULL |  NULL |
|  4 |    400 | NULL |   NULL |  NULL |
+----+--------+------+--------+-------+
4 rows in set (0.00 sec)
 
mysql> SELECT * FROM product LEFT JOIN product_details
       ON (product.id = product_details.id)
       WHERE product_details.id=2;
+----+--------+----+--------+-------+
| id | amount | id | weight | exist |
+----+--------+----+--------+-------+
|  2 |    200 |  2 |     22 |     0 |
+----+--------+----+--------+-------+
1 row in set (0.01 sec)

第一条查询使用 ON 条件决定了从 LEFT JOIN的 product_details表中检索符合的所有数据行。

第二条查询做了简单的LEFT JOIN,然后使用 WHERE 子句从 LEFT JOIN的数据中过滤掉不符合条件的数据行。

再来看一些示例:

mysql> SELECT * FROM product LEFT JOIN product_details
       ON product.id = product_details.id
       AND product.amount=100;
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist |
+----+--------+------+--------+-------+
|  1 |    100 | NULL |   NULL |  NULL |
|  2 |    200 | NULL |   NULL |  NULL |
|  3 |    300 | NULL |   NULL |  NULL |
|  4 |    400 | NULL |   NULL |  NULL |
+----+--------+------+--------+-------+
4 rows in set (0.00 sec)

所有来自product表的数据行都被检索到了,但没有在product_details表中匹配到记录(product.id = product_details.id AND product.amount=100 条件并没有匹配到任何数据)

mysql> SELECT * FROM product LEFT JOIN product_details
       ON (product.id = product_details.id)
       AND product.amount=200;
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist |
+----+--------+------+--------+-------+
|  1 |    100 | NULL |   NULL |  NULL |
|  2 |    200 |    2 |     22 |     0 |
|  3 |    300 | NULL |   NULL |  NULL |
|  4 |    400 | NULL |   NULL |  NULL |
+----+--------+------+--------+-------+
4 rows in set (0.01 sec)



mysql> SELECT * FROM product LEFT JOIN product_details
       ON (product.id = product_details.id)
       AND product.amount=200;
+----+--------+------+--------+-------+
| id | amount | id   | weight | exist |
+----+--------+------+--------+-------+
|  1 |    100 | NULL |   NULL |  NULL |
|  2 |    200 |    2 |     22 |     0 |
|  3 |    300 | NULL |   NULL |  NULL |
|  4 |    400 | NULL |   NULL |  NULL |
+----+--------+------+--------+-------+
4 rows in set (0.01 sec)

看看下面的示例:

mysql> SELECT a.* FROM product a LEFT JOIN product_details b
       ON a.id=b.id AND b.weight!=44 AND b.exist=0
       WHERE b.id IS NULL;
+----+--------+
| id | amount |
+----+--------+
|  1 |    100 |
|  3 |    300 |
|  4 |    400 |
+----+--------+
3 rows in set (0.00 sec)

让我们检查一下 ON 匹配子句:
    (a.id=b.id) AND (b.weight!=44) AND (b.exist=0)

我们可以把 IS NULL 子句 看作是否定匹配条件。

这意味着我们将检索到以下行:
    !( exist(b.id that equals to a.id) AND b.weight !=44 AND b.exist=0 )
    !exist(b.id that equals to a.id) || !(b.weight !=44) || !(b.exist=0)
    !exist(b.id that equals to a.id) || b.weight =44 || b.exist=1

就像在C语言中的逻辑 AND 和 逻辑 OR表达式一样,其操作数是从左到右求值的。如果第一个参数做够判断操作结果,那么第二个参数便不会被计算求值(短路效果)

看看别的示例:

mysql> SELECT a.* FROM product a LEFT JOIN product_details b
	       ON a.id=b.id AND b.weight!=44 AND b.exist=1
	       WHERE b.id IS NULL;
	+----+--------+
	| id | amount |
	+----+--------+
	|  1 |    100 |
	|  2 |    200 |
	|  3 |    300 |
	|  4 |    400 |
	+----+--------+
	4 rows in set (0.00 sec)

Matching-Conditions 与 Where-conditions 之战

如果你吧基本的查询条件放在 ON 子句中,把剩下的否定条件放在 WHERE 子句中,那么你会获得相同的结果。

例如,你可以不这样写:
    SELECT a.* FROM product a LEFT JOIN product_details b
    ON a.id=b.id AND b.weight!=44 AND b.exist=0
    WHERE b.id IS NULL;

你可以这样写:
    SELECT a.* FROM product a LEFT JOIN product_details b
    ON a.id=b.id
    WHERE b.id is null OR b.weight=44 OR b.exist=1;

    mysql> SELECT a.* FROM product a LEFT JOIN product_details b
           ON a.id=b.id
           WHERE b.id is null OR b.weight=44 OR b.exist=1;
    +—-+——–+
    | id | amount |
    +—-+——–+
    |  1 |    100 |
    |  3 |    300 |
    |  4 |    400 |
    +—-+——–+
    3 rows in set (0.00 sec)

你可以不这样写:
    SELECT a.* FROM product a LEFT JOIN product_details b
    ON a.id=b.id AND b.weight!=44 AND b.exist!=0
    WHERE b.id IS NULL;

可以这样写:
    SELECT a.* FROM product a LEFT JOIN product_details b
    ON a.id=b.id
    WHERE b.id is null OR b.weight=44 OR b.exist=0;

    mysql> SELECT a.* FROM product a LEFT JOIN product_details b
           ON a.id=b.id
           WHERE b.id is null OR b.weight=44 OR b.exist=0;
    +—-+——–+
    | id | amount |
    +—-+——–+
    |  1 |    100 |
    |  2 |    200 |
    |  3 |    300 |
    |  4 |    400 |
    +—-+——–+
    4 rows in set (0.00 sec)

这些查询真的效果一样?

如果你只需要第一个表中的数据的话,这些查询会返回相同的结果集。有一种情况就是,如果你从 LEFT JOIN的表中检索数据时,查询的结果就不同了。

如前所属,WHERE 子句是在匹配阶段之后用来过滤的。

例如:

mysql> SELECT * FROM product a LEFT JOIN product_details b
	       ON a.id=b.id AND b.weight!=44 AND b.exist=1
	       WHERE b.id is null;
	+----+--------+------+--------+-------+
	| id | amount | id   | weight | exist |
	+----+--------+------+--------+-------+
	|  1 |    100 | NULL |   NULL |  NULL |
	|  2 |    200 | NULL |   NULL |  NULL |
	|  3 |    300 | NULL |   NULL |  NULL |
	|  4 |    400 | NULL |   NULL |  NULL |
	+----+--------+------+--------+-------+
	4 rows in set (0.00 sec)
	  
	mysql> SELECT * FROM product a LEFT JOIN product_details b
	       ON a.id=b.id
	       WHERE b.id IS NULL OR b.weight=44 OR b.exist=0;
	+----+--------+------+--------+-------+
	| id | amount | id   | weight | exist |
	+----+--------+------+--------+-------+
	|  1 |    100 | NULL |   NULL |  NULL |
	|  2 |    200 |    2 |     22 |     0 |
	|  3 |    300 | NULL |   NULL |  NULL |
	|  4 |    400 |    4 |     44 |     1 |
	+----+--------+------+--------+-------+
	4 rows in set (0.00 sec)

原文链接:http://www.mysqldiary.com/mysql-left-join/

there are many accredited colleges to
how to lose weight fast Damaged Hair Care for the Black Woman

Instead of a couture dress that others can only dream of affording
christina aguilera weight lossExclusive interview with Danielle Pettee
black porn
发表在 网站开发 | 标签为 | 一条评论

Linux系统信息查看命令大全

系统

# uname -a               # 查看内核/操作系统/CPU信息
# head -n 1 /etc/issue   # 查看操作系统版本
# cat /proc/cpuinfo      # 查看CPU信息
# hostname               # 查看计算机名
# lspci -tv              # 列出所有PCI设备
# lsusb -tv              # 列出所有USB设备
# lsmod                  # 列出加载的内核模块
# env                    # 查看环境变量

资源

# free -m                # 查看内存使用量和交换区使用量
# df -h                  # 查看各分区使用情况
# du -sh <目录名>        # 查看指定目录的大小
# grep MemTotal /proc/meminfo   # 查看内存总量
# grep MemFree /proc/meminfo    # 查看空闲内存量
# uptime                 # 查看系统运行时间、用户数、负载
# cat /proc/loadavg      # 查看系统负载

磁盘和分区

# mount | column -t      # 查看挂接的分区状态
# fdisk -l               # 查看所有分区
# swapon -s              # 查看所有交换分区
# hdparm -i /dev/hda     # 查看磁盘参数(仅适用于IDE设备)
# dmesg | grep IDE       # 查看启动时IDE设备检测状况

网络

# ifconfig               # 查看所有网络接口的属性
# iptables -L            # 查看防火墙设置
# route -n               # 查看路由表
# netstat -lntp          # 查看所有监听端口
# netstat -antp          # 查看所有已经建立的连接
# netstat -s             # 查看网络统计信息

进程

# ps -ef                 # 查看所有进程
# top                    # 实时显示进程状态

用户

# w                      # 查看活动用户
# id <用户名>            # 查看指定用户信息
# last                   # 查看用户登录日志
# cut -d: -f1 /etc/passwd   # 查看系统所有用户
# cut -d: -f1 /etc/group    # 查看系统所有组
# crontab -l             # 查看当前用户的计划任务

服务

# chkconfig --list       # 列出所有系统服务
# chkconfig --list | grep on    # 列出所有启动的系统服务

程序

# rpm -qa                # 查看所有安装的软件包

and matching jeans
quick weight loss What Is a 5 Course Meal

Choosing a costume for any child can be difficult at best
gay pornJapan Most Famous Exports to the Western World
hd porn
发表在 网站架构 | 2 条评论

inux chkconfig命令参数及用法详解–linux系统服务设置命令

chkconfig

功能说明:chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接
语  法:chkconfig [--add][--del][--list][系统服务] 或 chkconfig [--level <等级代号>][系统服务][on/off/reset]

linux os 将操作环境分为以下7个等级:

0:开机(请不要切换到此等级)
1:单人使用者模式的文字界面
2:多人使用者模式的文字界面,不具有网络档案系统(NFS)功能
3:多人使用者模式的文字界面,具有网络档案系统(NFS)功能
4:某些发行版的linux使用此等级进入x windows system
5:某些发行版的linux使用此等级进入x windows system
6:重新启动

参  数:
–add            新增所指定的系统服务
–del             删除所指定的系统服务
–level         指定该系统服务要在哪个执行等级中开启或关闭
–list           列出当前可从chkconfig指令管理的所有系统服务和等级代号
on/off/reset    在指定的执行登记,开启/关闭/重置该系统服务

1:chkconfig 命令也可以用来激活和解除服务。chkconfig –list 命令显示系统服务列表,以及这些服务在运行级别0到6中已被启动(on)还是停止(off)。

chkconfig –list

chkconfig –list httpd

httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

2:chkconfig 还能用来设置某一服务在某一指定的运行级别内被启动还是被停运。譬如,要在运行级别3、4、5中停运 nscd 服务,使用下面的命令:

chkconfig –level 345 nscd off

3:由 xinetd 管理的服务会立即被 chkconfig 影响。譬如,如果 xinetd 在运行,finger 被禁用,那么执行了 chkconfig finger on 命令后,finger 就不必手工地重新启动 xinetd 来立即被启用。对其它服务的改变在使用 chkconfig 之后不会立即生效。必须使用service servicename  start/stop/restart命令来重起服务

每个被chkconfig管理的服务需要在对应的init.d下的脚本加上两行或者更多行的注释。第一行告诉chkconfig缺省启动的运行级以及启动和停止的优先级。如果某服务缺省不在任何运行级启动,那么使用- 代替运行级。第二行对服务进行描述,可以用/ 跨行注释

but it really depends on what time period you’re talking about
snooki weight loss Gucci Is the most Fashionable Brand all over the World

my weight loss progress jumped right back on track
how to lose weight fastFashion Design for Capri One Piece Jumpsuits
youjizz
发表在 网站架构 | 一条评论