C# 并发字典未在 Azure 函数中添加或更新值

C# Concurrent Dictionary is not adding or updating values in Azure Function

我正在尝试将一些键值对缓存到并发字典中,以避免每次我需要该值时都去 cosmos。这是我的代码。

public class ActivityLogHubIngress
    {
        private readonly ICustomerDbService _customerDbService;
        
        private static ConcurrentDictionary<string, string> _customerIdLookup => new ConcurrentDictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);

        public ActivityLogHubIngress(ICustomerDbService customerDbService)
        {
            _customerDbService = customerDbService;
        }

        [FunctionName("ActivityLogHubIngress")]
        public async Task Run([EventHubTrigger("activity-log-events", Connection = "ActivityLogEventHub")] EventData[] events, ILogger log)
        {
         //Some code removed for brevity. At some point in this Azure Function the following line gets called to retrieve a customer ID.
         
         string customerId = await GetCustomerId(activityLogHubEvent.SubscriptionId, log); 

         //Some code removed for brevity. 
        }

private async Task<string> GetCustomerId(string subscriptionId, ILogger log = null)
        {
            if (_customerIdLookup.TryGetValue(subscriptionId, out string customerId))
            {
                return customerId;
            }

            var customer = await _customerDbService.GetCustomerBySubscription(subscriptionId);
            if(customer is null)
            {
                _customerIdLookup.AddOrUpdate(subscriptionId, string.Empty, (key, value) => string.Empty);

                if (log != null)
                {
                    log.LogInformation(ApplicationEvents.ActivityLogUnknownSubscriptionFailure,
                    $"Added Non-Customer ({subscriptionId}: null) to _customerIdLookup" +
                    $"\nListing _customerIdLookup dictionary (item count {_customerIdLookup.Count})" +
                    $"\n{string.Join("\n", _customerIdLookup.Keys.Select(k => $"{k}: {_customerIdLookup[k]}").ToList())}");
                }

                return null;
            }

            _customerIdLookup.AddOrUpdate(subscriptionId, customer.Id, (key, value) => customer.Id);

            if(log != null)
            {
                log.LogInformation(ApplicationEvents.ActivityLogUnknownSubscriptionFailure,
                $"Added ({subscriptionId}: {customer.Id}) to _customerIdLookup" +
                $"\nListing _customerIdLookup dictionary (item count {_customerIdLookup.Count})" +
                $"\n{string.Join("\n", _customerIdLookup.Keys.Select(k => $"{k}: {_customerIdLookup[k]}").ToList())}");
            }                

            return customer.Id;
        }

代码执行正常,但意图是

返回的值

var customer = await _customerDbService.GetCustomerBySubscription(subscriptionId);

将缓存在 _customerIdLookup 中,然后在下一个具有相同订阅 ID 的事件中通过字典访问,而不是转到 cosmos。然而,_customerIdLookup 仍然是空的,代码总是通过 Cosmos 路由,即使我发送了多个具有相同订阅 ID 的事件。

我所有的日志都是这样的。

Added (84b*****-****-****-****-***********0f: 12345678) to _customerIdLookup
Listing _customerIdLookup dictionary (item count 0)

项目计数始终为零。

没有写入词典是我做错了什么?

问题出在你的字典声明上:

private static ConcurrentDictionary<string, string> _customerIdLookup => new ConcurrentDictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);

注意 lambda 符号 =>,这意味着每次访问成员时都会返回一个新实例。