Skip to content

Commit 6f70a1a

Browse files
Merge pull request #2 from narekhovhannisyan/pass-connection-expire
update cache util
2 parents d0ce08f + 65607b3 commit 6f70a1a

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ All connections are kept in cache and expire after given `connectionExpire` mill
1010
## Usage
1111

1212
### For separate servers.
13-
```
13+
```js
1414
const NodeSSHExtra = require('node-ssh-extra')
1515

1616
NodeSSHExtra({
@@ -29,7 +29,7 @@ NodeSSHExtra({
2929

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

32-
```
32+
```js
3333
const NodeSSHExtra = require('node-ssh-extra').NodeSSHExtraForSameCredentials({
3434
privateKey,
3535
host,
@@ -43,7 +43,8 @@ NodeSSHExtra('mock-domain.com')
4343
// Do whatever you need to do here.
4444
})
4545

46-
// And after if you need to do some other thing with new connection just call NodeSSHExtra the same way.
46+
// And after if you need to do some other thing on the same server, just call NodeSSHExtra the same way.
47+
// It will reuse the old opened connection if it's not expired, otherwise will create new connection.
4748

4849
NodeSSHExtra('mock-domain.com')
4950
.then((connection) => {

src/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const NodeSSH = require('node-ssh')
22

3-
const { getFromCacheAsync, storeInCache } = require('./util/cache').SINGLETON
3+
const { getValueByAsync, store } = require('./util/cache').SINGLETON
44

55
/**
66
* @typedef {import('./types/ssh').TNodeSSH} TNodeSSH
@@ -14,11 +14,11 @@ const { getFromCacheAsync, storeInCache } = require('./util/cache').SINGLETON
1414
* @return {Promise.<TNodeSSH>} SSH connection to desired remote.
1515
*/
1616
const connect = ({ domainOrIP, connectionExpire, ...sshOptions }) =>
17-
getFromCacheAsync(domainOrIP).then((connection) => {
17+
getValueByAsync(domainOrIP).then((connection) => {
1818
if (!connection) {
1919
return new NodeSSH()
2020
.connect(sshOptions)
21-
.then(storeInCache(domainOrIP, connectionExpire))
21+
.then(store(domainOrIP, connectionExpire))
2222
}
2323

2424
return connection

src/types/cache.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @typedef {Object} TCacheMethods
3+
* @property {(key: String) => any} getValueBy
4+
* @property {(key: String) => Promise.<any>} getValueByAsync
5+
* @property {(key: String, expire?: Number) => (value: any) => any} store
6+
* @property {() => String[]} getKeys
7+
* @property {() => any[]} getKeyValuePairs
8+
*/
9+
10+
module.exports = {}

src/util/cache.js

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
const storage = {}
1+
/**
2+
* @typedef {import('../types/cache').TCacheMethods} TCacheMethods
3+
*/
24

35
/**
46
* Key-value based cache with expire feature.
7+
* @returns {TCacheMethods}
58
*/
69
const Cache = () => {
10+
const storage = {}
11+
712
/**
813
* Checks if the key is close to expire.
914
* @private
1015
*/
11-
const timer = setInterval(() => {
16+
setInterval(() => {
1217
const now = Date.now()
1318
for (const key in storage) {
1419
const { date, expire } = storage[key]
@@ -24,28 +29,23 @@ const Cache = () => {
2429
* @param {String} key The key for storing data.
2530
* @returns {any}
2631
*/
27-
const getFromCache = (key) => storage[key] ? storage[key].value : undefined
32+
const getValueBy = (key) => storage[key] ? storage[key].value : undefined
2833

2934
/**
3035
* Stores data with given `key`, `expire` and `value`
3136
* @param {String} key The key for storing data.
3237
* @param {Number} [expire] The seconds to expire.
3338
* @returns {(any) => any}
3439
*/
35-
const storeInCache = (key, expire = Infinity) => (value) => {
40+
const store = (key, expire = Infinity) => (value) => {
3641
storage[key] = {}
3742
storage[key].value = value
3843
storage[key].date = Date.now()
3944
storage[key].expire = expire
4045

41-
return getFromCache(key)
46+
return getValueBy(key)
4247
}
4348

44-
/**
45-
* Stops searching for values to expire.
46-
*/
47-
const stopCache = () => clearInterval(timer)
48-
4949
/**
5050
* Get stored keys.
5151
* @returns {String[]}
@@ -54,15 +54,14 @@ const Cache = () => {
5454

5555
/**
5656
* Gets all key value pairs from cache.
57-
* @returns {Array} Array of key value pairs.
57+
* @returns {any[]} Array of key value pairs.
5858
*/
59-
const getKeyValuePairs = () => getKeys().map((key) => storage[key].value)
59+
const getKeyValuePairs = () => getKeys().map((key) => ({ key, value: storage[key].value }))
6060

6161
return {
62-
getFromCache,
63-
getFromCacheAsync: (key) => Promise.resolve(getFromCache(key)),
64-
storeInCache,
65-
stopCache,
62+
getValueBy,
63+
getValueByAsync: (key) => Promise.resolve(getValueBy(key)),
64+
store,
6665
getKeys,
6766
getKeyValuePairs
6867
}

0 commit comments

Comments
 (0)