From eb834af8c02d1571a39feb77e0257c22f4866e5f Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Thu, 12 Sep 2024 12:03:04 +0300 Subject: [PATCH 1/3] RabbitMQ.Publish - Bug fix for Memory Cache --- Frends.RabbitMQ.Publish/CHANGELOG.md | 4 ++++ .../Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish.csproj | 2 +- Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Publish.cs | 6 +++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Frends.RabbitMQ.Publish/CHANGELOG.md b/Frends.RabbitMQ.Publish/CHANGELOG.md index c335e8d..6db11e8 100644 --- a/Frends.RabbitMQ.Publish/CHANGELOG.md +++ b/Frends.RabbitMQ.Publish/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## [1.4.0] - 2024-09-12 +### Fixed +- Fixed issue with MemoryCache error `Index was outside the bounds of the array` by adding try - catch block and changing how cache key is formatted. + ## [1.3.0] - 2024-09-06 ### Fixed - Fixed issue with simultaneous calls by storing connections to Memory.Cache. diff --git a/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish.csproj b/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish.csproj index d948c75..f4f6851 100644 --- a/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish.csproj +++ b/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish.csproj @@ -2,7 +2,7 @@ net6.0 - 1.3.0 + 1.4.0 Frends Frends Frends diff --git a/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Publish.cs b/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Publish.cs index b7d614c..a2ce213 100644 --- a/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Publish.cs +++ b/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Publish.cs @@ -264,6 +264,10 @@ private static string GetCacheKey(Connection connection) private static string GetCacheKeyFromMemoryCache(string cacheKey) { - return RabbitMQConnectionCache.ToList().Where(e => e.Key.Split("_")[1] == cacheKey).Select(e => e.Key).FirstOrDefault(); + try + { + return RabbitMQConnectionCache.ToList().Where(e => e.Key.Split("_")[0] == cacheKey).Select(e => e.Key).FirstOrDefault(); + } + catch { return null; } } } \ No newline at end of file From 0852af058bf4343026d4d6f138e3980d5e033324 Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Thu, 12 Sep 2024 12:09:31 +0300 Subject: [PATCH 2/3] Excluded Memory Cache from test coverage --- Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Publish.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Publish.cs b/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Publish.cs index a2ce213..14b2d7a 100644 --- a/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Publish.cs +++ b/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Publish.cs @@ -262,6 +262,7 @@ private static string GetCacheKey(Connection connection) return key; } + [ExcludeFromCodeCoverage] private static string GetCacheKeyFromMemoryCache(string cacheKey) { try From 7032abda49ee0f20724801039ab3bf23682d9623 Mon Sep 17 00:00:00 2001 From: Riku Virtanen Date: Thu, 12 Sep 2024 12:25:40 +0300 Subject: [PATCH 3/3] Coverage --- Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Publish.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Publish.cs b/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Publish.cs index 14b2d7a..f7075a1 100644 --- a/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Publish.cs +++ b/Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Publish.cs @@ -230,7 +230,7 @@ private static IConnection GetRabbitMQConnection(Connection connection, Connecti try { var rabbitMQConnection = new RabbitMQConnection { AMQPConnection = factory.CreateConnection() }; - RabbitMQConnectionCache.Add($"{Guid.NewGuid()}_{cacheKey}", rabbitMQConnection, new CacheItemPolicy() { RemovedCallback = RemovedCallback, SlidingExpiration = TimeSpan.FromSeconds(connection.ConnectionExpirationSeconds) }); + RabbitMQConnectionCache.Add($"{cacheKey}_{Guid.NewGuid()}", rabbitMQConnection, new CacheItemPolicy() { RemovedCallback = RemovedCallback, SlidingExpiration = TimeSpan.FromSeconds(connection.ConnectionExpirationSeconds) }); return rabbitMQConnection.AMQPConnection; } catch (Exception ex) @@ -248,6 +248,7 @@ private static IConnection GetRabbitMQConnection(Connection connection, Connecti return null; } + [ExcludeFromCodeCoverage] private static string GetCacheKey(Connection connection) { var key = $"{connection.Host}:";