meddelare-server
Advanced tools
Comparing version 1.2.2 to 1.3.0
var express = require("express"); | ||
var Promise = require("bluebird"); | ||
var request = require("request"); | ||
var cache = require("memory-cache"); | ||
// Cache results in memory -- but keep good and bad (error thrown) results for different periods of time. | ||
var LOCAL_CACHE_TIME_GOOD_RESULT = 4 * 60 * 1000, | ||
LOCAL_CACHE_TIME_BAD_RESULT = 1 * 60 * 1000; | ||
// Return this count if none was found or an error was thrown. | ||
var DEFAULT_UNKNOWN_COUNT = -1; | ||
// How many minutes should we cache the results for a given request | ||
@@ -116,6 +124,37 @@ var CACHE_TIME = process.env.CACHE_TIME || 4 * 60; | ||
return -1; | ||
throw err; | ||
}); | ||
} | ||
function createCacheKey(url, network) { | ||
return network + " '" + url + "'"; | ||
} | ||
function getCachedOrRetrieveCount(url, network) { | ||
var cacheKey = createCacheKey(url, network); | ||
return Promise.resolve(cache.get(cacheKey)) | ||
.then(function(cachedResult) { | ||
if (typeof cachedResult !== "undefined" && cachedResult !== null) { | ||
console.log(cacheKey, "from cache", cachedResult); | ||
return cachedResult; | ||
} | ||
return retrieveCount(url, network) | ||
.tap(function(uncachedResult) { | ||
console.log(cacheKey, "fetched good result", uncachedResult); | ||
cache.put(cacheKey, uncachedResult, LOCAL_CACHE_TIME_GOOD_RESULT); | ||
}) | ||
.catch(function(err) { | ||
console.error(cacheKey, "fetched bad result", err); | ||
cache.put(cacheKey, DEFAULT_UNKNOWN_COUNT, LOCAL_CACHE_TIME_BAD_RESULT); | ||
return DEFAULT_UNKNOWN_COUNT; | ||
}); | ||
}); | ||
} | ||
function retrieveCounts(url, networks) { | ||
@@ -128,3 +167,3 @@ // Create an object of callbacks for each of the requested networks It is | ||
networks.forEach(function(network) { | ||
networksToRequest[network] = retrieveCount(url, network); | ||
networksToRequest[network] = getCachedOrRetrieveCount(url, network); | ||
}); | ||
@@ -222,2 +261,11 @@ | ||
// Perhaps it's futile to try and clean up before/exit, but one can try. | ||
process | ||
.once("beforeExit", function() { | ||
cache.clear(); | ||
}) | ||
.once("exit", function() { | ||
cache.clear(); | ||
}); | ||
module.exports = router; |
{ | ||
"name": "meddelare-server", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "Install custom social share counters on your website with your own hosted solution, which only makes a single API request and loads minimal or zero assets to display the counters.", | ||
@@ -18,2 +18,3 @@ "homepage": "http://meddelare.com/", | ||
"express": "^4.12.4", | ||
"memory-cache": "^0.1.4", | ||
"morgan": "^1.5.3", | ||
@@ -20,0 +21,0 @@ "request": "~2.33.0" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18768
242
6
+ Addedmemory-cache@^0.1.4
+ Addedmemory-cache@0.1.6(transitive)