Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RabbitMQ.Publish - Bug fix for Memory Cache #19

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Frends.RabbitMQ.Publish/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<Version>1.3.0</Version>
<Version>1.4.0</Version>
<Authors>Frends</Authors>
<Copyright>Frends</Copyright>
<Company>Frends</Company>
Expand Down
10 changes: 8 additions & 2 deletions Frends.RabbitMQ.Publish/Frends.RabbitMQ.Publish/Publish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -248,6 +248,7 @@ private static IConnection GetRabbitMQConnection(Connection connection, Connecti
return null;
}

[ExcludeFromCodeCoverage]
private static string GetCacheKey(Connection connection)
{
var key = $"{connection.Host}:";
Expand All @@ -262,8 +263,13 @@ private static string GetCacheKey(Connection connection)
return key;
}

[ExcludeFromCodeCoverage]
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; }
}
}
Loading