Browse Source

Refactor ChatClientNameAttribute to reduce reflection calls

pull/23533/head
Enis Necipoglu 1 year ago
parent
commit
706d3e06ee
No known key found for this signature in database GPG Key ID: 1EC55E13241E1680
  1. 19
      framework/src/Volo.Abp.AI/Volo/Abp/AI/ChatClientNameAttribute.cs

19
framework/src/Volo.Abp.AI/Volo/Abp/AI/ChatClientNameAttribute.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Collections.Concurrent;
namespace Volo.Abp.AI; namespace Volo.Abp.AI;
@ -15,6 +16,8 @@ public class ChatClientNameAttribute : Attribute
Name = name; Name = name;
} }
private static readonly ConcurrentDictionary<Type, string> _nameCache = new();
public static string GetChatClientName<TChatClient>() public static string GetChatClientName<TChatClient>()
{ {
return GetChatClientName(typeof(TChatClient)); return GetChatClientName(typeof(TChatClient));
@ -22,16 +25,14 @@ public class ChatClientNameAttribute : Attribute
public static string GetChatClientName(Type chatClientType) public static string GetChatClientName(Type chatClientType)
{ {
var chatClientNameAttribute = chatClientType return _nameCache.GetOrAdd(chatClientType, type =>
.GetCustomAttributes(true)
.OfType<ChatClientNameAttribute>()
.FirstOrDefault();
if (chatClientNameAttribute != null)
{ {
return chatClientNameAttribute.Name; var chatClientNameAttribute = type
} .GetCustomAttributes(true)
.OfType<ChatClientNameAttribute>()
.FirstOrDefault();
return chatClientType.FullName!; return chatClientNameAttribute?.Name ?? type.FullName!;
});
} }
} }
Loading…
Cancel
Save