Example #1
0
        public Task <IWord> GetWordInfo(string language, string word)
        {
            // Lock to prevent concurrent requests of the same word to the Dictionary API
            // If the word has no description, when the lock is over it should already have it.
            // next call in queue will not get an empty description on wordRepository.Get
            lock (padLock)
            {
                var getWordTask = wordRepository.Get(language, word);
                getWordTask.Wait();
                var result = getWordTask.Result;
                if (result == null)
                {
                    return(null);
                }

                if (string.IsNullOrEmpty(result.Description))
                {
                    var newWord = dictionaryApiFactory.UpdateDescription(result);
                    logger.Info($"Aquired new word from API: [{newWord.Name}] = [{newWord.Description}]");

                    Task.WaitAll(wordRepository.Update(newWord));
                    result = newWord;
                }
                return(Task.FromResult(result));
            }
        }
Example #2
0
        public async Task <IWord> GetWordInfo(string language, string word)
        {
            await EnsureCacheIsUpdated();

            // Lock to prevent concurrent requests of the same word to the Dictionary API
            // If the word has no description, when the lock is over it should already have it.
            // next call in queue will not get an empty description on GetWord
            lock (padLock)
            {
                var result = DictionaryCache.Instance.GetWord(language, word);
                if (result == null)
                {
                    return(null);
                }

                if (string.IsNullOrEmpty(result.Description))
                {
                    var newWord = dictionaryApiFactory.UpdateDescription(result);
                    DictionaryCache.Instance.AddOrUpdateWord(newWord);

                    logger.Info($"Aquired new word from API: [{newWord.Name}] = [{newWord.Description}]");

                    Task.WaitAll(wordRepository.Update(newWord));
                    result = newWord;
                }
                return(result);
            }
        }