Skip to content

Support for an advanced “persistentCache” #124

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

Merged
7 commits merged into from
Feb 14, 2017

Conversation

martinheidegger
Copy link
Collaborator

There are a few implementations of persistent caches for browserify such as persistify or browserify-incremental. But looking at each implementation it seemed like they struggle with the interface given by module-deps to cache: which is just an object lookup. This PR adds an additional cache interface to module-deps that should make it possible to implement not just resource-efficient but also less hacky caching solutions for browserify.

readme.markdown Outdated
}
if (isInCache()) {
return cb(null, {
source: fs.readFileSync(file, 'utf8'), // String of the fully processed file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is the "in cache" case, the example should probably show the contents coming from somewhere other than fs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does now.

readme.markdown Outdated
// optional (can be null) stream that provides the data from
// the hard disk, can be provided in case the file data is used
// to evaluate the cache identifier
stream = fs.createReadStream(file)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can there be a better example here, since this is what I expect fallback to be doing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the example. it now uses a db.

index.js Outdated
fromSource(file, body.toString('utf8'), pkg, fakePath);
}))
;
self.persistentCache(file, id, pkg, function fallback (stream, cb) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to move the fallback function somewhere other than an inline parameter here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved it to an outside scope.

- Used database instead of filesystem in the cache example.
- Moved the fallback handler to a method one scope out.
@martinheidegger
Copy link
Collaborator Author

@terinjokes Thank you for your review! I updated the PR. One thing to mention: I changed the implementation. Now the fallback takes an optional string instead of a stream. This change might seem odd, I came to the conclusion that this would be the better API when I tried to implement a cache. The cache could read the source in order to create a Key but if it did that with streams (i.e. to create a hash) then it would need to keep all the file data in memory and create yet another stream off of the in-memory data which would just make the impl. more complex without a real win for the module-deps.

@martinheidegger
Copy link
Collaborator Author

@terinjokes Thank you for approving the PR, how good are the chances for a merge?

@terinjokes
Copy link
Contributor

I'd prefer to have someone else take a look first.

@martinheidegger
Copy link
Collaborator Author

Whom would be good to ask?

@martinheidegger
Copy link
Collaborator Author

Looked into the npm owners and pinging @substack @zertosh @jmm @MellowMelon @ahdinosaur for a review.

Copy link
Member

@ahdinosaur ahdinosaur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi hi hi

@@ -550,6 +582,13 @@ function xhas (obj) {
return true;
}

function toStream (dataAsString) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice refactor 👍

});

function getDeps (file, src) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more goods 🐰

@@ -27,6 +27,11 @@ function Deps (opts) {
if (!opts) opts = {};

this.basedir = opts.basedir || process.cwd();
this.persistentCache = opts.persistentCache || function (file, id, pkg, fallback, cb) {
setImmediate(function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why setImmediate here?

Copy link
Collaborator Author

@martinheidegger martinheidegger Feb 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

͓̞̦̼͎. ̖̱̗̝͉͈Z̧̬͎̥͕̻̜AḺ̡̼G̝͓͘O̴̫̹̰̯͕!͈̱̟͈̕

Edit: let me extend: Since the implementation expects an async handler it felt only appropriate to uphold the contract in the default implementation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of course, thanks!

index.js Outdated

function persistentCacheFallback (dataAsString, cb) {
var stream
if (dataAsString) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ternary? also semicolon styles missing here;

index.js Outdated
source: src,
package: pkg,
deps: deps.reduce(function (deps, dep) {
deps[dep] = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (pedantic);

// then you can pass `null` instead of the source and the fallback will
// fetch the data by itself.
//
fallback(fileData, function (error, cacheableEntry) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where did fallback come from here? is missing from above description of function arguments.

p.on('error', function (err) { t.equals(err.message, 'foo') });
});

test('allow passing of a different stream', function (t) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what stream? do you mean a different string to fallback?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it used to be a stream :)

@martinheidegger
Copy link
Collaborator Author

Thanks for the review @ahdinosaur! Pushed a few fixes to the PR in regards to the linting.

Copy link
Member

@ahdinosaur ahdinosaur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya ya ya

@martinheidegger
Copy link
Collaborator Author

Two approved reviews 😃

@martinheidegger
Copy link
Collaborator Author

martinheidegger commented Feb 2, 2017

@terinjokes @ahdinosaur Are the chances of a merge higher now with two Reviews? 😅

@martinheidegger
Copy link
Collaborator Author

Note: I published an implementation of persistentCache here: https://github.com/martinheidegger/browserify-persist-fs

@ghost ghost merged commit c940a1c into browserify:master Feb 14, 2017
@ghost
Copy link

ghost commented Feb 14, 2017

Thanks everyone for reviewing this and going back and forth. Published as 4.1.0.

@ghost
Copy link

ghost commented Feb 14, 2017

We should also ping @jsdf and @royriojas upstream to make sure they know about this patch so they can make the best use of these changes.

@ahdinosaur
Copy link
Member

ahdinosaur commented Mar 20, 2017

@martinheidegger hey, so the persistent cache breaks when using bulk-require and bulkify. specifically when you create a new file in your bulk require glob, it is ignored and built as before. i see you tried to fix file events with #127, but as far as i can tell it's still broken when using that fix. any ideas?

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants