Skip to content

Commit

Permalink
cache tracking results
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrutcher committed Aug 16, 2023
1 parent a91dac0 commit 10785da
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const NodeGeocoder = require('node-geocoder');
const PettyCache = require('petty-cache');
const PitneyBowes = require('./carriers/pitneyBowes');
const UPS = require('./carriers/ups');
const FedEx = require('./carriers/fedEx');
Expand All @@ -7,6 +8,8 @@ const DHL = require('./carriers/dhl');

const geography = require('./util/geography');

let pettyCache;

function Bloodhound(options) {
// Options are optional
if (!options) {
Expand All @@ -19,6 +22,10 @@ function Bloodhound(options) {
}

if (options.pettyCache) {
if (!pettyCache) {
pettyCache = new PettyCache(options.pettyCache.port, options.pettyCache.host, options.pettyCache.options);
}

// Allow DHL to cache geocode results in Redis (via petty-cache)
if (options.dhl) {
options.dhl.pettyCache = options.pettyCache;
Expand Down Expand Up @@ -112,6 +119,27 @@ function Bloodhound(options) {
trackingNumber = trackingNumber.replace(/\s/g, '');
trackingNumber = trackingNumber.toUpperCase();

if (pettyCache) {
// Cache for 3 hours
return pettyCache.fetch(`bloodhound.track:${trackingNumber}`, function(callback) {
if (options.carrier === 'dhl') {
dhl.track(trackingNumber, options, callback);
} else if (options.carrier === 'fedex') {
fedEx.track(trackingNumber, options, callback);
} else if (options.carrier === 'newgistics') {
pitneyBowes.track(trackingNumber, options, callback);
} else if (options.carrier === 'pitney bowes') {
pitneyBowes.track(trackingNumber, options, callback);
} else if (options.carrier === 'ups'){
ups.track(trackingNumber, options, callback);
} else if (options.carrier === 'usps') {
usps.track(trackingNumber, options, callback);
} else {
return callback(new Error(`Carrier ${options.carrier} is not supported.`));
}
}, { ttl: 10800000 }, callback);
}

if (options.carrier === 'dhl') {
dhl.track(trackingNumber, options, callback);
} else if (options.carrier === 'fedex') {
Expand Down

0 comments on commit 10785da

Please sign in to comment.