Skip to content
This repository has been archived by the owner on Nov 6, 2022. It is now read-only.

Commit

Permalink
fix map caching in redis
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Abbott committed Apr 13, 2017
1 parent 2d5e8fb commit 8d7d79e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 32 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.2.4
- Fix bad GPX caching in Redis

## 2.2.3
- Update dependencies
- Use memory instead of Redis to cache views
Expand Down
2 changes: 1 addition & 1 deletion lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ module.exports = {
* @returns {Promise.<ViewCacheItem>}
*/
add: (key, geoJSON) => addItem(mapKey, key, geoJSON, config.cache.maps),

/**
* Whether cache map exists
* @param {string} key
Expand Down
3 changes: 3 additions & 0 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const template = require('./template');
const factory = require('./factory');
const library = require('./library');
const C = require('./constants');
/**
* Route placeholders
*/
const ph = C.route;

/**
Expand Down
23 changes: 12 additions & 11 deletions lib/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,9 @@ function buildSearchPhoto(json, sizeField) {
}

/**
* @param {Object} json
* @param {String|String[]} sizeField Size or list of size field names in order of preference
* @returns {Size|Object}
* @param {object} json
* @param {string|string[]} sizeField Size or list of size field names in order of preference
* @returns {Size|object}
*/
function buildPhotoSize(json, sizeField) {
const size = {
Expand Down Expand Up @@ -649,7 +649,6 @@ function identifyPhotoDateOutliers(photos) {

/**
* Coordinate property used by Google's static maps
* @returns {String} Comma delimited list of coordinates
* @this {Post}
* @see https://developers.google.com/maps/documentation/static-maps/intro
*/
Expand All @@ -675,14 +674,14 @@ function serializePhotoCoordinates() {
//= EXIF ======================================================================

/**
* @param {String} photoID
* @param {string} photoID
* @returns {Promise}
*/
function getEXIF(photoID) { return flickr.getExif(photoID).then(buildExif); }

/**
* @param {Flickr.PhotoExif} flickrExif
* @return {EXIF|Object}
* @return {EXIF|object}
*/
function buildExif(flickrExif) {
const parser = (exif, tag, empty = null) => {
Expand All @@ -704,7 +703,7 @@ function buildExif(flickrExif) {
}

/**
* @param {EXIF|Object} exif
* @param {EXIF|object} exif
* @returns {EXIF}
*/
function sanitizeExif(exif) {
Expand Down Expand Up @@ -767,7 +766,7 @@ const cache = require('./cache');

/**
* Load all map information (track and photo features) for a post
* @param {String} postKey
* @param {string} postKey
* @returns {Promise.<ViewCacheItem>}
* @see http://geojsonlint.com/
*/
Expand All @@ -776,7 +775,9 @@ const mapForPost = postKey => config.cache.maps
: loadMapForPost(postKey);

/**
* @param {String} postKey
* Get GeoJSON for map. If post has no track then GPX will only include photo
* markers.
* @param {string} postKey
* @returns {Promise.<ViewCacheItem>}
*/
function loadMapForPost(postKey) {
Expand All @@ -789,7 +790,7 @@ function loadMapForPost(postKey) {
? noGPX
: google.drive.loadGPX(post)
.then(map.featuresFromGPX)
.catch(err => noGPX);
.catch(() => noGPX);

return getFeatures
.then(geo => mapPhotoFeatures(post, geo))
Expand All @@ -800,7 +801,7 @@ function loadMapForPost(postKey) {
* Append photo GeoFeatures to GeoJSON
* @param {Post} post
* @param {GeoJSON.FeatureCollection} [geo]
* @returns {Promise.<Object>} GeoJSON
* @returns {Promise.<object>} GeoJSON
*/
const mapPhotoFeatures = (post, geo) => new Promise(resolve => {
// move to the first post in a series
Expand Down
31 changes: 16 additions & 15 deletions lib/library.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const is = require('./is');

/**
* Singleton collection of photos grouped into "posts" (called a "set" or "album" in most providers)
* that are in turn assigned categories. Additional library methods are added by the factory.
* Singleton collection of photos grouped into "posts" (called a "set" or
* "album" in most providers) that are in turn assigned categories. Additional
* library methods are added by the factory.
* @see addHelperMethods
* @type {Library}
*/
module.exports = {
/**
* Root categories indexed by their name
* @type {Object.<Category>}
* @type {object.<Category>}
*/
categories: {},
/**
Expand All @@ -19,15 +20,15 @@ module.exports = {
posts: [],
/**
* All photo tags in hash[slug] = full name format
* @type {Object.<String>}
* @type {object.<string>}
*/
tags: {},
loaded: false,
postInfoLoaded: false,
/**
* Track keys of posts and categories that change on library reload
* (can be used for cache invalidation)
* @type {String[]}
* @type {string[]}
*/
changedKeys: [],

Expand All @@ -42,14 +43,14 @@ module.exports = {

/**
* Array of all post keys
* @returns {String[]}
* @returns {string[]}
*/
postKeys() { return this.posts.map(p => p.key); },

/**
* Array of all category keys
* @param {String[]} [filterList] List of category names or all if no list given
* @returns {String[]}
* @param {string[]} [filterList] List of category names or all if no list given
* @returns {string[]}
*/
categoryKeys(filterList = []) {
const keys = [];
Expand Down Expand Up @@ -82,7 +83,7 @@ module.exports = {

/**
* Find category with given key
* @param {String} key
* @param {string} key
* @returns {Category}
*/
categoryWithKey(key) {
Expand All @@ -99,15 +100,15 @@ module.exports = {

/**
* Find post with given ID
* @param {String} id
* @param {string} id
* @returns {Post}
*/
postWithID(id) { return this.posts.find(p => p.id == id); },

/**
* Find post with given slug
* @param {String} key
* @param {String} [partKey]
* @param {string} key
* @param {string} [partKey]
* @returns {Post}
*/
postWithKey(key, partKey) {
Expand All @@ -117,7 +118,7 @@ module.exports = {

/**
* Unload particular posts to force refresh from source
* @param {String|String[]} keys
* @param {string|string[]} keys
*/
unload(keys) {
if (!is.array(keys)) { keys = [keys]; }
Expand All @@ -130,7 +131,7 @@ module.exports = {

/**
* Remove posts (primarily for testing)
* @param {String|String[]} keys
* @param {string|string[]} keys
*/
remove(keys) {
if (!is.array(keys)) { keys = [keys]; }
Expand All @@ -146,7 +147,7 @@ module.exports = {
/**
* Get unique list of tags used on photos in the post and update photo tags to use full names
* @param {Photo[]} photos
* @returns {String} Unique list of photo tags
* @returns {string} Unique list of photo tags
*/
photoTagList(photos) {
// all photo tags in the post
Expand Down
17 changes: 12 additions & 5 deletions lib/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ client.on('end', ()=> {
function normalize(value) {
if (typeof value == is.type.OBJECT) {
const cache = require('./cache');
return is.cacheItem(value) ? cache.view.serialize(value) : JSON.stringify(value);
return is.cacheItem(value) ? cache.redisView.serialize(value) : JSON.stringify(value);
} else {
return value;
}
Expand All @@ -91,16 +91,23 @@ function normalize(value) {
* @returns {object}
*/
function parseObject(value) {
return is.empty(value) ? null : JSON.parse(value);
if (is.empty(value)) { return null; }

try {
return JSON.parse(value)
} catch (err) {
log.error('Unable to JSON parse "%s"', value);
return null;
}
}

/**
* Normalize response
* @param {string} key Cache key
* @param {number} type Data type
* @param {Function} resolve
* @param {Function} reject
* @returns {Function}
* @param {function} resolve
* @param {function} reject
* @returns {function}
*/
function makeHandler(key, type = dataType.NONE, resolve, reject) {
// calculate expected response
Expand Down

0 comments on commit 8d7d79e

Please sign in to comment.