-
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
Conversation
- Adds cacheLocation (cache|bundle) options to CachedImage and ImageCacheProvider - Uses respective directories for caching
@sladkoff Thanks! This look awesome! :) |
@sladkoff Hey, looks good. Do you have time to add Document directory support too? Thanks |
CachedImage.js
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
why the change to let
?
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.
No reason, will revert to const
. I think I missed that during cleanup.
@sladkoff Hey! Thanks for this! I'd really like to merge this ASAP, this is a great PR :) |
CachedImage.js
Outdated
@@ -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 |
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 an enum
to allow more flexibility.
We could expose the LOCATION
enum with some convenience values, but still allow the user to input what ever he wants
What 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.
@sladkoff I left some comments in the code, basically I'd rather have the |
@kfiroo Thanks for the feedback. I'm currently on vacation, will fix this up next week B) |
The `cacheLocation` option can now be specified as a string. `ImageCacheProvider.LOCATION` contains default / convenience values. Updated readme.
CachedImage.js
Outdated
@@ -47,7 +47,7 @@ const CachedImage = React.createClass({ | |||
React.PropTypes.array | |||
]).isRequired, | |||
resolveHeaders: React.PropTypes.func, | |||
cacheLocation: React.PropTypes.oneOf(Object.values(ImageCacheProvider.LOCATION)).isRequired | |||
cacheLocation: React.PropTypes.string |
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.
Made cacheLocation
path fully customizable via string option.
.then(() => fs.exists(dirPath).then(exists => { | ||
// Check if dir has indeed been created because | ||
// there's no exception on incorrect user-defined paths (?)... | ||
if (!exists) throw new Error('Invalid cacheLocation'); |
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.
Had to add this workaround in order to be able to properly throw an exception when a user-defined cacheLocation path could not be created. Let me know if there's a better way.
ImageCacheProvider.js
Outdated
CACHE: 'cache', | ||
BUNDLE: 'bundle' | ||
CACHE: fs.dirs.CacheDir + '/imagesCacheDir', | ||
BUNDLE: fs.dirs.MainBundleDir + '/imagesCacheDir' | ||
}; |
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.
The LOCATION
enum still contains some defaults / convenience values.
@kfiroo The latest version supports custom |
@sladkoff Cool, thanks. |
@sladkoff Hey, I agree, I think we should move forward with this PR. |
This PR addresses what I already mentioned in #36:
Background
Files in the default cache directory may be purged by the Android OS automatically to free up space. While this is fine in most cases, you may want to make sure that the cached images are permanent (when implementing an offline-mode for example).
API changes
This PR introduces a
cacheLocation
prop in CachedImage and acacheLocation
option in ImageCacheProvider. All utility functions likeclearCache
now take an optional parameter as well.Possible values:
ImageCacheProvider.LOCATION.CACHE (='cache')
->fs.dirs.CacheDir
ImageCacheProvider.LOCATION.BUNDLE (='bundle')
->fs.dirs.MainBundleDir
Everything falls back to 'cache' so there should be no breaking changes.
Feedback appreciated
I tested the
CachedImage
component and some of the caching functions inImageCacheProvider
. I did not get to test all of the utility functions likecollectFilesInfo
,clearCache
, etc.I verified that the images are stored in the respective directories.
I updated the README.
I only tested on Android
It'd be cool if your could look over the changes and test if you have the time.
Let me know what you think. I can fix stuff if needed.