-
Notifications
You must be signed in to change notification settings - Fork 467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow changing the cache root directory #38
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,15 +46,17 @@ const CachedImage = React.createClass({ | |
React.PropTypes.bool, | ||
React.PropTypes.array | ||
]).isRequired, | ||
resolveHeaders: React.PropTypes.func | ||
resolveHeaders: React.PropTypes.func, | ||
cacheLocation: React.PropTypes.oneOf(Object.values(ImageCacheProvider.LOCATION)).isRequired | ||
}, | ||
|
||
getDefaultProps() { | ||
return { | ||
renderImage: props => (<Image ref={CACHED_IMAGE_REF} {...props}/>), | ||
activityIndicatorProps: {}, | ||
useQueryParamsInCacheKey: false, | ||
resolveHeaders: () => Promise.resolve({}) | ||
resolveHeaders: () => Promise.resolve({}), | ||
cacheLocation: ImageCacheProvider.LOCATION.CACHE | ||
}; | ||
}, | ||
|
||
|
@@ -116,7 +118,9 @@ const CachedImage = React.createClass({ | |
processSource(source) { | ||
const url = _.get(source, ['uri'], null); | ||
if (ImageCacheProvider.isCacheable(url)) { | ||
const options = _.pick(this.props, ['useQueryParamsInCacheKey', 'cacheGroup']); | ||
let options = _.pick(this.props, ['useQueryParamsInCacheKey', 'cacheGroup']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the change to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No reason, will revert to |
||
options.cacheLocation = this.props.cacheLocation; | ||
|
||
// try to get the image path from cache | ||
ImageCacheProvider.getCachedImagePath(url, options) | ||
// try to put the image in cache if | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,12 @@ const { | |
} = RNFetchBlob; | ||
|
||
const baseCacheDir = fs.dirs.CacheDir + '/imagesCacheDir'; | ||
const baseBundleDir = fs.dirs.MainBundleDir + '/imagesCacheDir'; | ||
|
||
const LOCATION = { | ||
CACHE: 'cache', | ||
BUNDLE: 'bundle' | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
const SHA1 = require("crypto-js/sha1"); | ||
const URL = require('url-parse'); | ||
|
@@ -18,7 +24,8 @@ const defaultImageTypes = ['png', 'jpeg', 'jpg', 'gif', 'bmp', 'tiff', 'tif']; | |
const defaultResolveHeaders = _.constant(defaultHeaders); | ||
|
||
const defaultOptions = { | ||
useQueryParamsInCacheKey: false | ||
useQueryParamsInCacheKey: false, | ||
cacheLocation: LOCATION.CACHE | ||
}; | ||
|
||
const activeDownloads = {}; | ||
|
@@ -58,6 +65,14 @@ function generateCacheKey(url, options) { | |
return SHA1(cacheable) + '.' + type; | ||
} | ||
|
||
function getBaseDir(cacheLocation) { | ||
switch (cacheLocation) { | ||
case LOCATION.CACHE: return baseCacheDir; | ||
case LOCATION.BUNDLE: return baseBundleDir; | ||
default: return baseCacheDir; | ||
} | ||
} | ||
|
||
function getCachePath(url, options) { | ||
if (options.cacheGroup) { | ||
return options.cacheGroup; | ||
|
@@ -72,7 +87,7 @@ function getCachedImageFilePath(url, options) { | |
const cachePath = getCachePath(url, options); | ||
const cacheKey = generateCacheKey(url, options); | ||
|
||
return `${baseCacheDir}/${cachePath}/${cacheKey}`; | ||
return `${getBaseDir(options.cacheLocation)}/${cachePath}/${cacheKey}`; | ||
} | ||
|
||
function deleteFile(filePath) { | ||
|
@@ -300,23 +315,25 @@ function seedCache(local, url, options = defaultOptions) { | |
|
||
/** | ||
* Clear the entire cache. | ||
* @param cacheLocation | ||
* @returns {Promise} | ||
*/ | ||
function clearCache() { | ||
return fs.unlink(baseCacheDir) | ||
function clearCache(cacheLocation) { | ||
return fs.unlink(getBaseDir(cacheLocation)) | ||
.catch(() => { | ||
// swallow exceptions if path doesn't exist | ||
}) | ||
.then(() => ensurePath(baseCacheDir)); | ||
.then(() => ensurePath(getBaseDir(cacheLocation))); | ||
} | ||
|
||
/** | ||
* Return info about the cache, list of files and the total size of the cache. | ||
* @param cacheLocation | ||
* @returns {Promise.<{size}>} | ||
*/ | ||
function getCacheInfo() { | ||
return ensurePath(baseCacheDir) | ||
.then(() => collectFilesInfo(baseCacheDir)) | ||
function getCacheInfo(cacheLocation) { | ||
return ensurePath(getBaseDir(cacheLocation)) | ||
.then(() => collectFilesInfo(getBaseDir(cacheLocation))) | ||
.then(cache => { | ||
const files = _.flattenDeep(cache); | ||
const size = _.sumBy(files, 'size'); | ||
|
@@ -336,5 +353,6 @@ module.exports = { | |
deleteMultipleCachedImages, | ||
clearCache, | ||
seedCache, | ||
getCacheInfo | ||
getCacheInfo, | ||
LOCATION | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd consider making this a
string
value instead of anenum
to allow more flexibility.We could expose the
LOCATION
enum with some convenience values, but still allow the user to input what ever he wantsWhat do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I concur with this. A future change I would like to add is to support falling back to multiple cache locations.
One of our applications would be bundling a set of content to the app on for release. We'd like to directly reference the bundled cached files if avaliable (rather than seeding by duplicating or pulling a fresh cache down).
By accepting cacheLocation as string, this may be one step closer to our requirement.