本文介绍了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对象池C#是什么样子?

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

编辑:

推荐答案

您还可以通过使用

示例代码:

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对象池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-20 10:11