Skip to content

Commit

Permalink
Merge pull request #2 from narekhovhannisyan/pass-connection-expire
Browse files Browse the repository at this point in the history
update cache util
  • Loading branch information
narekhovhannisyan authored Feb 9, 2020
2 parents d0ce08f + 65607b3 commit 6f70a1a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -29,7 +29,7 @@ NodeSSHExtra({

### If all servers have the same connection credentials.

```
```js
const NodeSSHExtra = require('node-ssh-extra').NodeSSHExtraForSameCredentials({
privateKey,
host,
Expand All @@ -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) => {
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,11 +14,11 @@ const { getFromCacheAsync, storeInCache } = require('./util/cache').SINGLETON
* @return {Promise.<TNodeSSH>} 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
Expand Down
10 changes: 10 additions & 0 deletions src/types/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @typedef {Object} TCacheMethods
* @property {(key: String) => any} getValueBy
* @property {(key: String) => Promise.<any>} getValueByAsync
* @property {(key: String, expire?: Number) => (value: any) => any} store
* @property {() => String[]} getKeys
* @property {() => any[]} getKeyValuePairs
*/

module.exports = {}
31 changes: 15 additions & 16 deletions src/util/cache.js
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -24,28 +29,23 @@ 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`
* @param {String} key The key for storing data.
* @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[]}
Expand All @@ -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
}
Expand Down

0 comments on commit 6f70a1a

Please sign in to comment.