Wardrome is first of all a database query eater, an enormous management software that rather than deal with a company or other annoying things is here to make you play and have fun.
The first thing to be dealt with when making such an application is to make sure not to overload the database as soon as the players grows more than normal.
The common method used (in addition to slim queries) is to make a caching the results of our query for a certain period of time will be stored in order to avoid stress the database with constant requests for things we already have.
Wardrome makes extensive use of this: memcached “a distributed memory object caching system”.
What does memcached work in practice? Allows the concurrent processes (using a “lung” of shared RAM) to store any data, in our case the results of the queries.
Stop talking now, let’s watch the implementation, showing PHP code.
The first thing to be dealt with when making such an application is to make sure not to overload the database as soon as the players grows more than normal.
The common method used (in addition to slim queries) is to make a caching the results of our query for a certain period of time will be stored in order to avoid stress the database with constant requests for things we already have.
Wardrome makes extensive use of this: memcached “a distributed memory object caching system”.
What does memcached work in practice? Allows the concurrent processes (using a “lung” of shared RAM) to store any data, in our case the results of the queries.
Stop talking now, let’s watch the implementation, showing PHP code.
// override of mysqli class. class db extends mysqli { // modify the costants below with your connections settings. private $hostname = DATABASE_HOST; private $username = DATABASE_USER; private $password = DATABASE_PASSWORD; private $database = DATABASE_NAME; private $memcachedhostname= MEMCACHED_HOST; private $memcachedport=MEMCACHED_PORT; private $memcache; private $prefix; public function __construct() { $this->prefix=md5($_SERVER['SERVER_NAME']); $this->memcache=new MemCache(); $this->memcache->connect($this->memcachedhostname, $this->memcachedport); $result=parent::__construct($this->hostname, $this->username, $this->password, $this->database); if(mysqli_error($this)) { throw new exception(mysqli_error($this), mysqli_errno($this)); } return $result; } // query simple call parent. public function query($query) { $result = parent::query($query); if(mysqli_error($this)){ throw new exception(mysqli_error($this), mysqli_errno($this)); } return $result; } // select collects data in a object array. public function select($query) { $ret=Array(); $result=$this->query($query); while($o=$result->fetch_object()) $ret[]=$o; return $ret; } // same of select but with caching and expire time. public function cselect($query,$expire=3600) { $ret=Array(); $memcontrol=$this->prefix."-".md5($query); $ret=$this->memcache->get($memcontrol); if ($ret==NULL) { $ret=$this->select($query); $this->memcache->set($memcontrol, $ret,false, $expire); return $ret; }else { return $ret; } } }
That’s all, i hope you’ll find it useful.
« wardrome-2-28 10/10/10 Galaxiadi (Wardrome Olympic Games) »



3 Trackbacks / Pingbacks for this entry:
[...] mysql che supporta memcached, la trovate sul neonato Blog di Wardrome cliccando sul link seguente: Give me a mysql connection and I will move the galaxy Share and [...]
[...] I’m starting with this post my “Browser Games Develoment School“, which began some time ago, unofficially, with a post that described a PHP class to connect to mysql using memcached, [...]
[...] This post was mentioned on Twitter by wardrome, wardrome. wardrome said: http://blog.wardrome.com/2010/10/give-me-a-mysql-connection-and-i-will-move-the-galaxy/ mysql class [...]