WarDrome Blog

Give me a mysql connection and I will move the galaxy.

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.

// 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.

3 pensieri su “Give me a mysql connection and I will move the galaxy.

  1. Pingback: dev/null » Blog Archive » Classe DB mysql con supporto di memcached.

  2. Pingback: Wardrome Blog » Browser Games Development School

  3. Pingback: Tweets that mention Wardrome Blog » Give me a mysql connection and I will move the galaxy. -- Topsy.com

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Optionally add an image (JPEG only)

Cookies help us deliver our services. By using our services, you agree to our use of cookies. More Info | Close