<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>幻想曲.Net &#187; Develop</title>
	<atom:link href="http://yibin.us/category/develop/feed" rel="self" type="application/rss+xml" />
	<link>http://yibin.us</link>
	<description>曾经沧海难为水，除却巫山不是云</description>
	<lastBuildDate>Tue, 15 May 2012 14:31:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>数据是怎么多出来的？</title>
		<link>http://yibin.us/archives/6854</link>
		<comments>http://yibin.us/archives/6854#comments</comments>
		<pubDate>Tue, 15 May 2012 14:27:41 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6854</guid>
		<description><![CDATA[这段时间比较折磨，在做一个广告页面，该页面需要统计到各个联盟站点过来的ＩＰ数和注册、充值量。 最开始的做法，是在静态的广告页面里放入一段php的script，由该php去实现ip/pv的统计。 即： ad.html中嵌入 &#60;script src=”http://xxxxx/stat.php?id=xxxxxxxx”&#62;&#60;/script&#62; 在stat.php页面中主要有以下业务逻辑： 1、取到广告id 2、判断该广告id在数据库中是否存在，如果不存在，则直接中止 3、判断ip在该广告id中是否出现过，如果没有出现过，则insert，然后update统计表，做+1操作 4、对pv统计表做insert(当天当时的记录不存在)或update(存在，做+1)操作 这里采用了前期统计，即，每进来一条数据，都会更新一下统计表，这样后台在查询时，就无需做count或group之类的查询了。 二天后，广告开始投放了，结果发现我们统计到的数据比网盟的数据要少，相差约在30%-40%左右，正常情况下应该是在10%以下，很明显，这里产生了数据的丢失。 经过分析，可能是stat.php在处理一个请求时，要经过5次db操作，在大并发时可能产生mysql排队，无法处理后续的数据请求，造成数据丢失；由于stat.php文件与最终的广告页面不在同一个域，会造成一次多余的http请求，而对stat.php的请求可能会产生失败，这里也存在数据丢失的可能性。 后来，想到一个方案，去掉stat.php文件，对外提供一个ad.php文件，直接在该文件做ip的统计，该文件只做insert，不做任何判断，后台的数据查看采用后期统计，虽然会慢一点，但是可以最大限度保证原始数据的正确性。 表stat存储引擎为MyISAM，没有任何索引。ad.php文件大致内容如下： 用loadrunner开始2400个虚拟用户，每1秒钟增加5个用户，当达到2400用户时，连续压测1个小时。1小时后loadrunner通过的事务数与我在数据库中查到的完全一致，此时loadrunner的每秒处理量是1700-1800左右，从数据的对比看起来好像没有问题。 下午找了一个网盟做测试，10分钟后，网盟给的数据是1.15W个独立ip，站长统计得到的ip是12000左右，可我这边对ip字段做distinct后却发现是15000个ip……，pv也远远多于站长统计里的pv量，我。。凌乱了。。。 这多的数据是哪里来的？分析nginx的日志，nginx中ad.php的访问量也少于我数据库中的记录数，ip也少于数据库中的记录数……我无语了…… 简单的一条insert，却造成3方数据不统一，这问题到底出在哪？我开始怀疑自己了。。。。]]></description>
			<content:encoded><![CDATA[<p>这段时间比较折磨，在做一个广告页面，该页面需要统计到各个联盟站点过来的ＩＰ数和注册、充值量。</p>
<p>最开始的做法，是在静态的广告页面里放入一段php的script，由该php去实现ip/pv的统计。</p>
<p>即：</p>
<p>ad.html中嵌入</p>
<p>&lt;script src=”http://xxxxx/stat.php?id=xxxxxxxx”&gt;&lt;/script&gt;</p>
<p>在stat.php页面中主要有以下业务逻辑：</p>
<p>1、取到广告id</p>
<p>2、判断该广告id在数据库中是否存在，如果不存在，则直接中止</p>
<p>3、判断ip在该广告id中是否出现过，如果没有出现过，则insert，然后update统计表，做+1操作</p>
<p>4、对pv统计表做insert(当天当时的记录不存在)或update(存在，做+1)操作</p>
<p>这里采用了前期统计，即，每进来一条数据，都会更新一下统计表，这样后台在查询时，就无需做count或group之类的查询了。</p>
<p>二天后，广告开始投放了，结果发现我们统计到的数据比网盟的数据要少，相差约在30%-40%左右，正常情况下应该是在10%以下，很明显，这里产生了数据的丢失。</p>
<p>经过分析，可能是stat.php在处理一个请求时，要经过5次db操作，在大并发时可能产生mysql排队，无法处理后续的数据请求，造成数据丢失；由于stat.php文件与最终的广告页面不在同一个域，会造成一次多余的http请求，而对stat.php的请求可能会产生失败，这里也存在数据丢失的可能性。</p>
<p>后来，想到一个方案，去掉stat.php文件，对外提供一个ad.php文件，直接在该文件做ip的统计，该文件只做insert，不做任何判断，后台的数据查看采用后期统计，虽然会慢一点，但是可以最大限度保证原始数据的正确性。</p>
<p>表stat存储引擎为MyISAM，没有任何索引。ad.php文件大致内容如下：</p>
<pre class="brush: php; title: ; notranslate">

&lt;?php

mysql_pconnect('localhost','user','passwd');

mysql_select_db('ad_analysis');

//insert量比较大，这里用了延迟写入

mysql_query('insert delayed into.......');

//方便loadrunner加检测点，这里直接输出了//ok

die('//ok');

//其它html代码

//为了对比，这里还加入了站长统计的代码

?&gt;
</pre>
<p>用loadrunner开始2400个虚拟用户，每1秒钟增加5个用户，当达到2400用户时，连续压测1个小时。1小时后loadrunner通过的事务数与我在数据库中查到的完全一致，此时loadrunner的每秒处理量是1700-1800左右，从数据的对比看起来好像没有问题。</p>
<p>下午找了一个网盟做测试，10分钟后，网盟给的数据是1.15W个独立ip，站长统计得到的ip是12000左右，可我这边对ip字段做distinct后却发现是15000个ip……，pv也远远多于站长统计里的pv量，我。。凌乱了。。。</p>
<p>这多的数据是哪里来的？分析nginx的日志，nginx中ad.php的访问量也少于我数据库中的记录数，ip也少于数据库中的记录数……我无语了……</p>
<p>简单的一条insert，却造成3方数据不统一，这问题到底出在哪？我开始怀疑自己了。。。。</p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6854/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MongoDB中自增ID的实现</title>
		<link>http://yibin.us/archives/6841</link>
		<comments>http://yibin.us/archives/6841#comments</comments>
		<pubDate>Fri, 17 Jun 2011 01:00:45 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[mongodb]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6841</guid>
		<description><![CDATA[MongoDB自己采用的是ObjectId，因为是分布式存储，所以并没有用自增型的ID。 但有时我们却又需要这种类型的字段，如何去实现呢？ MongoDB中新建一个Collection，数据为 {“app”:collectionname,”UniqueId”:0} app为collection名称，UniqueId为当前的最大ID，利用MongoDB的FindAndModify方法，实现对指定collection中的UniqueId的自增操作。 这里采用的MongoDB驱动是官方推荐的MongoDB-CSharp]]></description>
			<content:encoded><![CDATA[<p>MongoDB自己采用的是ObjectId，因为是分布式存储，所以并没有用自增型的ID。</p>
<p>但有时我们却又需要这种类型的字段，如何去实现呢？</p>
<p>MongoDB中新建一个Collection，数据为</p>
<p>{“app”:collectionname,”UniqueId”:0}</p>
<p>app为collection名称，UniqueId为当前的最大ID，利用MongoDB的FindAndModify方法，实现对指定collection中的UniqueId的自增操作。</p>
<pre class="brush: csharp; title: ; notranslate">

Int64 GenerateUniqueId(string CollectionName,int InitValue)
 {
 Connect();
 CollectionName = CollectionName.Trim();

 var collection = db.GetCollection(&quot;UniqueStore&quot;);

 var query = Query.EQ(&quot;app&quot;, CollectionName);
 var update = Update.Inc(&quot;id&quot;, 1);

 var doc = collection.FindAndModify(query, SortBy.Null, update, true);
 if (doc.ModifiedDocument == null)
 {
 collection.Save(new BsonDocument { { &quot;app&quot;, CollectionName }, { &quot;UniqueId&quot;, InitValue } });
 return InitValue;
 }
 return Convert.ToInt64(doc.ModifiedDocument[&quot;UniqueId&quot;]);
 }
</pre>
<p>这里采用的MongoDB驱动是官方推荐的MongoDB-CSharp</p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6841/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>apache2在win7 rtm下无法启动</title>
		<link>http://yibin.us/archives/6368</link>
		<comments>http://yibin.us/archives/6368#comments</comments>
		<pubDate>Sun, 19 Jul 2009 13:35:55 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[win7]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6368</guid>
		<description><![CDATA[昨天安装的win7 rtm 7600.16385，这个是win7 rtm的最终版本了，值得一试。 安装完成后，装上apache2与mysql均一切顺利，配置好httpd.conf，加载php5apache2模块后，启动apache失败，日志如下： [Sun Jul 19 17:05:55 2009] [warn] pid file I:/win7_amp/Apache2/logs/httpd.pid overwritten &#8212; Unclean shutdown of previous Apache run? [Sun Jul 19 17:06:00 2009] [warn] pid file I:/win7_amp/Apache2/logs/httpd.pid overwritten &#8212; Unclean shutdown of previous Apache run? [Sun Jul 19 17:06:04 2009] [warn] pid file I:/win7_amp/Apache2/logs/httpd.pid overwritten &#8212; Unclean shutdown of previous Apache [...]]]></description>
			<content:encoded><![CDATA[<p>昨天安装的win7 rtm 7600.16385，这个是win7 rtm的最终版本了，值得一试。</p>
<p>安装完成后，装上apache2与mysql均一切顺利，配置好httpd.conf，加载php5apache2模块后，启动apache失败，日志如下：</p>
<blockquote><p>[Sun Jul 19 17:05:55 2009] [warn] pid file I:/win7_amp/Apache2/logs/httpd.pid overwritten &#8212; Unclean shutdown of previous Apache run?<br />
[Sun Jul 19 17:06:00 2009] [warn] pid file I:/win7_amp/Apache2/logs/httpd.pid overwritten &#8212; Unclean shutdown of previous Apache run?<br />
[Sun Jul 19 17:06:04 2009] [warn] pid file I:/win7_amp/Apache2/logs/httpd.pid overwritten &#8212; Unclean shutdown of previous Apache run?<br />
[Sun Jul 19 19:00:56 2009] [warn] pid file I:/win7_amp/Apache2/logs/httpd.pid overwritten &#8212; Unclean shutdown of previous Apache run?</p></blockquote>
<p>注释掉<strong>LoadModule php5_module I:/win7_amp/php/php5apache2_2.dll</strong>后就能顺利启动，看来是加载这个模块后出了问题。但是我未能找到解决办法，以后在win7 rc 7100中都能正常使用:(，不知道有没有朋友和我一样遇到此类问题。</p>
<p>google<strong> httpd.pid overwritten &#8212; Unclean shutdown of previous Apache run?</strong>后倒是有些发现，但大家都是删除httpd.pid这个文件后重启就OK了，win7下无效。</p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6368/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>mp3插放器插件,需要的拿去用吧</title>
		<link>http://yibin.us/archives/6348</link>
		<comments>http://yibin.us/archives/6348#comments</comments>
		<pubDate>Sun, 28 Jun 2009 03:41:47 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6348</guid>
		<description><![CDATA[模仿一个runcode插件改写的，wp的插件写起来还是比较简单的。 播放器是用豆瓣的，你要不喜欢可以换成别的，就需要自己改代码了。 调用方法【mp3】xxxxx【/mp3】，全角换成半角的就行了。 代码如下： 上传到wp的插件目录，然后在后台启用就行了]]></description>
			<content:encoded><![CDATA[<p>模仿一个runcode插件改写的，wp的插件写起来还是比较简单的。</p>
<p>播放器是用豆瓣的，你要不喜欢可以换成别的，就需要自己改代码了。</p>
<p>调用方法【mp3】xxxxx【/mp3】，全角换成半角的就行了。</p>
<p>代码如下：</p>
<pre class="brush: php; title: ; notranslate">
&amp;lt;?php
/*
Plugin Name: mp3player
Plugin URI: http://yibin.us/archives/6348
Description: mp3player in a post
Version: 1.0
Author: yibin
Author URI: http://yibin.us
*/

$player = new player();
add_filter('the_content', array(&amp;amp;$player, 'convert'), -500);
unset($player);
class player
{
function __builderplayer($url) {
if(empty($url)) return '';
return sprintf('&amp;lt;p&amp;gt;&amp;lt;embed src=&amp;quot;http://www.douban.com/swf/player.swf?url=%s&amp;amp;amp;autoplay=0&amp;quot; type=&amp;quot;application/x-shockwave-flash&amp;quot; wmode=&amp;quot;transparent&amp;quot; allowscriptaccess=&amp;quot;always&amp;quot; width=&amp;quot;400&amp;quot; height=&amp;quot;80&amp;quot; /&amp;gt;&amp;lt;/p&amp;gt;',$url);
}
function convert($content)
{

$str_pattern = &amp;quot;/\[mp3\](.*?)\[\/mp3\]/i&amp;quot;;
$content = preg_replace($str_pattern,$this-&amp;gt;__builderplayer('\\1'),$content);
return $content;
}
}
?&amp;gt;
</pre>
<p>上传到wp的插件目录，然后在后台启用就行了</p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6348/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>我承认我在DZ上有RPWT</title>
		<link>http://yibin.us/archives/6325</link>
		<comments>http://yibin.us/archives/6325#comments</comments>
		<pubDate>Mon, 22 Jun 2009 08:13:02 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[discuz]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6325</guid>
		<description><![CDATA[操作cdb_sessions表，发现大量的locked，直接后果就是mysql无法响应WEB的请求，WEB无法响应浏览器端的请求。 太壮观： 最为BT的是，这些locked形成之后，必须重启mysql才行]]></description>
			<content:encoded><![CDATA[<p>操作cdb_sessions表，发现大量的locked，直接后果就是mysql无法响应WEB的请求，WEB无法响应浏览器端的请求。</p>
<p><a href="http://yibin.us/wp-content/uploads/2009/06/ScreenShot00019.jpg"><img class="alignnone size-medium wp-image-6326" title="ScreenShot00019" src="http://yibin.us/wp-content/uploads/2009/06/ScreenShot00019-300x203.jpg" alt="ScreenShot00019" width="300" height="203" /></a></p>
<p><a href="http://yibin.us/wp-content/uploads/2009/06/ScreenShot00020.jpg"><img class="alignnone size-medium wp-image-6327" title="ScreenShot00020" src="http://yibin.us/wp-content/uploads/2009/06/ScreenShot00020-300x197.jpg" alt="ScreenShot00020" width="300" height="197" /></a></p>
<p>太壮观：</p>
<pre class="brush: sql; title: ; notranslate">
+-------+------+-----------+--------+---------+------+--------+------------------------------------------------------------------------------------------------------+
| Id    | User | Host      | db     | Command | Time | State  | Info                                                                                                 |
+-------+------+-----------+--------+---------+------+--------+------------------------------------------------------------------------------------------------------+
| 84082 | root | localhost | discuz | Query   |   54 | update | INSERT INTO  cdb_sessions (sid, ip1, ip2, ip3, ip4, uid, username, groupid, styleid, invisible, acti |
| 84083 | root | localhost | discuz | Query   |   54 | Locked | DELETE FROM  cdb_sessions WHERE sid='Z71z19' OR lastactivity&lt;(1245657270-900) OR ('0'&lt;&gt;'0' AND uid=' |
| 84084 | root | localhost | discuz | Query   |   54 | Locked | SELECT uid, username, groupid, invisible, action, lastactivity, fid FROM cdb_sessions WHERE uid &lt;&gt; 0 |
| 84086 | root | localhost | discuz | Query   |   54 | Locked | SELECT uid, username, groupid, invisible, action, lastactivity, fid FROM cdb_sessions WHERE uid &lt;&gt; 0 |
| 84085 | root | localhost | discuz | Query   |   54 | Locked | SELECT uid, username, groupid, invisible, action, lastactivity, fid FROM cdb_sessions WHERE uid &lt;&gt; 0 |
| 84087 | root | localhost | discuz | Query   |   54 | Locked | SELECT uid, username, groupid, invisible, action, lastactivity, fid FROM cdb_sessions WHERE uid &lt;&gt; 0 |
| 84088 | root | localhost | discuz | Query   |   54 | Locked | SELECT uid, username, groupid, invisible, action, lastactivity, fid FROM cdb_sessions WHERE uid &lt;&gt; 0 |
| 84089 | root | localhost | discuz | Query   |   54 | Locked | SELECT uid, username, groupid, invisible, action, lastactivity, fid FROM cdb_sessions WHERE uid &lt;&gt; 0 |
| 84090 | root | localhost | discuz | Query   |   54 | Locked | SELECT uid, username, groupid, invisible, action, lastactivity, fid FROM cdb_sessions WHERE uid &lt;&gt; 0 |
| 84091 | root | localhost | discuz | Query   |   54 | Locked | SELECT uid, username, groupid, invisible, action, lastactivity, fid FROM cdb_sessions WHERE uid &lt;&gt; 0 |
| 84092 | root | localhost | discuz | Query   |   54 | Locked | SELECT uid, username, groupid, invisible, action, lastactivity, fid FROM cdb_sessions WHERE uid &lt;&gt; 0 |
| 84093 | root | localhost | discuz | Query   |   54 | Locked | DELETE FROM  cdb_sessions WHERE sid='wsX92s' OR lastactivity&lt;(1245657270-900) OR ('0'&lt;&gt;'0' AND uid=' |
| 84095 | root | localhost | discuz | Query   |   54 | Locked | SELECT uid, username, groupid, invisible, action, lastactivity, fid FROM cdb_sessions WHERE uid &lt;&gt; 0 |
| 84096 | root | localhost | discuz | Query   |   54 | Locked | DELETE FROM  cdb_sessions WHERE sid='Ra5ZqL' OR lastactivity&lt;(1245657270-900) OR ('0'&lt;&gt;'0' AND uid=' |
| 84094 | root | localhost | discuz | Query   |   54 | Locked | SELECT uid, username, groupid, invisible, action, lastactivity, fid FROM cdb_sessions WHERE uid &lt;&gt; 0 |
| 84097 | root | localhost | discuz | Query   |   54 | Locked | SELECT sid, uid AS sessionuid, groupid, groupid='6' AS ipbanned, pageviews AS spageviews, styleid, l |
| 84100 | root | localhost | discuz | Query   |   54 | Locked | SELECT sid, uid AS sessionuid, groupid, groupid='6' AS ipbanned, pageviews AS spageviews, styleid, l |
| 84099 | root | localhost | discuz | Query   |   54 | Locked | SELECT uid, username, groupid, invisible, action, lastactivity, fid FROM cdb_sessions WHERE uid &lt;&gt; 0 |
</pre>
<p><span style="color: #ff0000;"><strong>最为BT的是，这些locked形成之后，必须重启mysql才行</strong></span></p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6325/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>DZ7在windows下的表现</title>
		<link>http://yibin.us/archives/6314</link>
		<comments>http://yibin.us/archives/6314#comments</comments>
		<pubDate>Fri, 19 Jun 2009 00:48:01 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[Develop]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6314</guid>
		<description><![CDATA[Discuz7的性能问题一直没能得到很好的解决， 具体表现为在linux+apache环境下，做压力测试时总是当机，要么是mysql show processlist出现众多locked，致使mysql当机；要么是apache不响应，直接当掉。 昨天配置到linux+nginx中，效果还是一样，囧得要命~~~ linux我不知道怎么玩，只能交由平台运维部门的同事全权处理，所以有关的配置也只是他们最清楚了。 无奈之下，昨天晚上对我机器上的discuz做了压测，结果在10个多小时的持续测试中，未出现一个错误，而且Hits/sec总是保持在650左右。 并发人数控制如下(出于我机器性能考虑，并发人数不大)： 100人浏览首页、50人发帖、50人查看帖子列表，这样的压力持续10小时。]]></description>
			<content:encoded><![CDATA[<p>Discuz7的性能问题一直没能得到很好的解决，</p>
<p>具体表现为在linux+apache环境下，做压力测试时总是当机，要么是mysql show processlist出现众多locked，致使mysql当机；要么是apache不响应，直接当掉。</p>
<p>昨天配置到linux+nginx中，效果还是一样，囧得要命~~~</p>
<p>linux我不知道怎么玩，只能交由平台运维部门的同事全权处理，所以有关的配置也只是他们最清楚了。</p>
<p>无奈之下，昨天晚上对我机器上的discuz做了压测，结果在10个多小时的持续测试中，未出现一个错误，而且Hits/sec总是保持在650左右。</p>
<p style="text-align: center;"><img class="size-full wp-image-6315 aligncenter" title="lr1" src="http://yibin.us/wp-content/uploads/2009/06/ScreenShot00017.jpg" alt="lr1" width="428" height="255" /></p>
<p>并发人数控制如下(出于我机器性能考虑，并发人数不大)：</p>
<p>100人浏览首页、50人发帖、50人查看帖子列表，这样的压力持续10小时。</p>
<div id="attachment_6316" class="wp-caption aligncenter" style="width: 310px"><a href="http://yibin.us/wp-content/uploads/2009/06/ScreenShot00018.jpg"><img class="size-medium wp-image-6316" title="点击看大图" src="http://yibin.us/wp-content/uploads/2009/06/ScreenShot00018-300x106.jpg" alt="点击看大图" width="300" height="106" /></a><p class="wp-caption-text">点击看大图</p></div>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6314/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>真正兼容.net的php 3DES加解密</title>
		<link>http://yibin.us/archives/6307</link>
		<comments>http://yibin.us/archives/6307#comments</comments>
		<pubDate>Mon, 15 Jun 2009 08:16:31 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[des]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6307</guid>
		<description><![CDATA[php中3des加密的结果与.Net/java不同的帖子与话题实在是太多了， 我前不久也在倒腾这些，不过今天已经搞定了，完全与.net中的兼容 直接拿去用吧。]]></description>
			<content:encoded><![CDATA[<p>php中3des加密的结果与.Net/java不同的帖子与话题实在是太多了，</p>
<p>我前不久也在倒腾这些，不过今天已经搞定了，完全与.net中的兼容</p>
<pre class="brush: php; title: ; notranslate">
&amp;lt;?php
class Crypt3Des
{
private $key = &amp;quot;&amp;quot;;
private $iv = &amp;quot;&amp;quot;;
/**
* 构造，传递二个已经进行base64_encode的KEY与IV
*
* @param string $key
* @param string $iv
*/
function __construct ($key, $iv)
{
if (empty($key) || empty($iv)) {
echo 'key and iv is not valid';
exit();
}
$this-&amp;gt;key = $key;
$this-&amp;gt;iv = $iv;
}
/**
*加密
* @param &amp;lt;type&amp;gt; $value
* @return &amp;lt;type&amp;gt;
*/
public function encrypt ($value)
{
$td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
$iv = base64_decode($this-&amp;gt;iv);
$value = $this-&amp;gt;PaddingPKCS7($value);
$key = base64_decode($this-&amp;gt;key);
mcrypt_generic_init($td, $key, $iv);
$ret = base64_encode(mcrypt_generic($td, $value));
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $ret;
}
/**
*解密
* @param &amp;lt;type&amp;gt; $value
* @return &amp;lt;type&amp;gt;
*/
public function decrypt ($value)
{
$td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
$iv = base64_decode($this-&amp;gt;iv);
$key = base64_decode($this-&amp;gt;key);
mcrypt_generic_init($td, $key, $iv);
$ret = trim(mdecrypt_generic($td, base64_decode($value)));
$ret = $this-&amp;gt;UnPaddingPKCS7($ret);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $ret;
}
private function PaddingPKCS7 ($data)
{
$block_size = mcrypt_get_block_size('tripledes', 'cbc');
$padding_char = $block_size - (strlen($data) % $block_size);
$data .= str_repeat(chr($padding_char), $padding_char);
return $data;
}
private function UnPaddingPKCS7 ($text)
{
$pad = ord($text{strlen($text) - 1});
if ($pad &amp;gt; strlen($text)) {
return false;
}
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
return false;
}
return substr($text, 0, - 1 * $pad);
}
}
?&amp;gt;
</pre>
<pre>直接拿去用吧。</pre>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6307/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>UCenter简析</title>
		<link>http://yibin.us/archives/6275</link>
		<comments>http://yibin.us/archives/6275#comments</comments>
		<pubDate>Sun, 10 May 2009 08:34:38 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ucenter]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6275</guid>
		<description><![CDATA[UCenter是采用很经典的MVC架构   UCenter采用index.php单点入口 假定我们检测用户登录，那么对应的接口址就为： xxx/index.php?m=user&#38;a=login 这样，就会初始化usercontrol并调用onlogin方法，看看usercontrol的onlogin() model层直接与数据库交互，control层与model层交互，得到结果，返回给view。 如果我们要扩展自己的方法该怎么办？ 首先，增加model类，定义我们所需要的方法，直接与DB进行交互。 其次，增加control类，并继承自base，定义on{xxxx}方法，与相对的model交互，并返回结果。]]></description>
			<content:encoded><![CDATA[<p>UCenter是采用很经典的MVC架构</p>
<p><img class="alignnone" title="mvc" src="http://pic.yupoo.com/samyi/5587076a0529/medium.jpg" alt="" width="500" height="472" /></p>
<p> <span id="more-6275"></span></p>
<p>UCenter采用index.php单点入口</p>
<pre class="brush: php; title: ; notranslate">
$m = getgpc('m'); //判断加载哪一个Model
$a = getgpc('a'); //判断加载哪一个Control
$release = getgpc('release');
if(empty($m) &amp;&amp; empty($a)) { //如果直接访问则跳转到管理界面
header('Location: admin.php');
exit;
}

//版本判断
if(empty($release)) {
define('RELEASE_ROOT', &quot;release/20080429/&quot;);
} elseif(intval($release) != UC_SERVER_RELEASE) {
define('RELEASE_ROOT', &quot;release/$release/&quot;);
} else {
define('RELEASE_ROOT', '');
}
//加载基类，基类中定义了很多的方法，所有控制器类都继承于该基类
if(file_exists(UC_ROOT.RELEASE_ROOT.'model/base.php')) {
require UC_ROOT.RELEASE_ROOT.'model/base.php';
} else {
require UC_ROOT.'model/base.php';
}
//判断是否加载的是指定的model
if(in_array($m, array('app', 'frame', 'user', 'pm', 'pm_client', 'tag', 'feed', 'friend', 'domain', 'credit', 'mail', 'version'))) {
if(file_exists(UC_ROOT.RELEASE_ROOT.&quot;control/$m.php&quot;)) {
include UC_ROOT.RELEASE_ROOT.&quot;control/$m.php&quot;;
} else {
include UC_ROOT.&quot;control/$m.php&quot;;
}
$classname = $m.'control';  //构造control类，如$m=user时control就为usercontrol
$control = new $classname();
$method = 'on'.$a;          //构造方法名

if(method_exists($control, $method) &amp;&amp; $a{0} != '_') {  //如果方法存在且不是私有方法则执行该方法
$data = $control-&gt;$method();
echo is_array($data) ? $control-&gt;serialize($data, 1) : $data;
exit;
} elseif(method_exists($control, '_call')) {  //否则执行重载
$data = $control-&gt;_call('on'.$a, '');
echo is_array($data) ? $control-&gt;serialize($data, 1) : $data;
exit;
} else {
exit('Action not found!');
}
} else {
exit('Module not found!');
}
</pre>
<p>假定我们检测用户登录，那么对应的接口址就为：<br />
xxx/index.php?m=user&amp;a=login<br />
这样，就会初始化usercontrol并调用onlogin方法，看看usercontrol的onlogin()</p>
<pre class="brush: php; title: ; notranslate"> //部分代码，有所删减
class usercontrol extends base {
function __construct() {
$this-&gt;usercontrol();
}
function usercontrol() {
parent::__construct();    //执行基类构造函数
$this-&gt;load('user');   //加载user model,base类的load方法首先从$_ENV环境变量中查找是否已设置$_ENV['user'],若未设置，则加载model/user.php，然后实例化该类并写入环境变量中，也就是此时，user model已经实例化了。
}
function onlogin() {
$this-&gt;init_input();  //调用基类init_input()方法
$isuid = $this-&gt;input('isuid');  //从基类中获取isuid
$username = $this-&gt;input('username');  //获取用户名
$password = $this-&gt;input('password');   //获取密码
$checkques = $this-&gt;input('checkques');　　//获取是否回答提示问题　
$questionid = $this-&gt;input('questionid');　//获取提示问题ID
$answer = $this-&gt;input('answer');　　//获取回答
if($isuid) {   //判断是用UID登录还是用用户名登录
$user = $_ENV['user']-&gt;get_user_by_uid($username);  //调用/model/user.php中的get_user_by_id方法从数据库中获取该用户的实例
} else {
$user = $_ENV['user']-&gt;get_user_by_username($username);
}

$passwordmd5 = preg_match('/^\w{32}$/', $password) ? $password : md5($password);
//判断各种可能的结果
if(empty($user)) {
$status = -1;
} elseif($user['password'] != md5($passwordmd5.$user['salt'])) {
$status = -2;
} elseif($checkques &amp;&amp; $user['secques'] != '' &amp;&amp; $user['secques'] != $_ENV['user']-&gt;quescrypt($questionid, $answer)) {
$status = -3;
} else {
$status = $user['uid'];
}
$merge = $status != -1 &amp;&amp; !$isuid &amp;&amp; $_ENV['user']-&gt;check_mergeuser($username) ? 1 : 0;
return array($status, $user['username'], $password, $user['email'], $merge);
}
</pre>
<p>model层直接与数据库交互，control层与model层交互，得到结果，返回给view。<br />
如果我们要扩展自己的方法该怎么办？<br />
首先，增加model类，定义我们所需要的方法，直接与DB进行交互。<br />
其次，增加control类，并继承自base，定义on{xxxx}方法，与相对的model交互，并返回结果。</p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6275/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>php.js</title>
		<link>http://yibin.us/archives/6243</link>
		<comments>http://yibin.us/archives/6243#comments</comments>
		<pubDate>Mon, 04 May 2009 09:19:39 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6243</guid>
		<description><![CDATA[嗯，这个标题没有错。 以前有个同事写了个asp版的Cute ASP Framework，封装了一些很实用的函数。 后来我们谈到php中的一些内置函数，他也曾有过将其翻译成asp版的想法。 没想到，有人居然把php中内置的函数直接翻成了js。 如php中的strcmp，JS版的： 看这里： http://phpjs.org/functions/index 这个太NB了。]]></description>
			<content:encoded><![CDATA[<p>嗯，这个标题没有错。</p>
<p>以前有个同事写了个asp版的<a href="http://www.terranc.cn/blog/Article/development/CuteAspFramework.html" target="_blank">Cute ASP Framework</a>，封装了一些很实用的函数。</p>
<p>后来我们谈到php中的一些内置函数，他也曾有过将其翻译成asp版的想法。</p>
<p>没想到，有人居然把php中内置的函数直接翻成了js。</p>
<p>如php中的strcmp，JS版的：</p>
<pre class="brush: jscript; title: ; notranslate">
function strcmp ( str1, str2 ) {
// Binary safe string comparison
//
// version: 812.316
// discuss at: http://phpjs.org/functions/strcmp
// +   original by: Waldo Malqui Silva
// +      input by: Steve Hilder
// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// +    revised by: gorthaur
// *     example 1: strcmp( 'waldo', 'owald' );
// *     returns 1: 1
// *     example 2: strcmp( 'owald', 'waldo' );
// *     returns 2: -1
return ( ( str1 == str2 ) ? 0 : ( ( str1 &amp;gt; str2 ) ? 1 : -1 ) );
}
</pre>
<p>看这里：</p>
<p><a href="http://phpjs.org/functions/index" target="_blank">http://phpjs.org/functions/index</a></p>
<p>这个太NB了。</p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6243/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>寻Apache2下Discuz高性能配置方案</title>
		<link>http://yibin.us/archives/6096</link>
		<comments>http://yibin.us/archives/6096#comments</comments>
		<pubDate>Tue, 28 Apr 2009 13:42:55 +0000</pubDate>
		<dc:creator>幻想曲</dc:creator>
				<category><![CDATA[Develop]]></category>
		<category><![CDATA[discuz]]></category>
		<category><![CDATA[Others]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://yibin.us/?p=6096</guid>
		<description><![CDATA[服务器品牌 DELL 服务器型号 PE1850 cpu类型 Intel Xeon 2.80GHz cpu个数 2 内存条类型 512M 内存条个数 2 硬盘类型1 36G 硬盘个数1 1 操作系统：Linux centos2 2.6.18-92.el5 Apache 2.0 PHP 5.2.9 目前300并发，持续时间1小时 测试截图: 已安装Discuz7.0版，目前论坛并无帖子。 下午进行LoadRunner测试， 100用户并发时，响应时间在1.5s左右。 400用户并发时，响应时间约为3s，但每秒连接数一直处于170左右，无法继续上升。 此时，web服务器CPU占用70%-80% Db服务器为2%，基本无压力。 在网上看到一些文章，Apache在配置正确的情况下基本上可以处理每秒2K的并发请求，但今天的测试让我大跌眼镜。 响应时间也不够理想，况且这次的测试只是测了首页的访问，而且首页是做了缓存处理的。 最开始是怀疑httpd.conf中最大连接数没有设置正确，改动后重启Apache依然如此。 最令人担忧的是居高不下的CPU占用率。 希望有在Apache下配置Discuz经验的大虾们能给一个合理的配置方案。]]></description>
			<content:encoded><![CDATA[<p>服务器品牌  DELL<br />
服务器型号  PE1850<br />
cpu类型  Intel Xeon 2.80GHz<br />
cpu个数  2<br />
内存条类型  512M<br />
内存条个数  2<br />
硬盘类型1  36G<br />
硬盘个数1  1</p>
<p>操作系统：Linux centos2 2.6.18-92.el5<br />
Apache 2.0<br />
PHP 5.2.9</p>
<p>目前300并发，持续时间1小时<br />
测试截图:</p>
<p><a class="thickbox" href="http://bbs.phpchina.com/attachments/month_0904/09042911287b8c1b085214064d.png"><img class="img" src="http://bbs.phpchina.com/attachments/month_0904/09042911287b8c1b085214064d.png" alt="" /></a><br />
已安装Discuz7.0版，目前论坛并无帖子。</p>
<p>下午进行LoadRunner测试，<br />
100用户并发时，响应时间在1.5s左右。<br />
400用户并发时，响应时间约为3s，但每秒连接数一直处于170左右，无法继续上升。<br />
此时，web服务器CPU占用70%-80%<br />
Db服务器为2%，基本无压力。</p>
<p>在网上看到一些文章，Apache在配置正确的情况下基本上可以处理每秒2K的并发请求，但今天的测试让我大跌眼镜。<br />
响应时间也不够理想，况且这次的测试只是测了首页的访问，而且首页是做了缓存处理的。<br />
最开始是怀疑httpd.conf中最大连接数没有设置正确，改动后重启Apache依然如此。<br />
最令人担忧的是居高不下的CPU占用率。</p>
<p>希望有在Apache下配置Discuz经验的大虾们能给一个合理的配置方案。</p>
]]></content:encoded>
			<wfw:commentRss>http://yibin.us/archives/6096/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

