From 55c6f1481576a1c9d439186daa18ed076f72bf5b Mon Sep 17 00:00:00 2001 From: luin Date: Sat, 3 Dec 2016 15:41:10 +0800 Subject: [PATCH] feat: support Elastic Cache Redis & RedisLabs for selecting dbs Closes: #16, #33, #50 --- .../main/Main/Database/KeyBrowser/Footer.jsx | 39 ++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/client/components/main/Main/Database/KeyBrowser/Footer.jsx b/client/components/main/Main/Database/KeyBrowser/Footer.jsx index 4ebb47c2..60f9c929 100644 --- a/client/components/main/Main/Database/KeyBrowser/Footer.jsx +++ b/client/components/main/Main/Database/KeyBrowser/Footer.jsx @@ -10,6 +10,7 @@ class Footer extends React.Component { componentDidMount() { this.updateInfo(); + this.updateDBCount(); this.interval = setInterval(this.updateInfo.bind(this), 10000); } @@ -19,6 +20,25 @@ class Footer extends React.Component { } } + updateDBCount() { + this.props.redis.config('get', 'databases', (err, res) => { + if (!err) { + if (res[1]) { + this.setState({ databases: Number(res[1]) }); + } else { + const redis = this.props.redis.duplicate(); + const select = redis.select.bind(redis); + this.guessDatabaseNumber(select, 15).then((count) => { + console.log('===', count) + return typeof count === 'number' ? count : this.guessDatabaseNumber(select, 1, 0); + }).then((count) => { + this.setState({ databases: count + 1 }); + }); + } + } + }); + } + updateInfo() { this.props.redis.info((err, res) => { if (err) { @@ -36,12 +56,21 @@ class Footer extends React.Component { this.setState(info); }); + } - this.props.redis.config('get', 'databases', (err, res) => { - if (res && res[1]) { - this.setState({ databases: Number(res[1]) }); + guessDatabaseNumber(select, startIndex, lastSuccessIndex) { + if (startIndex > 30) { + return Promise.resolve(30); + } + return select(startIndex) + .then(() => { + return this.guessDatabaseNumber(select, startIndex + 1, startIndex); + }).catch((err) => { + if (typeof lastSuccessIndex === 'number') { + return lastSuccessIndex; } - }); + return null; + }) } componentWillUnmount() { @@ -87,7 +116,7 @@ class Footer extends React.Component { >{i}); } return items; - })(this.state.databases) + })(this.state.databases || 1) }