diff --git a/compass/.env.example b/compass/.env.example index ab2dec3..c27d0ab 100644 --- a/compass/.env.example +++ b/compass/.env.example @@ -33,6 +33,8 @@ SESSION_LIFETIME=500000 QUEUE_DRIVER=database # QUEUE_DRIVER=redis +CACHE_DRIVER=redis + # This is how Compass signs users in. You can leave it set to the default, and # you'll be redirected there to authenticate as your domain name. # You can also run your own instance, or even point it to your own website diff --git a/compass/app/Http/Controllers/Api.php b/compass/app/Http/Controllers/Api.php index 47ebc94..028d31d 100644 --- a/compass/app/Http/Controllers/Api.php +++ b/compass/app/Http/Controllers/Api.php @@ -4,9 +4,8 @@ namespace App\Http\Controllers; use Laravel\Lumen\Routing\Controller as BaseController; use Illuminate\Http\Request; -use DB; +use DB, Log, Cache; use Quartz; -use Log; use DateTime, DateTimeZone, DateInterval; use App\Jobs\TripComplete; use App\Jobs\NotifyOfNewLocations; @@ -237,9 +236,17 @@ class Api extends BaseController $date = new DateTime($loc['properties']['timestamp']); if($date) { - $num++; - $qz->add($date, $loc); - $last_location = $loc; + $cacheKey = 'compass::'.$db->name.'::'.$date->format('U'); + + // Skip adding if the timestamp is already in the cache. + // Helps prevent writing duplicate data when the HTTP request is interrupted. + if(!env('CACHE_DRIVER') || !Cache::has($cacheKey)) { + $num++; + $qz->add($date, $loc); + if(env('CACHE_DRIVER')) + Cache::put($cacheKey, 1, 360); // cache this for 6 hours + $last_location = $loc; + } if(array_key_exists('type', $loc['properties']) && $loc['properties']['type'] == 'trip') { try {