internal static TranslationResult AddOrUpdate(TranslationCacheKey key, TranslationResult result)
        {
            if (result == null)
                return null;

            return _translationResultsCache.AddOrUpdate(key, result, (cacheKey, translationResult) => result);
        }
        internal static bool RemoveUserEditedItem(string translationSourceName, string sourceLang, string destinationLang,
            string originalText, string editedText)
        {
            if (string.IsNullOrEmpty(translationSourceName) || string.IsNullOrEmpty(destinationLang) || string.IsNullOrEmpty(originalText) || string.IsNullOrEmpty(editedText))
                return false;

            var originalTextClean = originalText.Trim();
            var editedTextClean = editedText.Trim();

            if (originalTextClean.Equals(editedTextClean, StringComparison.Ordinal))
                return false;

            var translationCacheKey = new TranslationCacheKey(translationSourceName, sourceLang, destinationLang, originalText);
            TranslationResult translationResult;
            if (!TryGetValue(translationCacheKey, out translationResult))
            {
                return false;
            }

            var dictionaryItem = translationResult.DictionaryItems.FirstOrDefault(p => p.Title.Equals(Constants.TranslationCache.UserEditedItemHeader, StringComparison.OrdinalIgnoreCase));
            if (dictionaryItem == null)
            {
                return false;
            }

            var result = dictionaryItem.Terms.Remove(editedTextClean);

            if (!dictionaryItem.Terms.Any())
                translationResult.DictionaryItems.Remove(dictionaryItem);

            return result;
        }
示例#3
0
        public async Task <IEnumerable <Translation> > Get(string languageKey, string projectKey)
        {
            var key = TranslationCacheKey.Create(languageKey, projectKey);
            IEnumerable <Translation> translations = null;

            if (_cache.TryGetValue(key.ToString(), out translations))
            {
                return(translations);
            }

            await s_semaphoreSlim.WaitAsync();

            try
            {
                if (_cache.TryGetValue(key.ToString(), out translations))
                {
                    return(translations.ToList());
                }

                translations = await _repository.GetAll(languageKey, projectKey);

                var cacheExpirationOptions = new MemoryCacheEntryOptions();
                cacheExpirationOptions.AbsoluteExpiration = DateTime.Now.AddHours(4);
                cacheExpirationOptions.Priority           = CacheItemPriority.Normal;

                _cache.Set(key.ToString(), translations, cacheExpirationOptions);
                return(translations.ToList());
            }
            finally
            {
                s_semaphoreSlim.Release();
            }
        }
        internal static bool AddUserEditedItem(string translationSourceName, string sourceLang, string destinationLang, string originalText, string editedText)
        {
            if(string.IsNullOrEmpty(translationSourceName) || string.IsNullOrEmpty(destinationLang) || string.IsNullOrEmpty(originalText) || string.IsNullOrEmpty(editedText))
                return false;

            var originalTextClean = originalText.Trim();
            var editedTextClean = editedText.Trim();

            if (originalTextClean.Equals(editedTextClean, StringComparison.Ordinal))
                return false;

            var translationCacheKey = new TranslationCacheKey(translationSourceName, sourceLang, destinationLang, originalText);
            TranslationResult translationResult;
            if (!TryGetValue(translationCacheKey, out translationResult))
            {
                translationResult = AddOrUpdate(translationCacheKey, new TranslationResult
                {
                    TranslationSource = translationSourceName,
                    SourceLanguage = sourceLang,
                    DestinationLanguage = destinationLang,
                    OriginalText = originalText,
                    FromCache = true
                });
            }

            var dictionaryItem = translationResult.DictionaryItems.FirstOrDefault(p => p.Title.Equals(Constants.TranslationCache.UserEditedItemHeader, StringComparison.OrdinalIgnoreCase));
            if (dictionaryItem == null)
            {
                dictionaryItem = new DictionaryItem
                {
                    Title = Constants.TranslationCache.UserEditedItemHeader
                };
                translationResult.DictionaryItems.Add(dictionaryItem);
            }

            var alreadyHasItem = dictionaryItem.Terms.Any(p=> p.Equals(editedTextClean, StringComparison.Ordinal));
            if (alreadyHasItem)
                return false;

            dictionaryItem.Terms.Add(editedTextClean);

            return true;
        }
	    public virtual async Task<TranslationResult> GetTranslationAsync(string text, string sourceLang, string destinationLang,
	        bool forceRemote = false)
	    {

            var cacheKey = new TranslationCacheKey(Name, sourceLang, destinationLang, text);
            TranslationResult result;
            if (!forceRemote && EnableCache && TranslationCache.TryGetValue(cacheKey, out result))
            {
                if (result != null)
                {
                    result.OriginalText = text;
                    result.FromCache = true;
                    return result;
                }
            }

	        result = await GetRemoteTranslationAsync(text, sourceLang, destinationLang);

            if (EnableCache)
                TranslationCache.AddOrUpdate(cacheKey, result);

            return result;
	    }
 internal static bool TryGetValue(TranslationCacheKey key, out TranslationResult result)
 {
     return _translationResultsCache.TryGetValue(key, out result);
 }
示例#7
0
        public void Clear(string languageKey, string projectKey)
        {
            var key = TranslationCacheKey.Create(languageKey, projectKey);

            _cache.Remove(key.ToString());
        }