[Phpfreechat-svn] SF.net SVN: phpfreechat: [484] trunk/testcase
Status: Beta
Brought to you by:
kerphi
From: <ke...@us...> - 2006-04-29 17:57:29
|
Revision: 484 Author: kerphi Date: 2006-04-29 10:57:20 -0700 (Sat, 29 Apr 2006) ViewCVS: http://svn.sourceforge.net/phpfreechat/?rev=484&view=rev Log Message: ----------- optimization: add a cache for the nickname list Modified Paths: -------------- trunk/src/containers/file.class.php trunk/testcase/container_generic.php Modified: trunk/src/containers/file.class.php =================================================================== --- trunk/src/containers/file.class.php 2006-04-28 22:34:41 UTC (rev 483) +++ trunk/src/containers/file.class.php 2006-04-29 17:57:20 UTC (rev 484) @@ -29,8 +29,8 @@ */ class pfcContainer_File extends pfcContainer { - // var $_users = array(); - //var $_cache_nickid = array(); + var $_users = array(); + var $_cache_nickid = array(); function pfcContainer_File(&$config) { @@ -118,10 +118,6 @@ flock ($fp, LOCK_UN); // unlock fclose($fp); - - // if (!in_array($nickname, $this->_users)) - // $this->_users[] = $nickname; // _users will be used by getOnlineUserList - return true; } @@ -150,14 +146,13 @@ @unlink($nick_filename); - /* // remove the nickname from the cache list - if (in_array($nick, $this->_users)) + if (isset($this->_users[$chan]) && + in_array($nick, $this->_users[$chan])) { - $key = array_search($nick, $this->_users); - unset($this->_users[$key]); + $key = array_search($nick, $this->_users[$chan]); + unset($this->_users[$chan][$key]); } - */ return true; } @@ -182,6 +177,13 @@ if (file_exists($nick_filename)) $there = true; @touch($nick_filename); @chmod($nick_filename, 0777); + + // append the nickname to the cache list + if (isset($this->_users[$chan]) && + !in_array($nick, $this->_users[$chan])) + { + $this->_users[$chan][] = $nick; + } return $there; } @@ -204,6 +206,20 @@ $oldnick_filename = $nick_dir."/".$this->_encode($oldnick); $ok = @rename($oldnick_filename, $newnick_filename); + + // update the nick cache list + if($ok) + { + if (isset($this->_users[$chan]) && + in_array($oldnick, $this->_users[$chan])) + { + // remove the oldnick from the cache + $key = array_search($oldnick, $this->_users[$chan]); + unset($this->_users[$chan][$key]); + // append the new nick to the cache + $this->_users[$chan][] = $newnick; + } + } return $ok; } @@ -216,33 +232,31 @@ */ function getNickId($nickname) { - //if (!isset($this->_cache_nickid[$nickname])) - //{ - $c =& $this->c; - $nickid = "undefined"; - - $nick_dir = $c->container_cfg_server_dir."/nicknames"; - $nick_filename = $nick_dir."/".$this->_encode($nickname); - - if (file_exists($nick_filename)) + if (!isset($this->_cache_nickid[$nickname])) { - $fsize = filesize($nick_filename); - if ($fsize>0) + $c =& $this->c; + $nickid = "undefined"; + + $nick_dir = $c->container_cfg_server_dir."/nicknames"; + $nick_filename = $nick_dir."/".$this->_encode($nickname); + + if (file_exists($nick_filename)) { - // write the nickid into the new nickname file - $fp = fopen($nick_filename, "r"); - $nickid = fread($fp, $fsize); - if ($nickid == "") $nickid = "undefined"; - fclose($fp); + $fsize = filesize($nick_filename); + if ($fsize>0) + { + // write the nickid into the new nickname file + $fp = fopen($nick_filename, "r"); + $nickid = fread($fp, $fsize); + if ($nickid == "") $nickid = "undefined"; + fclose($fp); + } } + $this->_cache_nickid[$nickname] = $nickid; } - //$this->_cache_nickid[$nickname] = $nickid; - //if ($c->debug) pxlog("getNickId[".$c->sessionid."]: nickname=".$nickname." nickid=".$nickid, "chat", $c->getId()); - //} - return $nickid; //$this->_cache_nickid[$nickname]; + return $this->_cache_nickid[$nickname]; } - /** * Remove (disconnect/quit) the timeouted nickname from the server or from a channel * Notice: this function must remove all nicknames which are not uptodate from the given channel or from the server @@ -278,7 +292,8 @@ } } - // $this->_users =& $users; // _users will be used by getOnlineUserList + // cache the updated user list + $this->_users[$chan] =& $users; return $deleted_user; } @@ -290,8 +305,10 @@ */ function getOnlineNick($chan) { - // if (is_array($this->_users)) - // return $this->_users; + // return the cached user list if it exists + if (isset($this->_users[$chan]) && + is_array($this->_users[$chan])) + return $this->_users[$chan]; $c =& $this->c; @@ -306,6 +323,10 @@ if ($file == "." || $file == "..") continue; // skip . and .. generic files $users[] = $this->_decode($file); } + + // cache the user list + $this->_users[$chan] =& $users; + return $users; } Modified: trunk/testcase/container_generic.php =================================================================== --- trunk/testcase/container_generic.php 2006-04-28 22:34:41 UTC (rev 483) +++ trunk/testcase/container_generic.php 2006-04-29 17:57:20 UTC (rev 484) @@ -177,27 +177,27 @@ // on the channel $this->ct->createNick($chan, $nick, $nickid); $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "nickname should be online on the channel"); + $this->assertTrue(in_array($nick, $online_nick), "1-nickname should be online on the channel"); sleep(2); $ret = $this->ct->updateNick($chan, $nick); - $this->assertTrue($ret, "nickname should be correctly updated on the channel"); + $this->assertTrue($ret, "2-nickname should be correctly updated on the channel"); $ret = $this->ct->removeObsoleteNick($chan, "1000"); - $this->assertFalse(in_array($nick, $ret), "nickname should not be removed from the channel because it has been updated"); + $this->assertFalse(in_array($nick, $ret), "3-nickname should not be removed from the channel because it has been updated"); $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "nickname should be online on the channel"); + $this->assertTrue(in_array($nick, $online_nick), "4-nickname should be online on the channel"); // on the server $chan = NULL; $this->ct->createNick($chan, $nick, $nickid); $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "nickname should be online on the server"); + $this->assertTrue(in_array($nick, $online_nick), "5-nickname should be online on the server"); sleep(2); $ret = $this->ct->updateNick($chan, $nick); - $this->assertTrue($ret, "nickname should be correctly updated on the server"); + $this->assertTrue($ret, "6-nickname should be correctly updated on the server"); $ret = $this->ct->removeObsoleteNick($chan, "1000"); - $this->assertFalse(in_array($nick, $ret), "nickname should not be removed from the server because it has been updated"); + $this->assertFalse(in_array($nick, $ret), "7-nickname should not be removed from the server because it has been updated"); $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "nickname should be online on the server"); + $this->assertTrue(in_array($nick, $online_nick), "8-nickname should be online on the server"); } function testchangeNick_Generic() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |