add

Monday, October 22, 2012

memcached using php and perl


Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
The system uses a client–server architecture. The servers maintain a key–value associative array; the clients populate this array and query it. Keys are up to 250 bytes long and values can be at most 1 megabyte in size.
Clients use client side libraries to contact the servers which, by default, expose their service at port 11211. Each client knows all servers; the servers do not communicate with each other. If a client wishes to set or read the value corresponding to a certain key, the client's library first computes a hash of the key to determine the server that will be used. Then it contacts that server. The server will compute a second hash of the key to determine where to store or read the corresponding value.
Perl script for fetching a Memcached result.


use Cache::Memcached;
use Digest::MD5;
use Digest::MD5 qw(md5 md5_hex md5_base64);
my $memcli = new Cache::Memcached {
    'servers' => [ "192.168.0.27:11211" ],
    'debug' => 0
};
my $serviceID=$ARGV[0];
print "Service ID__".$serviceID." **_\n";
my $perlKey='perl_'.$serviceID;
my $result = $memcli->get($perlKey);
#print "already : ___".$result."________\n";
if($result eq "")
{
#hit a php page to set memcache values if this key does not exist.
        use LWP::UserAgent;
        #my $serviceID = 12;
        my $url= "http://192.168.0.27/chargerequest/perlcache.php";
        my $ua       = LWP::UserAgent->new();
        my $response = $ua->post( $url, { 'id' => $serviceID } );
        my $content  = $response->decoded_content();
        $result = $memcli->get($perlKey);
}
else {
        print "\n cached result \n ".$result;
}


Now the php page

include("db.php");
$dbOBJ = new DB();
$conn = $dbOBJ->connectDB();
if ($conn){
$serviceid=$_REQUEST['id'];
$qry='select id, abc, def, ghi, jkl, mno, pqr from table where id='.$id; $memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$perlKey='perl_'.$serviceid;
$cache_result = array();
//$memcache->set($perlKey, $perlString, 0, 1);
$cache_result = $memcache->get($perlKey); // Memcached object
if($cache_result)
{
// Second User Request
echo "
caching
";
$dbOBJ->print_rr($cache_result);
$demos_result=$cache_result;
}
else
{
$curser = OCIParse($conn,$qry);
OCIExecute($curser);
OCIFetchInto($curser , $values);
$demos_result[]=$values; // Results storing in array
$memcache->set($key, $demos_result, 0, 100);
$val1=$demos_result[0][0];          
$val2=$demos_result[0][1];
$val3=$demos_result[0][2];        
$val4=$demos_result[0][3];        
$val5=$demos_result[0][4];          
$val6=$demos_result[0][5];
$perlString = $val1.','. $val2.','. $val3.','. $val4.','. $val5.','. $val6;
$memcache->set($perlKey, $perlString, 0, 10);
}
}
else
echo $errorMessage='Other error';
?>




No comments:

Post a Comment