Browse Source

cache timestamps and avoid storing duplicate data

closes #10
pull/14/merge
Aaron Parecki 6 years ago
parent
commit
5d65277c3a
No known key found for this signature in database GPG Key ID: 276C2817346D6056
2 changed files with 14 additions and 5 deletions
  1. +2
    -0
      compass/.env.example
  2. +12
    -5
      compass/app/Http/Controllers/Api.php

+ 2
- 0
compass/.env.example View File

@ -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

+ 12
- 5
compass/app/Http/Controllers/Api.php View File

@ -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 {

Loading…
Cancel
Save