Add redis cache to have the JOs from the last 24 hours. #535
This commit is contained in:
parent
9473609459
commit
4231c144d0
4 changed files with 59 additions and 17 deletions
|
|
@ -55,6 +55,9 @@ CVU_BRAND_ID=insert_brandid_for_unknown_vehicles
|
|||
# country code prefix
|
||||
COUNTRY_CODE=+insert_country_code_here
|
||||
|
||||
# redis hash
|
||||
LATEST_ACTIVE_JO=latest_active_jo
|
||||
|
||||
# dashboard
|
||||
DASHBOARD_ENABLE=set_to_true_or_false
|
||||
|
||||
|
|
|
|||
|
|
@ -223,6 +223,7 @@ services:
|
|||
arguments:
|
||||
$redis_prov: "@App\\Service\\RedisClientProvider"
|
||||
$active_jo_key: "%env(LOCATION_JO_ACTIVE_KEY)%"
|
||||
$latest_jo_key: "%env(LATEST_ACTIVE_JO)%"
|
||||
|
||||
App\Service\RiderCache:
|
||||
arguments:
|
||||
|
|
|
|||
|
|
@ -47,7 +47,8 @@ class HomeController extends Controller
|
|||
public function getRiderLocations(JobOrderCache $jo_cache, RiderCache $rider_cache, EntityManagerInterface $em, RiderTracker $rider_tracker)
|
||||
{
|
||||
// get active JOs from cache
|
||||
$active_jos = $jo_cache->getAllActiveJobOrders();
|
||||
// $active_jos = $jo_cache->getAllActiveJobOrders();
|
||||
$active_jos = $jo_cache->getAllLatestActiveJobOrders();
|
||||
$riders = $rider_cache->getAllActiveRiders();
|
||||
|
||||
// TODO: optimize this
|
||||
|
|
@ -78,17 +79,6 @@ class HomeController extends Controller
|
|||
continue;
|
||||
}
|
||||
|
||||
// check if JO's date schedule is from the last 24 hours
|
||||
$date = new DateTime();
|
||||
$date_interval = new DateInterval('P1D');
|
||||
$date->sub($date_interval);
|
||||
|
||||
if ($jo->getDateSchedule() < $date)
|
||||
{
|
||||
unset($active_jos[$jo_id]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($jo->getSource() == TransactionOrigin::MOBILE_APP)
|
||||
{
|
||||
$active_jos[$jo_id]['is_mobile'] = true;
|
||||
|
|
@ -98,7 +88,6 @@ class HomeController extends Controller
|
|||
$active_jos[$jo_id]['is_mobile'] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// get active riders from cache
|
||||
// get all riders
|
||||
|
|
|
|||
|
|
@ -9,11 +9,14 @@ class JobOrderCache
|
|||
{
|
||||
protected $redis;
|
||||
protected $active_jo_key;
|
||||
protected $latest_active_jo_key;
|
||||
|
||||
public function __construct(RedisClientProvider $redis_prov, $active_jo_key)
|
||||
public function __construct(RedisClientProvider $redis_prov, $active_jo_key,
|
||||
$latest_jo_key)
|
||||
{
|
||||
$this->redis = $redis_prov->getRedisClient();
|
||||
$this->active_jo_key = $active_jo_key;
|
||||
$this->latest_active_jo_key = $latest_jo_key;
|
||||
}
|
||||
|
||||
public function addActiveJobOrder(JobOrder $jo)
|
||||
|
|
@ -26,6 +29,17 @@ class JobOrderCache
|
|||
$coords->getLatitude(),
|
||||
$jo->getID()
|
||||
);
|
||||
|
||||
// add to JO cache with expiry date
|
||||
$key = $jo->getID();
|
||||
$data = [
|
||||
'id' => $key,
|
||||
'latitude' => $coords->getLatitude(),
|
||||
'longitude' => $coords->getLongitude(),
|
||||
];
|
||||
$value = json_encode($data);
|
||||
|
||||
$this->redis->hset($this->latest_active_jo_key, $key, $value);
|
||||
}
|
||||
|
||||
public function getAllActiveJobOrders()
|
||||
|
|
@ -56,11 +70,46 @@ class JobOrderCache
|
|||
return $jo_locs;
|
||||
}
|
||||
|
||||
public function getAllLatestActiveJobOrders()
|
||||
{
|
||||
// get all fields in latest_active_jo hash
|
||||
$latest_active_jos = $this->redis->hgetall($this->latest_active_jo_key);
|
||||
|
||||
$jo_locations = [];
|
||||
foreach ($latest_active_jos as $active_jo)
|
||||
{
|
||||
$jo_data = json_decode($active_jo, true);
|
||||
$id = '';
|
||||
$lat = '';
|
||||
$lng = '';
|
||||
foreach ($jo_data as $key => $value)
|
||||
{
|
||||
if ($key == 'id')
|
||||
$id = $value;
|
||||
if ($key == 'longitude')
|
||||
$lng = $value;
|
||||
if ($key == 'latitude')
|
||||
$lat = $value;
|
||||
|
||||
}
|
||||
$jo_locations[$id] = [
|
||||
'longitude' => $lng,
|
||||
'latitude' => $lat,
|
||||
];
|
||||
}
|
||||
|
||||
//error_log(print_r($jo_locations, true));
|
||||
return $jo_locations;
|
||||
}
|
||||
|
||||
public function removeActiveJobOrder(JobOrder $jo)
|
||||
{
|
||||
$this->redis->zrem(
|
||||
$this->active_jo_key,
|
||||
$jo->getID()
|
||||
);
|
||||
|
||||
// remove key from latest active jo
|
||||
$this->redis->hdel($this->latest_active_jo_key, $jo->getID());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue