-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from yujiosaka/pluggable_cache_storage
Add pluggable cache
- Loading branch information
Showing
21 changed files
with
575 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ jobs: | |
build: | ||
docker: | ||
- image: circleci/node:6.10 | ||
- image: redis | ||
steps: | ||
- checkout | ||
- run: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const HCCrawler = require('../'); | ||
|
||
HCCrawler.launch({ | ||
maxConcurrency: 1, | ||
maxRequest: 2, | ||
evaluatePage: (() => ({ | ||
title: $('title').text(), | ||
h1: $('h1').text(), | ||
})), | ||
onSuccess: (result => { | ||
console.log('onSuccess', result); | ||
}), | ||
}) | ||
.then(crawler => { | ||
crawler.queue({ url: 'https://example.com/' }); | ||
crawler.queue({ url: 'https://example.net/' }); | ||
crawler.queue({ url: 'https://example.org/' }); // The queue won't be requested until resumed | ||
crawler.onIdle() | ||
.then(() => { | ||
crawler.setMaxRequest(3); | ||
crawler.resume(); | ||
return crawler.onIdle(); | ||
}) | ||
.then(() => crawler.close()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const HCCrawler = require('../'); | ||
|
||
HCCrawler.launch({ | ||
maxConcurrency: 1, | ||
evaluatePage: (() => ({ | ||
title: $('title').text(), | ||
h1: $('h1').text(), | ||
})), | ||
onSuccess: (result => { | ||
console.log('onSuccess', result); | ||
}), | ||
cache: new HCCrawler.SessionCache(), | ||
}) | ||
.then(crawler => { | ||
crawler.queue('https://example.com/'); | ||
crawler.queue('https://example.net/'); | ||
crawler.queue('https://example.com/'); // The queue won't be requested | ||
crawler.onIdle() | ||
.then(() => crawler.close()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
const _ = require('lodash'); | ||
const { hash, jsonStableReplacer } = require('../helper'); | ||
|
||
const OMITTED_HASH_FIELDS = [ | ||
'priority', | ||
'allowedDomains', | ||
'delay', | ||
'retryCount', | ||
'retryDelay', | ||
'jQuery', | ||
'username', | ||
'password', | ||
'preRequest', | ||
'evaluatePage', | ||
'onSuccess', | ||
'onError', | ||
'timeout', | ||
'waitUntil', | ||
]; | ||
const MAX_LENGTH = 10; | ||
|
||
class BaseCache { | ||
constructor(settings) { | ||
this._settings = settings; | ||
} | ||
|
||
/** | ||
* Initializing the cache storage | ||
* @return {Promise} resolves when init operation completed | ||
* @interface | ||
*/ | ||
init() { | ||
throw new Error('Init is not overridden!'); | ||
} | ||
|
||
/** | ||
* Closing the cache storage | ||
* @return {Promise} resolves when close operation completed | ||
* @interface | ||
*/ | ||
close() { | ||
throw new Error('Close is not overridden!'); | ||
} | ||
|
||
/** | ||
* Clearing the cache storage | ||
* @return {Promise} resolves when clear operation completed | ||
* @interface | ||
*/ | ||
clear() { | ||
throw new Error('Clear is not overridden!'); | ||
} | ||
|
||
/** | ||
* Method to check whether the requested options already exists in the cache storage | ||
* @param {Object} options | ||
* @return {Promise} resolves whether the requested options already exists | ||
* @interface | ||
*/ | ||
exists() { | ||
throw new Error('Get is not overridden!'); | ||
} | ||
|
||
/** | ||
* Method to set the requested options to the cache storage | ||
* @param {Object} options | ||
* @return {Promise} resolves when set operation completed | ||
* @interface | ||
*/ | ||
set() { | ||
throw new Error('Set is not overridden!'); | ||
} | ||
|
||
/** | ||
* Method to check whether the requested options already exists in the cache storage | ||
* @param {Object} options | ||
* @return {String} session cache key for the option | ||
* @static | ||
*/ | ||
static key(options) { | ||
const json = JSON.stringify(_.omit(options, OMITTED_HASH_FIELDS), jsonStableReplacer); | ||
return hash(json).substring(0, MAX_LENGTH); | ||
} | ||
} | ||
|
||
module.exports = BaseCache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exports.BaseCache = require('./base'); | ||
exports.SessionCache = require('./session'); | ||
exports.RedisCache = require('./redis'); |
Oops, something went wrong.