当前位置: 首页>>代码示例>>C#>>正文


C# RedisConnection.SetKeepAlive方法代码示例

本文整理汇总了C#中BookSleeve.RedisConnection.SetKeepAlive方法的典型用法代码示例。如果您正苦于以下问题:C# RedisConnection.SetKeepAlive方法的具体用法?C# RedisConnection.SetKeepAlive怎么用?C# RedisConnection.SetKeepAlive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BookSleeve.RedisConnection的用法示例。


在下文中一共展示了RedisConnection.SetKeepAlive方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ConnectToNodes

        private static void ConnectToNodes(TextWriter log, string tieBreakerKey, int syncTimeout, int keepAlive, bool allowAdmin, string clientName, string[] arr, List<RedisConnection> connections, out Task<string>[] infos, out Task<string>[] aux, AuxMode mode)
        {
            TraceWriteTime("Infos");
            infos = new Task<string>[arr.Length];
            aux = new Task<string>[arr.Length];
            var opens = new Task[arr.Length];
            for (int i = 0; i < arr.Length; i++)
            {
                var option = arr[i];
                if (string.IsNullOrWhiteSpace(option)) continue;

                RedisConnection conn = null;
                try
                {

                    var parts = option.Split(':');
                    if (parts.Length == 0) continue;

                    string host = parts[0].Trim();
                    int port = 6379, tmp;
                    if (parts.Length > 1 && int.TryParse(parts[1].Trim(), out tmp)) port = tmp;
                    conn = new RedisConnection(host, port, syncTimeout: syncTimeout, allowAdmin: allowAdmin);
                    conn.Name = clientName;
                    log.WriteLine("Opening connection to {0}:{1}...", host, port);
                    if (keepAlive >= 0) conn.SetKeepAlive(keepAlive);
                    opens[i] = conn.Open();
                    var info = conn.GetInfoImpl(null, false, false);
                    connections.Add(conn);
                    infos[i] = info;
                    switch (mode)
                    {
                        case AuxMode.TieBreakers:
                            if (tieBreakerKey != null)
                            {
                                aux[i] = conn.Strings.GetString(0, tieBreakerKey);
                            }
                            break;
                        case AuxMode.ClusterNodes:
                            aux[i] = conn.Cluster.GetNodes();
                            break;
                    }

                }
                catch (Exception ex)
                {
                    if (conn == null)
                    {
                        log.WriteLine("Error parsing option \"{0}\": {1}", option, ex.Message);
                    }
                    else
                    {
                        log.WriteLine("Error connecting: {0}", ex.Message);
                    }
                }
            }

            TraceWriteTime("Wait for infos");
            RedisConnectionBase.Trace("select-create", "wait...");
            var watch = new Stopwatch();
            foreach (Task task in infos.Concat(aux).Concat(opens))
            {
                if (task != null)
                {
                    try
                    {
                        int remaining = unchecked((int)(syncTimeout - watch.ElapsedMilliseconds));
                        if (remaining > 0) task.Wait(remaining);
                    }
                    catch { }
                }
            }
            watch.Stop();
            RedisConnectionBase.Trace("select-create", "complete");
        }
开发者ID:raycode,项目名称:booksleeve,代码行数:74,代码来源:ConnectionUtils.cs

示例2: SelectAndCreateConnection


//.........这里部分代码省略.........
                                log.WriteLine("Sentinel {0}:{1} nominates {2}:{3}", conn.Host, conn.Port, master.Item1, master.Item2);
                                int count;
                                if (discoveredPairs.TryGetValue(master, out count)) count = 0;
                                discoveredPairs[master] = count + 1;
                            }
                        } catch (Exception ex) {
                            log.WriteLine("Error from sentinel {0}:{1} - {2}", conn.Host, conn.Port, ex.Message);
                        }
                    }
                    Tuple<string, int> finalChoice;
                    switch (discoveredPairs.Count)
                    {
                        case 0:
                            log.WriteLine("No sentinels nominated a master; unable to connect");
                            finalChoice = null;
                            break;
                        case 1:
                            finalChoice = discoveredPairs.Single().Key;
                            log.WriteLine("Sentinels nominated unanimous master: {0}:{1}", finalChoice.Item1, finalChoice.Item2);
                            break;
                        default:
                            finalChoice = discoveredPairs.OrderByDescending(kvp => kvp.Value).First().Key;
                            log.WriteLine("Sentinels nominated multiple masters; choosing arbitrarily: {0}:{1}", finalChoice.Item1, finalChoice.Item2);
                            break;
                    }

                    if (finalChoice != null)
                    {
                        RedisConnection toBeDisposed = null;
                        try
                        { // good bet that in this scenario the input didn't specify any actual redis servers, so we'll assume open a new one
                            log.WriteLine("Opening nominated master: {0}:{1}...", finalChoice.Item1, finalChoice.Item2);
                            toBeDisposed = new RedisConnection(finalChoice.Item1, finalChoice.Item2, allowAdmin: allowAdmin, syncTimeout: syncTimeout);
                            if (keepAlive >= 0) toBeDisposed.SetKeepAlive(keepAlive);
                            toBeDisposed.Wait(toBeDisposed.Open());
                            if (toBeDisposed.ServerType == ServerType.Master)
                            {
                                var tmp = toBeDisposed;
                                toBeDisposed = null; // so we don't dispose it
                                selectedConfiguration = tmp.Host + ":" + tmp.Port;
                                availableEndpoints = new string[] { selectedConfiguration };
                                return tmp;
                            }
                            else
                            {
                                log.WriteLine("Server is {0} instead of a master", toBeDisposed.ServerType);
                            }
                        }
                        catch (Exception ex)
                        {
                            log.WriteLine("Error: {0}", ex.Message);
                        }
                        finally
                        { // dispose if something went sour
                            using (toBeDisposed) { }
                        }
                    }
                    // something went south; BUT SENTINEL WINS TRUMPS; quit now
                    selectedConfiguration = null;
                    availableEndpoints = new string[0];
                    return null;
                }

                TraceWriteTime("Check tie-breakers");
                // check for tie-breakers (i.e. when we store which is the master)
                switch (breakerScores.Count)
开发者ID:raycode,项目名称:booksleeve,代码行数:67,代码来源:ConnectionUtils.cs


注:本文中的BookSleeve.RedisConnection.SetKeepAlive方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。