Skip to content

Commit

Permalink
Relative path kept in url cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Chrusciel committed Oct 18, 2017
1 parent d9a6a94 commit 98d8dd7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
16 changes: 10 additions & 6 deletions ImageCacheManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,27 @@ module.exports = (defaultOptions = {}, urlCache = MemoryCache, fs = fsUtils, pat
const cacheableUrl = path.getCacheableUrl(url, options.useQueryParamsInCacheKey);
// note: urlCache may remove the entry if it expired so we need to remove the leftover file manually
return urlCache.get(cacheableUrl)
.then(filePath => {
if (!filePath) {
.then(fileRelativePath => {
if (!fileRelativePath) {
// console.log('ImageCacheManager: cache miss', cacheableUrl);
throw new Error('URL expired or not in cache');
}
// console.log('ImageCacheManager: cache hit', cacheableUrl);
return filePath;
return `${options.cacheLocation}/${fileRelativePath}`;
})
// url is not found in the cache or is expired
.catch(() => {
const filePath = path.getImageFilePath(cacheableUrl, options.cacheLocation);
// const filePath = path.getImageFilePath(cacheableUrl, options.cacheLocation);
const fileRelativePath = path.getImageRelativeFilePath(cacheableUrl);
// remove expired file if exists

const filePath = `${options.cacheLocation}/${fileRelativePath}`

return fs.deleteFile(filePath)
// get the image to cache (download / copy / etc)
.then(() => getCachedFile(filePath))
// add to cache
.then(() => urlCache.set(cacheableUrl, filePath, options.ttl))
.then(() => urlCache.set(cacheableUrl, fileRelativePath, options.ttl))
// return filePath
.then(() => filePath);
});
Expand Down Expand Up @@ -127,4 +131,4 @@ module.exports = (defaultOptions = {}, urlCache = MemoryCache, fs = fsUtils, pat
},

};
};
};
20 changes: 17 additions & 3 deletions utils/pathUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function generateCacheKey(url, useQueryParamsInCacheKey = true) {
return SHA1(cacheable) + '.' + type;
}

function getCachePath(url) {
function getHostCachePathComponent(url) {
const {
host
} = new URL(url);
Expand All @@ -60,12 +60,26 @@ module.exports = {
* @returns {string}
*/
getImageFilePath(url, cacheLocation) {
const cachePath = getCachePath(url);
const hostCachePath = getHostCachePathComponent(url);
const cacheKey = generateCacheKey(url);

return `${cacheLocation}/${cachePath}/${cacheKey}`;
return `${cacheLocation}/${hostCachePath}/${cacheKey}`;
},

/**
* Given a URL returns the relative file path combined from host and url hash
* @param url
* @returns {string}
*/

getImageRelativeFilePath(url) {
const hostCachePath = getHostCachePathComponent(url);
const cacheKey = generateCacheKey(url);

return `${hostCachePath}/${cacheKey}`;
},


/**
* returns the url after removing all unused query params
* @param url
Expand Down

0 comments on commit 98d8dd7

Please sign in to comment.