Browse Source

Add geocode cache (close #30)

master
Skylar Ittner 2 years ago
parent
commit
4d9b4abd19
  1. 29
      www/assets/js/packages.js
  2. 1
      www/settings.js

29
www/assets/js/packages.js

@ -290,6 +290,28 @@ function addPackageByAddress(number, street, citystate, type, callback) {
var deadline = false;
var ajaxlookup = function () {
var geocodecache = localStorage.getItem("geocode_cache");
if (geocodecache == null) {
geocodecache = "{}";
localStorage.setItem("geocode_cache", "{}");
}
geocodecache = JSON.parse(geocodecache);
var cachekey = number + " || " + street + " || " + citystate;
var cacheitem = geocodecache[cachekey];
var timestamp = Math.floor(Date.now() / 1000);
if (typeof cacheitem != 'undefined') {
if (cacheitem.added + SETTINGS.geocodecacheexpiry < timestamp) {
console.log("Info", "Removing expired geocode cache item " + cachekey);
delete geocodecache[cachekey];
localStorage.setItem("geocode_cache", JSON.stringify(geocodecache));
} else {
console.log("Info", "Using cached geocode result", cacheitem);
addPackage(cacheitem.address, cacheitem.latitude, cacheitem.longitude, type, callback, deadline);
return;
}
}
$.ajax({
url: SETTINGS.geocodeapi,
dataType: 'json',
@ -308,6 +330,13 @@ function addPackageByAddress(number, street, citystate, type, callback) {
if (resp.status == "OK") {
if (resp.accuracy.ok) {
addPackage(resp.address.street, resp.coords[0], resp.coords[1], type, callback, deadline);
geocodecache[cachekey] = {
address: resp.address.street,
latitude: resp.coords[0],
longitude: resp.coords[1],
added: Math.floor(Date.now() / 1000)
};
localStorage.setItem("geocode_cache", JSON.stringify(geocodecache));
} else {
playSound("error");
app.dialog.confirm(

1
www/settings.js

@ -301,6 +301,7 @@ var SETTINGS = {
]
}
],
geocodecacheexpiry: 604800, // One week
geocodeapi: "https://apis.netsyms.net/packagehelper/geocode.php",
trackingapi: "https://apis.netsyms.net/packagehelper/track.php",
weatherapi: "https://apis.netsyms.net/packagehelper/weather.php",

Loading…
Cancel
Save