{"id":767,"date":"2016-06-10T18:28:06","date_gmt":"2016-06-10T10:28:06","guid":{"rendered":"http:\/\/www.luwl.net\/?p=767"},"modified":"2016-06-10T18:28:06","modified_gmt":"2016-06-10T10:28:06","slug":"swoole%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"http:\/\/www.luwl.net\/?p=767","title":{"rendered":"swoole\u5e94\u7528"},"content":{"rendered":"<p>\u5b9e\u73b0\u4e86\u6570\u636e\u53d1\u9001json\u683c\u5f0f\u540e\u5b58\u50a8\u5230\u6570\u636e\u5e93\uff0c\u540c\u65f6\u5b58\u50a8\u5230cache\u6587\u4ef6\u91cc\u3002<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\n&lt;?php\r\n\/*\r\n$cache_set = array(\r\n\/\/\u7f13\u5b58\u8def\u5f84 , \u6700\u540e\u8981\u52a0&quot;\/&quot;\r\n'cacheRoot'=&gt;'.\/cache\/',\r\n\/\/\u7f13\u5b58\u65f6\u95f4\r\n'cacheTime'=&gt;20,\r\n\/\/cache type\r\n'cacheType'=&gt;1,\r\n\/\/\u6269\u5c55\u540d\r\n'cacheExe'=&gt;'.php'\r\n);\r\n$cache = new Cache($cache_set);\r\n$a=array('1','2');\r\n$a=&quot;aaa&quot;;\r\n$b='';\r\nif($cache-&gt;cache_is(&quot;d&quot;)){\r\n$c=$cache-&gt;cache_read(&quot;d&quot;);\r\necho &quot;c&quot;;\r\nprint_r($c);\r\n}else {\r\n$b=$cache-&gt;cache_data('d',$a);\r\n}\r\nprint_r($b);\r\n\/\/$cache-&gt;clear(&quot;a&quot;);\r\n\/\/echo $cache-&gt;cache_read(&quot;.\/cache\/d.php&quot;);\r\n\/\/echo $d;\r\n*\/\r\n\r\n\r\n\/**\r\n* \u6570\u636e\u7f13\u5b58\u7c7b v1.0\r\n* @author shooke\r\n* 2009-11-13 16:02:26\r\n* \u7528\u4e8e\u7f13\u5b58\u6570\u636e,\u5982\u53d8\u91cf,\u4f46\u4e0d\u80fd\u7f13\u5b58\u9875\u9762\r\n*\/\r\nclass Cache{\r\n\/\/\u914d\u7f6e\r\npublic $config = array(\r\n\/\/\u7f13\u5b58\u8def\u5f84\r\n'cacheRoot'=&gt;'.\/cache\/',\r\n\/\/\u7f13\u5b58\u65f6\u95f4\r\n'cacheTime'=&gt;1,\r\n\/\/cache \u7c7b\u578b 1\u4e32\u5316\u6570\u636e 2\u53d8\u91cf\r\n'cacheType'=&gt;2,\r\n\/\/\u6269\u5c55\u540d\r\n'cacheExe'=&gt;'.php'\r\n\/\/\u8f6c\u6362\u4e2d\u95f4\u53d8\u91cf\r\n);\r\n\r\npublic $return_name=array();\r\n\r\nfunction __construct($cache_set = array())\r\n{\r\nif(!empty($cache_set)) $this-&gt;config=array_merge($this-&gt;config,$cache_set);\r\n$this-&gt;config&#x5B;'ClassName'] = __CLASS__;\r\n}\r\n\r\npublic function clear($filename=''){\r\nif (file_exists($this-&gt;cache_file($filename))) {\r\n@unlink($this-&gt;cache_file($filename));\r\n}elseif (empty($filename)){\r\n$this-&gt;clear_dir($this-&gt;config&#x5B;'cacheRoot']);\r\n}else{\r\n$this-&gt;clear_dir($this-&gt;config&#x5B;'cacheRoot'].$filename);\r\necho $this-&gt;config&#x5B;'cacheRoot'].$filename;\r\n}\r\n}\r\n\/\/\u5faa\u73af\u5220\u9664\u8def\u5f84\r\nprivate function clear_dir($dir,$to = false)\r\n{\r\nif ($list = glob($dir.'\/*'))\r\n{\r\nforeach ($list as $file)\r\n{\r\nis_dir($file) ? $this-&gt;clear_dir($file) : unlink($file);\r\n}\r\n}\r\n\r\nif ($to === false) rmdir($dir);\r\n}\r\n\r\n\/\/\u5199\u5165\u7f13\u5b58\r\nprivate function cache_write($filename, $writetext, $openmod='w'){\r\nif (!file_exists($filename)) {\r\n@$this-&gt;makeDir( dirname($filename ));\r\n}\r\nif(@$fp = fopen($filename, $openmod)) {\r\nflock($fp, 2);\r\nfwrite($fp, $writetext);\r\nfclose($fp);\r\nreturn true;\r\n} else {\r\necho &quot;File: $filename write error.&quot;;\r\nreturn false;\r\n}\r\n}\r\n\r\n\/\/\u7f13\u5b58\u6709\u6548\u671f \u6709\u6548\u8fd4\u56de true\r\npublic function cache_is($fileName){\r\n$fileName=$this-&gt;cache_file($fileName);\r\nif( file_exists( $fileName ) ) {\r\n\/\/\u5982\u679c\u7f13\u5b58\u65f6\u95f4\u4e3a\u8d1f\u6570\u5219\u6c38\u4e0d\u8fc7\u671f\r\nif ($this-&gt;config&#x5B;'cacheTime'] &lt; 0) {\r\nreturn true;\r\n}\r\n\/\/\u5982\u679c\u7f13\u5b58\u65f6\u95f4\u4e3a0\u5219\u4e00\u76f4\u8fc7\u671f\r\nif ($this-&gt;config&#x5B;'cacheTime'] == 0) {\r\nreturn false;\r\n}\r\n\/\/\u83b7\u53d6\u7f13\u5b58\u6587\u4ef6\u7684\u5efa\u7acb\u65f6\u95f4\r\n$ctime = intval(filemtime( $fileName ));\r\n\/\/\u6bd4\u8f83\u662f\u5426\u5927\u4e8e\u7f13\u5b58\u65f6\u95f4,\u662f\u5219\u8fc7\u671f \u5426\u5219\u4e0d\u8fc7\u671f\r\nif (time() - $ctime &gt; $this-&gt;config&#x5B;'cacheTime']) {\r\nreturn false;\r\n}else {\r\nreturn true;\r\n}\r\n\/\/\u6587\u4ef6\u4e0d\u5b58\u5728\u89c6\u4e3a\u8fc7\u671f\u5931\u6548\r\n}else {\r\nreturn false;\r\n}\r\n}\r\n\r\npublic function cache_data($name,$data){\r\n$varname=$name;\r\n$name = $this-&gt;cache_file($name);\r\n\/\/config&#x5B;'cacheTime']==0\u4e5f\u5c31\u662f\u4e0d\u542f\u7528\u7f13\u5b58\u662f\u76f4\u63a5\u8fd4\u56de\u6570\u636e\r\nif ($this-&gt;config&#x5B;'cacheTime'] &lt;&gt; 0) {\r\nif($this-&gt;config&#x5B;'cacheType']==1){\r\n$write_data = &quot;&lt;?php exit;?&gt;&quot;.serialize($data);\r\n\/\/return $data;\r\n}else {\r\n$write_data = &quot;&lt;?php\\r\\n\\$var= &quot;;\r\n$write_data .= var_export($data,true);\r\n$write_data .=&quot;;\\r\\n?&gt;&quot;;\r\n}\r\n$this-&gt;cache_write($name,$write_data);\r\n}\r\nreturn $data;\r\n\r\n}\r\n\/\/\u7f13\u5b58\u6587\u4ef6\u540d\r\nprivate function cache_file($filename){\r\nreturn $this-&gt;config&#x5B;'cacheRoot'].$filename.$this-&gt;config&#x5B;'cacheExe'];\r\n}\r\n\r\n\/\/\u8bfb\u53d6\u6587\u4ef6\r\npublic function cache_read($file){\r\n$file=$this-&gt;cache_file($file);\r\nif (!file_exists($file)) {\r\nreturn '';\r\n}\r\nif($this-&gt;config&#x5B;'cacheType']==1){\r\nif (function_exists('file_get_contents')){\r\n$cache_Content= file_get_contents($file);\r\n}else{\r\n$fopen = fopen($file,'r');\r\n$cache_Content = '';\r\ndo {\r\n$data = fread($fopen,filesize($file));\r\nif (strlen($data)===0) break;\r\n$cache_Content .= $data;\r\n}while(1);\r\nfclose($fopen);\r\n}\r\n$cache_Content = substr($cache_Content,13);\/* \u53bb\u9664&lt;?php exit;?&gt; *\/\r\n$cache_Content = unserialize($cache_Content);\r\nreturn $cache_Content;\r\n}else{\r\ninclude_once($file);\r\nreturn $var;\r\n}\r\n\r\n}\r\n\r\n\/\/\u5faa\u73af\u521b\u5efa\u76ee\u5f55\r\nprivate function makeDir( $dir, $mode = 0777 ) {\r\nif( ! $dir ) return 0;\r\n$dir = str_replace( &quot;\\\\&quot;, &quot;\/&quot;, $dir );\r\n$mdir = &quot;&quot;;\r\nforeach( explode( &quot;\/&quot;, $dir ) as $val ) {\r\n$mdir .= $val.&quot;\/&quot;;\r\nif( $val == &quot;..&quot; || $val == &quot;.&quot; || trim( $val ) == &quot;&quot; ) continue;\r\n\r\nif( ! file_exists( $mdir ) ) {\r\nif(!@mkdir( $mdir, $mode )){\r\nreturn false;\r\n}\r\n}\r\n}\r\nreturn true;\r\n}\r\n\r\n}\r\n\r\n?&gt;\r\n<\/pre>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\n&lt;?php\r\nrequire('Cache.php');\r\n$cache_set = array(\r\n\/\/\u7f13\u5b58\u8def\u5f84 , \u6700\u540e\u8981\u52a0&quot;\/&quot;\r\n'cacheRoot'=&gt;'.\/cache\/',\r\n\/\/\u7f13\u5b58\u65f6\u95f4\r\n'cacheTime'=&gt;20,\r\n\/\/cache type\r\n'cacheType'=&gt;1,\r\n\/\/\u6269\u5c55\u540d\r\n'cacheExe'=&gt;'.php'\r\n);\r\nfunction is_json($string) {\r\njson_decode($string);\r\nreturn (json_last_error() == JSON_ERROR_NONE);\r\n}\r\n$serv = new swoole_server(&quot;0.0.0.0&quot;, 9501, SWOOLE_BASE, SWOOLE_SOCK_UDP);\r\n$serv-&gt;set(array(\r\n'dispatch_mode' =&gt; 1,\r\n'worker_num' =&gt; 4,\r\n));\r\n$serv-&gt;on('Start', function ($serv){\r\necho &quot;MasterPid={$serv-&gt;master_pid}|Manager_pid={$serv-&gt;manager_pid}\\n&quot;;\r\necho &quot;Server: start.Swoole version is &#x5B;&quot;.SWOOLE_VERSION.&quot;]\\n&quot;;\r\n});\r\n$serv-&gt;on('receive', function ($serv, $fd, $from_id, $data) {\r\nif (is_json($data)){\r\n$array=json_decode($data);\r\nif (array_key_exists(&quot;add&quot;,$array)){\r\n$a=$array-&gt;add;\r\n$cache_set = array(\r\n\/\/\u7f13\u5b58\u8def\u5f84 , \u6700\u540e\u8981\u52a0&quot;\/&quot;\r\n'cacheRoot'=&gt;'.\/cache\/',\r\n\/\/\u7f13\u5b58\u65f6\u95f4\r\n'cacheTime'=&gt;20,\r\n\/\/cache type\r\n'cacheType'=&gt;1,\r\n\/\/\u6269\u5c55\u540d\r\n'cacheExe'=&gt;'.php'\r\n);\r\n$cache = new Cache($cache_set);\r\n$cache-&gt;cache_data('add'.$a,$fd);\r\nforeach($array as $key=&gt;$value)\r\n{\r\nif ($key&lt;&gt;&quot;add&quot;){\r\n$db_host = '127.0.0.1'; \/\/\u6570\u636e\u5e93\u4e3b\u673a\u540d\u79f0\uff0c\u4e00\u822c\u90fd\u4e3alocalhost\r\n$db_user = 'hsg'; \/\/\u6570\u636e\u5e93\u7528\u6237\u5e10\u53f7\uff0c\u6839\u636e\u4e2a\u4eba\u60c5\u51b5\u800c\u5b9a\r\n$db_passw = 'hongshigou'; \/\/\u6570\u636e\u5e93\u7528\u6237\u5bc6\u7801\uff0c\u6839\u636e\u4e2a\u4eba\u60c5\u51b5\u800c\u5b9a\r\n$db_name = 'hsg'; \/\/\u6570\u636e\u5e93\u5177\u4f53\u540d\u79f0\r\n\/\/\u8fde\u63a5\u6570\u636e\u5e93\r\n$conn = MySQL_connect($db_host,$db_user,$db_passw) or die ('\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25\uff01');\r\n\/\/\u8bbe\u7f6e\u5b57\u7b26\u96c6\uff0c\u5982utf8\u548cgbk\u7b49\uff0c\u6839\u636e\u6570\u636e\u5e93\u7684\u5b57\u7b26\u96c6\u800c\u5b9a\r\n\/\/MySQL_query(&quot;set names 'utf8'&quot;);\r\nMySQL_query(&quot;set names 'gb2312'&quot;);\r\n\/\/\u9009\u5b9a\u6570\u636e\u5e93\r\nMySQL_select_db($db_name,$conn) or die('\u6570\u636e\u5e93\u9009\u5b9a\u5931\u8d25\uff01');\r\n\/\/\u6267\u884cSQL\u8bed\u53e5(\u67e5\u8be2)\r\n$sql = &quot;INSERT INTO `hsg`.`data` (`point` , `val`)VALUES ('$key$a', '$value');&quot;;\r\n$result = MySQL_query($sql) or die('\u6570\u636e\u5e93\u67e5\u8be2\u5931\u8d25!&lt;br\/&gt;\u53ef\u80fd\u6570\u636e\u5e93\u4e2d\u6ca1\u6709\u8bb0\u5f55'); \/\/SQL\u8bed\u53e5\u5728\u8fd9\u91cc\u6267\u884c\r\nif(!$result){\r\necho $fd.&quot; &quot;;\r\n}\r\n$cache-&gt;cache_data($key.$a,$value);\r\nmysql_close($conn);\r\n}\r\n}\r\n$serv-&gt;send($fd, &quot;OK&quot;);\r\n}\r\n}\r\n});\r\n$serv-&gt;start();\r\n?&gt;\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9e\u73b0\u4e86\u6570\u636e\u53d1\u9001json\u683c\u5f0f\u540e\u5b58\u50a8\u5230\u6570\u636e\u5e93\uff0c\u540c\u65f6\u5b58\u50a8\u5230cache\u6587\u4ef6\u91cc\u3002 &lt; &hellip; <a href=\"http:\/\/www.luwl.net\/?p=767\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-767","post","type-post","status-publish","format-standard","hentry","category-net"],"_links":{"self":[{"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/posts\/767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.luwl.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=767"}],"version-history":[{"count":1,"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/posts\/767\/revisions"}],"predecessor-version":[{"id":768,"href":"http:\/\/www.luwl.net\/index.php?rest_route=\/wp\/v2\/posts\/767\/revisions\/768"}],"wp:attachment":[{"href":"http:\/\/www.luwl.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.luwl.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=767"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.luwl.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}