From 9f94fed8542a35bee7797ff15fe918149417a7b5 Mon Sep 17 00:00:00 2001 From: narekhovhannisyan Date: Sun, 9 Feb 2020 17:54:48 +0400 Subject: [PATCH 1/4] util: update cache. --- src/util/cache.js | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/util/cache.js b/src/util/cache.js index 6efd3f7..43dad91 100644 --- a/src/util/cache.js +++ b/src/util/cache.js @@ -1,14 +1,19 @@ -const storage = {} +/** + * @typedef {import('../types/cache').TCacheMethods} TCacheMethods + */ /** * Key-value based cache with expire feature. + * @returns {TCacheMethods} */ const Cache = () => { + const storage = {} + /** * Checks if the key is close to expire. * @private */ - const timer = setInterval(() => { + setInterval(() => { const now = Date.now() for (const key in storage) { const { date, expire } = storage[key] @@ -24,7 +29,7 @@ const Cache = () => { * @param {String} key The key for storing data. * @returns {any} */ - const getFromCache = (key) => storage[key] ? storage[key].value : undefined + const getValueBy = (key) => storage[key] ? storage[key].value : undefined /** * Stores data with given `key`, `expire` and `value` @@ -32,20 +37,15 @@ const Cache = () => { * @param {Number} [expire] The seconds to expire. * @returns {(any) => any} */ - const storeInCache = (key, expire = Infinity) => (value) => { + const store = (key, expire = Infinity) => (value) => { storage[key] = {} storage[key].value = value storage[key].date = Date.now() storage[key].expire = expire - return getFromCache(key) + return getValueBy(key) } - /** - * Stops searching for values to expire. - */ - const stopCache = () => clearInterval(timer) - /** * Get stored keys. * @returns {String[]} @@ -54,15 +54,14 @@ const Cache = () => { /** * Gets all key value pairs from cache. - * @returns {Array} Array of key value pairs. + * @returns {any[]} Array of key value pairs. */ - const getKeyValuePairs = () => getKeys().map((key) => storage[key].value) + const getKeyValuePairs = () => getKeys().map((key) => ({ key, value: storage[key].value })) return { - getFromCache, - getFromCacheAsync: (key) => Promise.resolve(getFromCache(key)), - storeInCache, - stopCache, + getValueBy, + getValueByAsync: (key) => Promise.resolve(getValueBy(key)), + store, getKeys, getKeyValuePairs } From 9845b5b5a10d74a19c79542cd7464fb402ed6d8d Mon Sep 17 00:00:00 2001 From: narekhovhannisyan Date: Sun, 9 Feb 2020 17:54:54 +0400 Subject: [PATCH 2/4] types: init cache. --- src/types/cache.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/types/cache.js diff --git a/src/types/cache.js b/src/types/cache.js new file mode 100644 index 0000000..8da3d5a --- /dev/null +++ b/src/types/cache.js @@ -0,0 +1,10 @@ +/** + * @typedef {Object} TCacheMethods + * @property {(key: String) => any} getValueBy + * @property {(key: String) => Promise.} getValueByAsync + * @property {(key: String, expire?: Number) => (value: any) => any} store + * @property {() => String[]} getKeys + * @property {() => any[]} getKeyValuePairs + */ + +module.exports = {} From 5072cdf4baf9a01e661cba8371e3157a898bbc98 Mon Sep 17 00:00:00 2001 From: narekhovhannisyan Date: Sun, 9 Feb 2020 17:55:09 +0400 Subject: [PATCH 3/4] src: in index use updated version of cache. --- src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 0d0c5a0..3006b87 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ const NodeSSH = require('node-ssh') -const { getFromCacheAsync, storeInCache } = require('./util/cache').SINGLETON +const { getValueByAsync, store } = require('./util/cache').SINGLETON /** * @typedef {import('./types/ssh').TNodeSSH} TNodeSSH @@ -14,11 +14,11 @@ const { getFromCacheAsync, storeInCache } = require('./util/cache').SINGLETON * @return {Promise.} SSH connection to desired remote. */ const connect = ({ domainOrIP, connectionExpire, ...sshOptions }) => - getFromCacheAsync(domainOrIP).then((connection) => { + getValueByAsync(domainOrIP).then((connection) => { if (!connection) { return new NodeSSH() .connect(sshOptions) - .then(storeInCache(domainOrIP, connectionExpire)) + .then(store(domainOrIP, connectionExpire)) } return connection From 65607b3f176ccac6e96348291fe1e639cfffeafb Mon Sep 17 00:00:00 2001 From: narekhovhannisyan Date: Sun, 9 Feb 2020 17:57:46 +0400 Subject: [PATCH 4/4] root: update readme file. --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index eae5acb..317be01 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ All connections are kept in cache and expire after given `connectionExpire` mill ## Usage ### For separate servers. -``` +```js const NodeSSHExtra = require('node-ssh-extra') NodeSSHExtra({ @@ -29,7 +29,7 @@ NodeSSHExtra({ ### If all servers have the same connection credentials. -``` +```js const NodeSSHExtra = require('node-ssh-extra').NodeSSHExtraForSameCredentials({ privateKey, host, @@ -43,7 +43,8 @@ NodeSSHExtra('mock-domain.com') // Do whatever you need to do here. }) -// And after if you need to do some other thing with new connection just call NodeSSHExtra the same way. +// And after if you need to do some other thing on the same server, just call NodeSSHExtra the same way. +// It will reuse the old opened connection if it's not expired, otherwise will create new connection. NodeSSHExtra('mock-domain.com') .then((connection) => {