Azure Redis缓存-ConnectionMultiplexer对象池 [英] Azure Redis Cache - pool of ConnectionMultiplexer objects

查看:355
本文介绍了Azure Redis缓存-ConnectionMultiplexer对象池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在应用程序中使用C1 Azure Redis缓存。最近,我们在GET操作上遇到了很多超时。

We are using C1 Azure Redis Cache in our application. Recently we are experiencing lots of time-outs on GET operations.

根据本文,一种可能的解决方案是实现ConnectionMultiplexer对象池。 / p>

According to this article, one of possible solutions is to implement pool of ConnectionMultiplexer objects.


另一种可能的解决方案是在客户端中使用ConnectionMultiplexer
对象池,然后选择负载最少的
发送新请求时使用ConnectionMultiplexer。这样应该可以防止
a单个超时导致其他请求也超时。

Another possible solution is to use a pool of ConnectionMultiplexer objects in your client, and choose the "least loaded" ConnectionMultiplexer when sending a new request. This should prevent a single timeout from causing other requests to also timeout.

如何使用以下方法实现ConnectionMultiplexer对象池C#是什么样子?

How would implementation of a pool of ConnectionMultiplexer objects using C# look like?

编辑:

我最近问过的相关问题。

推荐答案

您还可以通过使用 StackExchange以更简单的方式完成此任务.Redis.Extensions

示例代码:

using StackExchange.Redis;
using StackExchange.Redis.Extensions.Core.Abstractions;
using StackExchange.Redis.Extensions.Core.Configuration;
using System;
using System.Collections.Concurrent;
using System.Linq;

namespace Pool.Redis
{
/// <summary>
/// Provides redis pool
/// </summary>
public class RedisConnectionPool : IRedisCacheConnectionPoolManager
{
    private static ConcurrentBag<Lazy<ConnectionMultiplexer>> connections;
    private readonly RedisConfiguration redisConfiguration;

    public RedisConnectionPool(RedisConfiguration redisConfiguration)
    {
        this.redisConfiguration = redisConfiguration;
        Initialize();
    }

    public IConnectionMultiplexer GetConnection()
    {
        Lazy<ConnectionMultiplexer> response;
        var loadedLazys = connections.Where(lazy => lazy.IsValueCreated);

        if (loadedLazys.Count() == connections.Count)
        {
            response = connections.OrderBy(x => x.Value.GetCounters().TotalOutstanding).First();
        }
        else
        {
            response = connections.First(lazy => !lazy.IsValueCreated);
        }

        return response.Value;
    }

    private void Initialize()
    {
        connections = new ConcurrentBag<Lazy<ConnectionMultiplexer>>();

        for (int i = 0; i < redisConfiguration.PoolSize; i++)
        {
            connections.Add(new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(redisConfiguration.ConfigurationOptions)));
        }
    }

    public void Dispose()
    {
        var activeConnections = connections.Where(lazy => lazy.IsValueCreated).ToList();
        activeConnections.ForEach(connection => connection.Value.Dispose());
        Initialize();
    }
}

}

其中 RedisConfiguration 是这样的:

            return new RedisConfiguration()
        {
            AbortOnConnectFail = true,
            Hosts = new RedisHost[] {
                                      new RedisHost() 
                                      {
                                          Host = ConfigurationManager.AppSettings["RedisCacheAddress"].ToString(),
                                          Port = 6380
                                      },
                                    },
            ConnectTimeout = Convert.ToInt32(ConfigurationManager.AppSettings["RedisTimeout"].ToString()),
            Database = 0,
            Ssl = true,
            Password = ConfigurationManager.AppSettings["RedisCachePassword"].ToString(),
            ServerEnumerationStrategy = new ServerEnumerationStrategy()
            {
                Mode = ServerEnumerationStrategy.ModeOptions.All,
                TargetRole = ServerEnumerationStrategy.TargetRoleOptions.Any,
                UnreachableServerAction = ServerEnumerationStrategy.UnreachableServerActionOptions.Throw
            },
            PoolSize = 50
        };

这篇关于Azure Redis缓存-ConnectionMultiplexer对象池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆