public async Task Start(Chatty newChatty, ChattyLolCounts newChattyLolCounts)
        {
            if (_chatty != null || _chattyLolCounts != null)
            {
                throw new InvalidOperationException($"{nameof(EventProvider)} already started.");
            }

            await _lock.WithWriteLock(nameof(Start),
                                      action : () =>
            {
                _chatty          = newChatty;
                _chattyLolCounts = newChattyLolCounts;
            });
        }
        public async Task CycleSharedHttpClients()
        {
            await _anonymousHttpClientLock.WithWriteLock(nameof(CycleSharedHttpClients), () =>
            {
                _anonymousHttpClient.Dispose();
                _anonymousHttpClient = CreateHttpClient();
            });

            await _sharedHttpClientLock.WithWriteLock(nameof(CycleSharedHttpClients), () =>
            {
                _sharedHttpClient.Dispose();
                _sharedHttpClient = CreateHttpClient();
            });
        }
Exemplo n.º 3
0
        public async Task <int> GetUserIdFromName(string userName)
        {
            await _userIdCacheLock.WithWriteLock(nameof(GetUserIdFromName) + userName, () =>
            {
                if (_cacheExpire < DateTime.UtcNow)
                {
                    _cacheExpire = DateTime.UtcNow.AddHours(CACHE_TTL_HOURS);
                    _userNameToIdMapCache.Clear();
                }
            });

            var id = await _userIdCacheLock.WithReadLock(nameof(GetUserIdFromName) + userName, () =>
            {
                if (_userNameToIdMapCache.TryGetValue(userName, out var cachedId))
                {
                    return((int?)cachedId);
                }
                else
                {
                    return(default(int?));
                }
            });

            if (id.HasValue)
            {
                return(id.Value);
            }

            var results = await _searchParser.Search(string.Empty, userName, string.Empty, string.Empty, 0);

            if (results.Results.Count > 0)
            {
                var threadId = results.Results[0].Id;
                var thread   = await _threadParser.GetThread(threadId);

                var userPost = thread.Posts.FirstOrDefault(p => p.Author.Equals(userName, System.StringComparison.OrdinalIgnoreCase));
                if (userPost != null)
                {
                    await _userIdCacheLock.WithWriteLock(nameof(GetUserIdFromName) + userName, () =>
                    {
                        _userNameToIdMapCache[userName] = userPost.AuthorId;
                    });

                    return(userPost.AuthorId);
                }
            }

            throw new System.Exception($"Cannot find user id for {userName}");
        }