public async Task <IDatabase> GetDatabaseAsync(int dbNumber = 0, bool allowAdmin = false) { if (!_probeComplete) { _probeComplete = await Probe(); } if (dbNumber < 0 || dbNumber > _maxDb) { throw new IndexOutOfRangeException( $"DB number must be 0 to {_maxDb}."); } RedisConnector rc = new RedisConnector(_configurationOptions, allowAdmin); return(rc.Connection.GetDatabase(dbNumber)); }
async Task <bool> Probe() { ConfigurationOptions configDB0 = _configurationOptions.Clone(); configDB0.DefaultDatabase = 0; RedisConnector connectorDB0 = null; try { connectorDB0 = new RedisConnector(configDB0, true); } catch (Exception ex) { throw new RedisHelperException( "Cannot create DbConnection.", ex); } IDatabase db0 = null; try { db0 = connectorDB0.Connection.GetDatabase(0); } catch (Exception ex) { throw new RedisHelperException("Cannot GetDatabase(0).", ex); } TimeSpan pingDuration = default(TimeSpan); try { pingDuration = await db0.PingAsync(); } catch (Exception ex) { throw new RedisHelperException("Cannot ping db0.", ex); } try { _server = connectorDB0.Connection.GetServer( configDB0.EndPoints[0]); _features = _server.Features; } catch (Exception ex) { throw new RedisHelperException( "Cannot get server features.", ex); } try { IGrouping <string, KeyValuePair <string, string> >[] info = await _server.InfoAsync("Server"); if (info[0].Key.Equals("Server", StringComparison.CurrentCultureIgnoreCase)) { foreach (KeyValuePair <string, string> kvp in info[0]) { if (kvp.Key.Equals("redis_version", StringComparison.CurrentCultureIgnoreCase)) { _version = kvp.Value; break; } } } } catch (Exception ex) { throw new RedisHelperException( "Cannot get redis_version.", ex); } sbyte maxDb = 0; do { try { IDatabase nxtDb = connectorDB0 .Connection.GetDatabase(maxDb); RedisResult rr = await nxtDb.ExecuteAsync("PING"); if (rr == null || rr.IsNull || !rr.ToString().Equals( "PONG", StringComparison.CurrentCultureIgnoreCase)) { break; } ++maxDb; } catch { // expected... swallow break; } } while (maxDb < sbyte.MaxValue); _maxDb = maxDb; return(true); }