diff --git a/pdf2zh/cache.py b/pdf2zh/cache.py index c1daa34c..80d497be 100644 --- a/pdf2zh/cache.py +++ b/pdf2zh/cache.py @@ -45,6 +45,9 @@ def _sort_dict_recursively(obj): return obj def __init__(self, translate_engine: str, translate_engine_params: dict = None): + assert ( + len(translate_engine) < 20 + ), "current cache require translate engine name less than 20 characters" self.translate_engine = translate_engine self.replace_params(translate_engine_params) diff --git a/pdf2zh/translator.py b/pdf2zh/translator.py index 3f34de59..57b2a15a 100644 --- a/pdf2zh/translator.py +++ b/pdf2zh/translator.py @@ -78,8 +78,7 @@ def translate(self, text, ignore_cache=False): return cache translation = self.do_translate(text) - if not (self.ignore_cache or ignore_cache): - self.cache.set(text, translation) + self.cache.set(text, translation) return translation def do_translate(self, text): diff --git a/test/test_translator.py b/test/test_translator.py index 44526ddd..ea9706c7 100644 --- a/test/test_translator.py +++ b/test/test_translator.py @@ -50,17 +50,17 @@ def test_add_cache_impact_parameters(self): self.assertNotEqual(first_result, second_result) # Test cache with ignore_cache=True - no_cache_result = translator.translate(text, ignore_cache=True) - self.assertNotEqual(first_result, no_cache_result) + no_cache_result1 = translator.translate(text, ignore_cache=True) + self.assertNotEqual(first_result, no_cache_result1) translator.ignore_cache = True - no_cache_result = translator.translate(text) - self.assertNotEqual(first_result, no_cache_result) + no_cache_result2 = translator.translate(text) + self.assertNotEqual(no_cache_result1, no_cache_result2) # Test cache with ignore_cache=False translator.ignore_cache = False cache_result = translator.translate(text) - self.assertEqual(second_result, cache_result) + self.assertEqual(no_cache_result2, cache_result) # Test cache with another parameter translator.add_cache_impact_parameters("test2", "value2")