Apr 13, 2012
In Drupal 6, a number of caching strategies are incorporated to handle large traffic. One of them is the serialization of the whole variable table. It is being cached in the database and gets extracted into global $conf variable in each invoke.
In one of our production sites, we faced hard time to keep up with the memory requirement of PHP for the unserialization of this variable from the cache. The variables table was so large that we had to assign around 1GB memory to each PHP thread so that the value can be unserialized without memory exhaustion. This made it much harder to scale the application.
So, we decided to do something about it and successfully handled it by doing the following:
1. First of all, we installed the memcache module to move the cache storage from DB to memory
2. We then edited the memcache module’s cache_get and cache_set functions to store/retrieve individual items from the variables array and split/join them when requested.
3. This requires a memcache call for each of the items in the variable array, but consumes a small amount of memory as there is no huge unserialize operation going on.
4. We performed a few tests to see if the site is working as before, and found its working perfectly!
Here is the code in case you are facing similar issue: Read the rest of this article »
Mar 31, 2012
I was having a discussion with a colleague regarding how to add generic JSON based representation to a number of classes without making a big effort. The immediate solution that came in my mind is to use the Traits for it (introduced in PHP 5.4).
So I wrote the following example for him:
JSONized.php
User.php
class User
{
use JSONized
;
public $username;
public $password;
public $email;
public function __construct
($username, $password, $email)
{
$this->username = $username;
$this->password = md5($password);
$this->email = $email;
}
}
test.php
include_once 'JSONized.php';
include_once 'User.php';
$emran = new User('phpfour', 'emran123', 'phpfour@gmail.com');
echo $emran->toJson();
The result of running the above code will be like this:
{
"username" : "phpfour",
"password" : "02c2ec1e3f21e97dd5ac747147a141e0",
"email" : "phpfour@gmail.com"
}
Mar 24, 2012
A few days ago I was explaining mocking to a few colleagues. I came up with the following quick n dirty code to show them the elegance of Mockery as a mocking framework.
The code is straight-forward: it checks the health of a server using a tiny heart beat script.
1. The script residing in the remote server:
// Location: /remote/heart.php
$response = array('status' => 'online');
header("Content-type: application/json");
echo json_encode($response);
2. The class that tries to fetch the status from the remote server:
// Location: /test/Heartbeat.php
class Heartbeat
{
private $http;
public function __construct
(Http
$http)
{
$this->http = $http;
}
public function checkHealth
()
{
$url = 'http://localhost/remote/heart.php';
$response = $this->http->getResponse($url);
$beat = json_decode($response);
return (!is_null($beat) && $beat->status == 'online');
}
}
Read the rest of this article »
Feb 28, 2012
Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 491520 bytes) in /home/emran/public_html/blog/wp-content/plugins/codecolorer/lib/geshi/bash.php on line 386