Add caching for getJobOrderInfo. #689

This commit is contained in:
Korina Cordero 2022-06-29 07:55:12 +00:00
parent 363d7ed14a
commit ad99565d45
3 changed files with 113 additions and 15 deletions

View file

@ -233,6 +233,11 @@ services:
$loc_key: "%env(LOCATION_RIDER_ACTIVE_KEY)%"
$status_key: "%env(STATUS_RIDER_KEY)%"
App\Service\JsonCache:
arguments:
$redis_prov: "@App\\Service\\RedisClientProvider"
$jo_json_info_key: "%env(JO_JSON_INFO_KEY)%"
# inventory manager
App\Service\InventoryManager:
arguments:

View file

@ -48,6 +48,7 @@ use App\Service\HubDistributor;
use App\Service\HubFilterLogger;
use App\Service\HubFilteringGeoChecker;
use App\Service\HashGenerator;
use App\Service\JsonCache;
use App\Entity\MobileSession;
use App\Entity\Customer;
@ -1867,7 +1868,7 @@ class APIController extends Controller implements LoggedController
}
// we can't use param converter for now because we want to output the proper 404
public function getJobOrderInfo($id, Request $req, EntityManagerInterface $em, RiderTracker $rt)
public function getJobOrderInfo($id, Request $req, EntityManagerInterface $em, RiderTracker $rt, JsonCache $json_cache)
{
// check required parameters and api key
$res = $this->checkParamsAndKey($req, $em, []);
@ -1884,18 +1885,34 @@ class APIController extends Controller implements LoggedController
}
// get job order data
// $jo = $em->getRepository(JobOrder::class)->find($id);
// find just the job order and selected fields
$jo_data = $this->findJobOrder($id, $em);
if (empty($jo_data))
// find the JO in cache first
$jo_info = $this->findJobOrderFromCache($id, $json_cache);
if (!empty($jo_info))
{
$res->setError(true)
->setErrorMessage('No job order information found');
// check if job order belongs to customer / user
if ($cust->getID() != $jo_info['customer_id'])
{
$res->setError(true)
->setErrorMessage('No job order information found');
return $res->getReturnResponse();
}
$jo_data = $jo_info['jo_data'];
$data = [
'job_order' => $jo_data,
];
$res->setData($data);
return $res->getReturnResponse();
}
// check if job order belongs to customer / user
if ($cust->getID() != $jo_data['customer_id'])
// not in cache, get the JO from the DB
$jo_info = $this->findJobOrderFromDB($id, $em);
// check if not in db
if (empty($jo_info))
{
$res->setError(true)
->setErrorMessage('No job order information found');
@ -1904,12 +1921,19 @@ class APIController extends Controller implements LoggedController
// we already have customer from the mobile session
// get the rest of the needed information from invoice and rider rating and customer vehicle
$other_data = $this->findOtherData($jo_data, $id, $em);
$other_data = $this->findOtherData($jo_info, $id, $em);
// check if job order belongs to customer / user
if ($cust->getID() != $jo_info['customer_id'])
{
$res->setError(true)
->setErrorMessage('No job order information found');
return $res->getReturnResponse();
}
// put into job order data array
// TODO: might need a new function to generate the JO data
// $jo_data = $this->generateJobOrderData($req, $jo, $rt);
$jo_data = $this->createJobOrderData($req, $jo_data, $other_data, $cust, $rt);
$jo_data = $this->createJobOrderData($req, $jo_info, $other_data, $cust, $rt, $json_cache);
$data = [
'job_order' => $jo_data
@ -4703,16 +4727,17 @@ class APIController extends Controller implements LoggedController
return $jo_data;
}
protected function createJobOrderData($req, $jo_data, $other_data, $cust, $rt)
protected function createJobOrderData($req, $jo_data, $other_data, $cust, $rt, $json_cache)
{
$status = $jo_data['status'];
$jo_id = $jo_data['id'];
// need to convert the date_create to DateTime then string with the correct format
$date_create = DateTime::createFromFormat('Y-m-d H:i:s', $jo_data['date_create']);
$str_date_create = $date_create->format('M d, Y');
$data = [
'id' => $jo_data['id'],
'id' => $jo_id,
'date_create' => $str_date_create,
'service_type' => $jo_data['service_type'],
'destination' => [
@ -4819,14 +4844,41 @@ class APIController extends Controller implements LoggedController
break;
}
// need to put the jo data in json format to redis
$json_jo_data = json_encode($data);
$value = $cust->getID() . '|' . $json_jo_data;
$json_cache->addJOJsonInfo($jo_id, $value);
return $data;
}
protected function findJobOrder($id, EntityManagerInterface $em)
protected function findJobOrderFromCache($id, $json_cache)
{
$found_jo = [];
$cache_data = [];
$jo_in_cache = $json_cache->findJOJsonInfo($id);
if (!empty($jo_in_cache))
{
// need to get customer id from the string
$j_array = explode('|', $jo_in_cache, 2);
$cust_id = $j_array[0];
$data = json_decode($j_array[1]);
$cache_data = [
'customer_id' => $cust_id,
'jo_data' => $data,
];
}
return $cache_data;
}
protected function findJobOrderFromDB($id, EntityManagerInterface $em)
{
// not in cache, get from database
$found_jo = [];
$conn = $em->getConnection();
$jo_sql = 'SELECT jo.date_create, jo.service_type, ST_x(jo.coordinates), ST_y(jo.coordinates),

41
src/Service/JsonCache.php Normal file
View file

@ -0,0 +1,41 @@
<?php
namespace App\Service;
use App\Service\RedisClientProvider;
class JsonCache
{
protected $redis;
protected $jo_json_info_key;
public function __construct(RedisClientProvider $redis_prov, $jo_json_info_key)
{
$this->redis = $redis_prov->getRedisClient();
$this->jo_json_info_key = $jo_json_info_key;
}
public function addJOJsonInfo($jo_id, $jo_data)
{
$key = $jo_id;
$this->redis->hset($this->jo_json_info_key, $key, $jo_data);
}
public function findJOJsonInfo($jo_id)
{
$jo_data = '';
$key = $jo_id;
// check if JO id is in redis hash
$is_exist = $this->redis->hexists($this->jo_json_info_key, $key);
if ($is_exist)
{
// get the data
$jo_data = $this->redis->hget($this->jo_json_info_key, $key);
}
return $jo_data;
}
}