Azure上托管的ASP.NET核心应用程序中的Redis连接出错

时间:2017-06-01 01:33:35

标签: asp.net azure caching redis asp.net-core

我们正面临Redis缓存问题,并且导致我们网站崩溃。

以下是我们实施它的方式:

我们使用了以下连接字符串:

"*******.redis.cache.windows.net:6380,password=*****=,ssl=True,abortConnect=False"

我们创建了一个服务类:

using Microsoft.Extensions.Options;
using SarahahDataAccessLayer;
using StackExchange.Redis;
using System;

namespace Sarahah.Services
{
    public class RedisService
    {
        private static Lazy<ConnectionMultiplexer> lazyConnection;
        private readonly ApplicationSettings _settings;
        public RedisService(IOptions<ApplicationSettings> settings)
        {
            _settings = settings.Value;
            lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
            {
                return ConnectionMultiplexer.Connect(_settings.RedisConnection);
            });
        }



        public  ConnectionMultiplexer Connection
        {
            get
            {
                return lazyConnection.Value;
            }
        }
    }
}

然后在Startup.cs中我使用以下内容:

services.AddSingleton<RedisService>();

然后在控制器中我们使用依赖注入,我们分配给多路复用器:

connectionMultiplexer = redisService.Connection;

这是我们从缓存中获取的方式:

 private async Task<string> GetFromCache(string key)
    {
        if (connectionMultiplexer.IsConnected)
        {
            var cache = connectionMultiplexer.GetDatabase();

                return await cache.StringGetAsync(key);
        }
        else
        {
            return null;
        }
    }

这是我们删除的方式:

  private async Task DeleteFromCache(string subdomain)
    {

            if (connectionMultiplexer.IsConnected)
            {
                var cache = connectionMultiplexer.GetDatabase();
                await cache.KeyDeleteAsync(subdomain).ConfigureAwait(false);
            }
    }

这是我们添加的方式:

 {
        if (connectionMultiplexer.IsConnected)
        {
            var cache = connectionMultiplexer.GetDatabase();

                TimeSpan expiresIn;
                // Search Cache
                if (key.Contains("-"))
                {
                    expiresIn = new TimeSpan(0, GetMessagesCacheExpiryMinutes, 0);
                }
                // User info cache
                else
                {
                    expiresIn = new TimeSpan(GetProfileCacheExpiryHours, 0, 0);
                }
                await cache.StringSetAsync(key, serializedData, expiresIn).ConfigureAwait(false);

        }

但是,我们收到以下错误: 没有可用于此操作的连接

虽然我们有很多用户,但我们在Azure门户中只看到很少的连接:

Very small number of connections

请注意,我们在网络应用的同一区域托管了redis缓存。

非常感谢您的支持。

2 个答案:

答案 0 :(得分:2)

每次依赖注入调用实例化RedisService类时,代码最终都会向lazyConnection分配一个新的Lazy<ConnectionMultiplexer>,从而导致新的连接以及连接泄漏,因为您没有调用Close()或Dispose()在旧的lazyConnection上。

尝试更改您的代码:

在Startup.cs中:

public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            .........<whatever you have here>
            services.AddSingleton<RedisService>();
            services.Configure<ApplicationSettings>(options => Configuration.GetSection("ApplicationSettings").Bind(options));
        }

RedisService.cs

public class RedisService
{
    private readonly ApplicationSettings _settings;
    private static Lazy<ConnectionMultiplexer> lazyConnection;
    static object connectLock = new object();

    public RedisService(IOptions<ApplicationSettings> settings)
    {
        _settings = settings.Value;
        if (lazyConnection == null)
        {
            lock (connectLock)
            {
                if (lazyConnection == null)
                {
                    lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
                    {
                        return ConnectionMultiplexer.Connect(_settings.RedisConnection);
                    });
                }
            }
        }
    }

    public static ConnectionMultiplexer Connection
    {
        get
        {
            return lazyConnection.Value;
        }
    }
}

ApplicationSettings.cs

public class ApplicationSettings
    {
        public string RedisConnection { get; set; }
    }

appsettings.json

{
    "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
            "Default": "Debug",
            "System": "Information",
            "Microsoft": "Information"
        }
    },
    "ApplicationSettings": {
        "RedisConnection": "yourcachename.redis.cache.windows.net:6380,password=yourpassword,ssl=True,abortConnect=False,syncTimeout=4000"
    }
}

HomeController.cs

public class HomeController : Controller
    {
        private RedisService redisService;
        private ConnectionMultiplexer connectionMultiplexer;
        public HomeController(IOptions<ApplicationSettings> settings)
        {
            redisService = new RedisService(settings);
            connectionMultiplexer = RedisService.Connection;
        }
        public IActionResult Index()
        {
            AddToCache("foo1", "bar").GetAwaiter().GetResult();

            return View();
        }

        private async Task<string> GetFromCache(string key)
        {
            if (connectionMultiplexer.IsConnected)
            {
                var cache = connectionMultiplexer.GetDatabase();

                return await cache.StringGetAsync(key);
            }
            else
            {
                return null;
            }
        }

        private async Task DeleteFromCache(string subdomain)
        {
            if (connectionMultiplexer.IsConnected)
            {
                var cache = connectionMultiplexer.GetDatabase();
                await cache.KeyDeleteAsync(subdomain).ConfigureAwait(false);
            }
        }

        private async Task AddToCache(string key, string serializedData)
        {
            var GetMessagesCacheExpiryMinutes = 5;
            var GetProfileCacheExpiryHours = 1;
            if (connectionMultiplexer.IsConnected)
            {
                var cache = connectionMultiplexer.GetDatabase();

                TimeSpan expiresIn;
                // Search Cache
                if (key.Contains("-"))
                {
                    expiresIn = new TimeSpan(0, GetMessagesCacheExpiryMinutes, 0);
                }
                // User info cache
                else
                {
                    expiresIn = new TimeSpan(GetProfileCacheExpiryHours, 0, 0);
                }
                await cache.StringSetAsync(key, serializedData, expiresIn).ConfigureAwait(false);

            }
        }

答案 1 :(得分:0)

我认为这篇文章https://gist.github.com/JonCole/317fe03805d5802e31cfa37e646e419d对于使用azureRedis的用户可能会很有趣。 如果使用StackExchange.Redis,则可能会遇到connectionMultiplexer无法重新连接到Redis的情况,因此您应该实现重试逻辑。本文中有更多详细信息。