PHP Connect Redis Driver
PHPConnectRedisDriver
IwasusingPredisbefore.Butitseemsthatourcompanyisusinganotheroneinthecommoncodes.SoIplantofollowthatandusethatoneinstead.
https://github.com/phpredis/phpredis#readme
Installation
>gitclonehttps://github.com/phpredis/phpredis
>cdphpredis/
>phpize
>./configure
>make
>sudomakeinstall
Afterinstallation,wecantrytheseclassesasfollow:
<?php
namespaceBillingConsumerPHP;
require__DIR__.'/../../vendor/autoload.php';
use\Redis;
classPHPRedisClient
{
private$client=null;
private$ioc=null;
publicfunction__construct($ioc)
{
$this->ioc=$ioc;
$logger=$this->ioc->getService("logger");
$config=$this->ioc->getService("config");
$logger->info("==============Redisconfigstart==============");
$logger->info("redisHost=".$config['redisHost']);
$logger->info("redisPort=".$config['redisPort']);
$logger->info("===============================================");
try
{
$this->client=newRedis();
$this->client->pconnect($config['redisHost'],$config['redisPort'],0.5);
$logger->debug("SuccessfullyconnectedtoRedis");
}
catch(Exception$e)
{
$logger->error("Couldn'tconnectedtoRedis");
$logger->error($e->getMessage());
}
}
publicfunctionisRecoverNeeded(){
$max=$this->client->get("job_id_counter");
//ifcounteris0orempty,redisalreadycrashed.
return$max<=4294967295;
}
publicfunctionincrJobIDCounter(){
return$this->client->incr("job_id_counter");
}
publicfunctiongetJobIDCounter(){
return$this->client->get("job_id_counter");
}
publicfunctionsetJobIDCounter($number){
$this->client->set("job_id_counter",$number);
}
publicfunctioncleanJobIDCounter(){
$this->client->del("job_id_counter");
}
publicfunctionincrAndFetchSpend($jobID,$date,$spend){
$dateTimeUtil=$this->ioc->getService("dateTimeUtil");
//lifetimespend
$lifeTimeSpend=$this->client->hincrby("job_budget_{$jobID}",'lifetimespend',$spend);
//dailyspend
$dateString=$dateTimeUtil->getDateString($date);
$dailySpend=$this->client->hincrby("job_dailyspend_{$jobID}",$dateString,$spend);
//monthlyspend
$monthString=$dateTimeUtil->getMonthString($date);
$monthlySpend=$this->client->hincrby("job_monthlyspend_{$jobID}",$monthString,$spend);
returnarray(
'lifeTimeSpend'=>$lifeTimeSpend,
'dailySpend'=>$dailySpend,
'monthlySpend'=>$monthlySpend,
);
}
publicfunctionupdateJobIDMapping($jobIDMappings){
foreach($jobIDMappingsas$jobIDMap){
$sourceID=$jobIDMap['source_id'];
$referenceID=$jobIDMap['job_reference'];
$jobID=$jobIDMap['job_id'];
//step1sourceID_referenceID=>jobID=>value
$jobSourceKey="job_{$sourceID}_{$referenceID}";
$columnname="job_id";
$this->client->hset($jobSourceKey,$columnName,$jobID);
//step2jobID=>sourceID_referenceID
$redisKey="idmapping_{$jobID}";
$redisValue="{$sourceID}_{$referenceID}";
$this->client->set($redisKey,$redisValue);
}
}
publicfunctiongetJobIDMapping($jobID)
{
$redisKey="idmapping_{$jobID}";
$redisValue=$this->client->get($key);
if(null!==$redisValue){
$infos=explode("_",$redisValue);
if(count($infos)>1){
returnarray(
'sourceID'=>$infos[0],
'referenceID'=>$infos[1],
);
}else{
returnnull;
}
}else{
returnnull;
}
}
publicfunctionupdateHistoryDailySpending($spendings){
foreach($spendingsas$spend){
$jobID=$spend['job_id'];
$redisKey="job_dailyspend_{$jobID}";
$redisColumn=$spend['date'];
$redisValue=$spend['spend'];//mcents
$this->client->hincrbyfloat($redisKey,$redisColumn,$redisValue);
}
}
publicfunctionupdateHistoryMonthlySpending($spendings){
foreach($spendingsas$spend){
$jobID=$spend['job_id'];
$redisValue=$spend['spend']*1000;//mcents
$month=$spend['month'];
$redisKey="job_monthlyspend_{$jobID}";
$redisColumn=$month;
$this->client->hincrbyfloat($redisKey,$redisColumn,$redisValue);
}
}
publicfunctiongetBudget($jobID){
$result=$this->client->hmget("job_budget_{$jobID}",array('budget','budgetType'));
return$result;
}
publicfunctionsendRawJobContent($key,$message){
$logger=$this->ioc->getService("logger");
//sendtoredis
$this->client->setEx($key,TIME_3DAY,$message);
}
}
?>
Thisclasscanbetestedbyphpunit
<?php
use\BillingConsumerPHP\IOCUtil;
/**
*RUNNING_ENV=testphpunit--bootstrapvendor/autoload.phptests/BillingConsumerPHP/PHPRedisClientTest
*@authorcarl
*/
classPHPRedisClientTestextendsPHPUnit_Framework_TestCase
{
private$phpRedisClient;
protectedfunctionsetUp()
{
$ioc=newIOCUtil();
$this->phpRedisClient=$ioc->getService("phpRedisClient");
}
publicfunctiontestDummy()
{
$this->assertTrue(true);
}
publicfunctiontestDailySpending(){
$jobIDs="4294967295";
$date="2016-08-22";
$spending=12.34;
$spendings=array();
for($i=0;$i<10;$i++){
$spendings[]=array(
'job_id'=>$jobIDs.$i,
'date'=>$date,
'spend'=>$spending,
);
}
$this->phpRedisClient->updateHistoryDailySpending($spendings);
}
publicfunctiontestCrash(){
$this->phpRedisClient->cleanJobIDCounter();
$result1=$this->phpRedisClient->isRecoverNeeded();
$this->assertTrue($result1);
$this->phpRedisClient->setJobIDCounter(12);
$result2=$this->phpRedisClient->isRecoverNeeded();
$this->assertTrue($result2);
$this->phpRedisClient->setJobIDCounter(42949672951);
$result3=$this->phpRedisClient->isRecoverNeeded();
$this->assertFalse($result3);
}
}
?>
Iamalwaysusingdockerimagetodeploymythings.Actually,comparingPrediswhichisnotanextension,theinstallationofextensionisalittlecomplex.
Dockerfile
#installRedisNativePHPClient
RUNyuminstall-ygit
RUNgitclonehttps://github.com/phpredis/phpredis
WORKDIR/install/phpredis
RUNphpize
RUN./configure
RUNmake
RUNmakeinstall
Andwehavetoaddextensionconfigurationinphp.ini
extension=iconv.so
extension=raphf.so
extension=propro.so
extension=http.so
extension=redis.so
References:
http://www.thegeekstuff.com/2014/02/phpredis/