Skip to content

Commit

Permalink
Cache topics instead of types in PolymorphicSubscriptionStore
Browse files Browse the repository at this point in the history
  • Loading branch information
SzymonPobiega committed Nov 14, 2019
1 parent 6d01d7a commit de3d7da
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/NServiceBus.SqlServer/PubSub/PolymorphicSubscriptionStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public PolymorphicSubscriptionStore(SubscriptionTable subscriptionTable)

public Task<List<string>> GetSubscribers(Type eventType)
{
var topics = GetTopicsFor(eventType);
var topics = GetTopics(eventType);
return subscriptionTable.GetSubscribers(topics.ToArray());
}

Expand All @@ -29,14 +29,16 @@ public Task Unsubscribe(string endpointName, Type eventType)
return subscriptionTable.Unsubscribe(endpointName, TopicName.From(eventType));
}

IEnumerable<string> GetTopicsFor(Type messageType)
IEnumerable<string> GetTopics(Type messageType)
{
return GetMessageHierarchy(messageType).Select(TopicName.From);
return eventTypeToTopicListMap.GetOrAdd(messageType, GenerateTopics);
}

IEnumerable<Type> GetMessageHierarchy(Type messageType)
static string[] GenerateTopics(Type messageType)
{
return messageHierarchyCache.GetOrAdd(messageType, t => GenerateMessageHierarchy(t).ToArray());
return GenerateMessageHierarchy(messageType)
.Select(TopicName.From)
.ToArray();
}

static IEnumerable<Type> GenerateMessageHierarchy(Type messageType)
Expand All @@ -53,7 +55,7 @@ static IEnumerable<Type> GenerateMessageHierarchy(Type messageType)
}
}

ConcurrentDictionary<Type, Type[]> messageHierarchyCache = new ConcurrentDictionary<Type, Type[]>();
ConcurrentDictionary<Type, string[]> eventTypeToTopicListMap = new ConcurrentDictionary<Type, string[]>();
SubscriptionTable subscriptionTable;
}
}

0 comments on commit de3d7da

Please sign in to comment.