zoukankan      html  css  js  c++  java
  • StackService.Redis 应用

    如今StackService.Redis已经转向商业版本。4.0以下的低版本依然免费和开源。

    吴双,Redis系列命令拾遗分享  http://www.cnblogs.com/tdws/tag/NoSql/

    可以在NuGet中获取StackExchange.Redis类库

            static void Main(string[] args)
            {
                Task.Run(() => SetRedis());
                //GetRedis();
               // Task.Run(() => GetRedis());
                Console.ReadKey();
            }
    
            /// <summary>
            /// 获取ConnectionMultiplexer
            /// </summary>
            /// <returns></returns>
            public static ConnectionMultiplexer TredisInfo()
            {
                ConfigurationOptions options = new ConfigurationOptions();
                options.EndPoints.Add("xxx.xxx.x.xx:7000");
                options.EndPoints.Add("xxx.xxx.x.xx:7001");
                options.EndPoints.Add("xxx.xxx.x.xx:7002");
                options.EndPoints.Add("xxx.xxx.x.xx:7003");
                options.EndPoints.Add("xxx.xxx.x.xx:7004");
                options.EndPoints.Add("xxx.xxx.x.xx:7005");
                var redisClient = ConnectionMultiplexer.Connect(options);
                return redisClient;
            }
    
            /// <summary>
            /// 获取redis存储数据
            /// </summary>
            public static void  GetRedis()
            {
                try
                {
                    var multiplexer = TredisInfo();
                    var client = multiplexer.GetDatabase();
                    for (int i = 0; i < 600; i++)
                    {
                        var result =  client.StringGet("TCP-" + i);
                        Console.WriteLine(result);
                        Thread.Sleep(10);
                    }
                    multiplexer.Dispose();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
         
            }
    
            /// <summary>
            /// 向redis中存入数据
            /// </summary>
            /// <returns></returns>
            public static async Task SetRedis()
            {
                try
                {
                    var multiplexer = TredisInfo();
                    var client = multiplexer.GetDatabase();
                    for (int i = 0; i < 600; i++)
                    {
                       await client.StringSetAsync("TCP-" + i, "beijing欢迎你" + i);
                        Thread.Sleep(30);
                    }
                    multiplexer.Dispose();
                    Console.WriteLine("执行完毕");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
    
            }

    注意:redis集群进行大量数据处理时,需要进行线程休息,否则会出去错误

  • 相关阅读:
    如何动态调用WebServices
    Cache及(HttpRuntime.Cache与HttpContext.Current.Cache)
    SQL创建索引(转)
    TSQL用法四:OpenDataSource, OpenRowSet
    AppDomain动态加载程序集
    hdu 2544 最短路
    hdu 1151 Air Raid
    hdu3790 最短路径问题
    hdu 1548 A strange lift
    对于 前K短路径问题 和 A*算法 的一些小小总结
  • 原文地址:https://www.cnblogs.com/xiaoyaodijun/p/6551312.html
Copyright © 2011-2022 走看看