PHP Backend Application(3)Solr MySQL Cache
PHPBackendApplication(3)SolrMySQLCache
1Solr
Firstofall,addthedependency.
>./composer.pharrequiresolarium/solarium
CleanallSOLRdocuments
http://xxx.xxx.xxx.xxx:8983/job/update?stream.body=%3Cdelete%3E%3Cquery%3E*:*%3C/query%3E%3C/delete%3E&commit=true
SetupIndexer
https://sillycat.atlassian.net/wiki/display/EN/Deploy+a+New+Indexer
SolrSearchClient.phptosupportaddingdocumentstoSolrServer
<?php
namespaceJobConsumerPHP;
require__DIR__.'/../../vendor/autoload.php';
use\Solarium\Client;
use\Solarium\Exception;
classSolrSearchClient
{
protected$client=null;
protected$ioc=null;
publicfunction__construct($ioc){
$this->ioc=$ioc;
$this->client=newClient(
array(
'endpoint'=>array(
'localhost'=>array(
'host'=>‘xxx.xxx.xxx.xxx',
'port'=>8983,
'path'=>'/job',
)
)
)
);
}
publicfunctionping()
{
//setupfeaturesneededforthismethod
$logger=$this->ioc->getService("logger");
//createapingquery
$ping=$this->client->createPing();
//executethepingquery
try{
$result=$this->client->ping($ping);
$logger->debug("Pingquerysuccessful-----");
$logger->debug(var_export($result->getData(),true));
$logger->debug("--------------------------");
}catch(Exception$e){
$logger->error("Pingqueryfailed:".$e);
}
}
publicfunctionaddJobDocument($job,$commit)
{
//setupfeaturesneededforthismethod
$logger=$this->ioc->getService("logger");
//getanupdatequeryinstance
$update=$this->client->createUpdate();
//createanewdocumentforthedata
$doc=$update->createDocument();
$logger->debug("addJobDocument-----------------");
while(list($key,$value)=each($job))
{
if(is_array($value)){
//array
$logger->debug("$key=>".var_export($value,true));
}else{
//string
$logger->debug("$key=>$value");
}
$doc->addField($key,$value);
}
$logger->debug("-------------------------------");
try{
$update->addDocuments(array($doc));
if($commit)
{
$update->addCommit();
$logger->debug("committingduringadddocuments.");
}else{
$logger->debug("NOTcommittingduringadddocuments.");
}
$result=$this->client->update($update);
$logger->debug("Updatequeryexecuted---------");
$logger->debug("Querystatus:".$result->getStatus());
$logger->debug("Querytime:".$result->getQueryTime());
}catch(Exception$e){
$logger->error("Adddocumentfailed:".$e);
}
}
publicfunctiondeleteDocument($jobID)
{
//setupfeaturesneededforthismethod
$logger=$this->ioc->getService("logger");
//getanupdatequeryinstance
$update=$this->client->createUpdate();
//addthedeletequeryandacommitcommandtotheupdatequery
$update->addDeleteQuery("id:".$jobID);
$update->addCommit();
//thisexecutesthequeryandreturnstheresult
$result=$this->client->update($update);
$logger->debug("Updatequeryexecuted---------------");
$logger->debug("Querystatus:".$result->getStatus());
$logger->debug("Querytime:".$result->getQueryTime());
$logger->debug("------------------------------------");
}
}
?>
TestCaseforthatBaseClass,SolrSearchClientTest.php
<?php
use\JobConsumerPHP\IOCUtil;
classSolrSearchClientTestextendsPHPUnit_Framework_TestCase
{
protected$solrSearchClient;
protectedfunctionsetUp()
{
$ioc=newIOCUtil();
$this->solrSearchClient=$ioc->getService("solrSearchClient");
}
publicfunctiontestDummy()
{
$this->assertTrue(true);
}
publicfunctiontestPing()
{
$this->solrSearchClient->ping();
}
publicfunctiontestAddJobDocument()
{
$jobProperties=array(
"id"=>"id2",//required
"customer_id"=>1,//required
"pool_id"=>1,//required
"source_id"=>1,//required
"url"=>"http://url1",
"company_id"=>1,
"state_id"=>array(
1,
2,
),
"zipcode"=>array(
78729,
78749,
),
"cpc"=>12,
"reg_cpc"=>10,
"posted"=>"2016-06-23T22:00:00Z",
"created"=>"2016-05-23T22:00:00Z",
"experience"=>1,
"salary"=>1,
"education"=>1,
"jobtype"=>1,
"industry"=>1,//mainindustry
"industries"=>array(
1,
2,
),//listofindustries
"quality_score"=>1.0,
"boost_factor"=>1.0,
"paused"=>false,
"budget"=>100,
..snip...
);
$this->solrSearchClient->addJobDocument($jobProperties,true);
}
publicfunctiontestDeleteDocument()
{
$jobid="id2";
$this->solrSearchClient->deleteDocument($jobID);
}
}
?>
2MySQL
MySQLi
http://stackoverflow.com/questions/39753/connection-pooling-in-php
ItseemsthatthereisnoconnectionpoolinPHP.mysql_pconnectisdifferent.
http://codular.com/php-mysqli
http://cuiqingcai.com/1534.html
http://tao1848.blog.51cto.com/895768/196653
http://www.phpthinking.com/archives/1296
MySQLiBaseClasstoconnectto3differentDB,MySQLDAO.php
<?php
namespaceJobConsumerPHP;
require__DIR__.'/../../vendor/autoload.php';
classMySQLDAO
{
//jobDBinfo
protected$jobDBHost=null;
protected$jobDBName=null;
protected$jobDBUser=null;
protected$jobDBPassword=null;
//statsDBinfo
protected$statsDBHost=null;
protected$statsDBName=null;
protected$statsDBUser=null;
protected$statsDBPassword=null;
//coreDBinfo
protected$coreDBHost=null;
protected$coreDBName=null;
protected$coreDBUser=null;
protected$coreDBPassword=null;
protected$ioc=null;
publicfunction__construct($ioc){
$this->ioc=$ioc;
$logger=$this->ioc->getService("logger");
$config=$this->ioc->getService("config");
$this->jobDBHost=$config['jobDBHost'];
$this->jobDBName=$config['jobDBName'];
$this->jobDBUser=$config['jobDBUser'];
$this->jobDBPassword=$config['jobDBPassword'];
$this->statsDBHost=$config['statsDBHost'];
$this->statsDBName=$config['statsDBName'];
$this->statsDBUser=$config['statsDBUser'];
$this->statsDBPassword=$config['statsDBPassword'];
$this->coreDBHost=$config['coreDBHost'];
$this->coreDBName=$config['coreDBName'];
$this->coreDBUser=$config['coreDBUser'];
$this->coreDBPassword=$config['coreDBPassword'];
$logger->info("==============MySQLconfigstart==============");
$logger->info("jobDBHost=".$this->jobDBHost);
$logger->info("jobDBname=".$this->jobDBName);
$logger->info("jobDBUser=".$this->jobDBUser);
$logger->info("jobDBPassword=".$this->jobDBPassword);
$logger->info("===============================================");
$logger->info("statsDBHost=".$this->statsDBHost);
$logger->info("statsDBname=".$this->statsDBName);
$logger->info("statsDBUser=".$this->statsDBUser);
$logger->info("statsDBPassword=".$this->statsDBPassword);
$logger->info("===============================================");
$logger->info("coreDBHost=".$this->coreDBHost);
$logger->info("coreDBname=".$this->coreDBName);
$logger->info("coreDBUser=".$this->coreDBUser);
$logger->info("coreDBPassword=".$this->coreDBPassword);
}
publicfunctiongetJobSourceInfoBySourceID($sourceID)
{
$query="
SELECT
job_sources.id,
..snip...
job_pools.acct_mgr_id
FROM
job_sources
JOINjob_poolsONpool_id=job_pools.id
JOINjob_customersONjob_customers.id=job_customer_id
WHERE
job_sources.id=?AND
status=1
";
$conn=$this->getJobDBConn();
$stmt=$conn->prepare($query);
$stmt->bind_param("i",$sourceID);
$stmt->execute();
$result=$stmt->get_result();
//$data=$result->fetch_all(MYSQLI_ASSOC);
//justfetchthefirstrow;
$data=$result->fetch_assoc();
$this->closeDBConn($conn);
return$data;
}
privatefunctioncloseDBConn($conn)
{
mysqli_close($conn);
}
privatefunctiongetDBConn($dbHost,$dbName,$dbUser,$dbPass,$dbAlis)
{
$logger=$this->ioc->getService("logger");
$conn=mysqli_connect($dbHost,$dbUser,$dbPass,$dbName);
if(!$conn)
{
$logger->error("Failtoconnect".$dbAlis."DB---------");
$logger->error($dbAlis."DBHost=".$dbHost);
$logger->error($dbAlis."DBname=".$dbName);
$logger->error($dbAlis."DBUser=".$dbUser);
$logger->error($dbAlis."DBPassword=".$dbPass);
}
return$conn;
}
privatefunctiongetJobDBConn()
{
return$this->getDBConn($this->jobDBHost,$this->jobDBName,$this->jobDBUser,$this->jobDBPassword,"JOB");
}
privatefunctiongetStatsDBconn()
{
return$this->getDBConn($this->statsDBHost,$this->statsDBName,$this->statsDBUser,$this->statsDBPassword,"STATS");
}
privatefunctiongetCoreDBConn()
{
return$this->getDBConn($this->coreDBHost,$this->coreDBName,$this->coreDBUser,$this->coreDBPassword,"CORE");
}
}
?>
TesttheMySQLBaseclass,MySQLDAOTest.php
<?php
use\JobConsumerPHP\IOCUtil;
classMySQLDAOTestextendsPHPUnit_Framework_TestCase
{
protected$mySQLDAO;
protectedfunctionsetUp()
{
$ioc=newIOCUtil();
$this->mySQLDAO=$ioc->getService("mySQLDAO");
}
publicfunctiontestDummy()
{
$this->assertTrue(true);
}
publicfunctiontestGetJobSourceInfoBySourceID()
{
$result=$this->mySQLDAO->getJobSourceInfoBySourceID(790);
$this->assertNotEmpty($result);
}
}
?>
3CacheSystem
https://github.com/gilbitron/PHP-SimpleCache
http://www.phpfastcache.com/
https://github.com/PHPSocialNetwork/phpfastcache/wiki
Redisforthat
https://github.com/PHPSocialNetwork/phpfastcache/blob/final/examples/predis.php
>composerrequirephpFastCache/phpFastCache
TheCacheBaseClass,FastCache.php
<?php
namespaceJobConsumerPHP;
require__DIR__.'/../../vendor/autoload.php';
use\phpFastCache\CacheManager;
classFastCache
{
protected$cache=null;
protected$ioc=null;
publicfunction__construct($ioc){
$this->ioc=$ioc;
$logger=$this->ioc->getService("logger");
$config=$this->ioc->getService("config");
$logger->info("==============FastCacheconfigstart==========");
$logger->info("redisHost=".$config['redisHost']);
$logger->info("redisPort=".$config['redisPort']);
$logger->info("===============================================");
try{
CacheManager::setup(array(
'redis'=>array(
'host'=>$config['redisHost'],
'port'=>$config['redisPort'],
//'password'=>'',
//'database'=>'',
//'time'=>'',
),
));
$this->cache=CacheManager::getInstance('predis');
$logger->debug("SuccessfullyconnectedtoRedisandbuildCache");
}catch(Exception$e){
$logger->error("Couldn'tconnectedtoRedis");
$logger->error($e->getMessage());
}
}
publicfunctiongetCache()
{
return$this->cache;
}
}
?>
TestCaseclassforthat,FastCacheTest.php
<?php
use\JobConsumerPHP\IOCUtil;
classFastCacheTestextendsPHPUnit_Framework_TestCase
{
protected$fastCache;
protectedfunctionsetUp()
{
$ioc=newIOCUtil();
$this->fastCache=$ioc->getService("fastCache");
}
publicfunctiontestDummy()
{
$this->assertTrue(true);
}
publicfunctiontestCacheAndFetch()
{
$key="cache_key1";
$value="cache_value1";
//seconds
$this->fastCache->getCache()->set($key,$value,2);
$result1=$this->fastCache->getCache()->get($key);
$this->assertEquals($value,$result1);
sleep(3);
$result2=$this->fastCache->getCache()->get($key);
$this->assertEmpty($result2);
}
}
References:
http://www.solarium-project.org/
http://solarium.readthedocs.io/en/stable/getting-started/
http://solarium.readthedocs.io/en/stable/getting-started/#adding-documents
https://gist.github.com/basdenooijer/894286