Commit a0009086 authored by Michal Čihař's avatar Michal Čihař

Remove caching from the quality checks

The cost of cache query is higher than calculating the value every time.
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 3de4d1ce
......@@ -18,7 +18,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from django.core.cache import cache
import weblate
......@@ -122,29 +121,6 @@ class Check(object):
'''
return weblate.get_doc_url('user/checks', self.doc_id)
def get_cache_key(self, unit, cache_slot=0):
'''
Generates key for a cache.
'''
return 'check-%s-%d-%s-%d' % (
self.check_id,
unit.translation.subproject.project.id,
unit.checksum,
cache_slot
)
def get_cache(self, unit, cache_slot=0):
'''
Returns cached result.
'''
return cache.get(self.get_cache_key(unit, cache_slot))
def set_cache(self, unit, value, cache_slot=0):
'''
Sets cache.
'''
return cache.set(self.get_cache_key(unit, cache_slot), value)
class TargetCheck(Check):
'''
......
......@@ -167,30 +167,19 @@ class BaseFormatCheck(TargetCheck):
uses_position = True
# Try geting source parsing from cache
src_matches = self.get_cache(unit, cache_slot)
# New style cache
if isinstance(src_matches, tuple):
uses_position, src_matches = src_matches
else:
src_matches = None
# We ignore %% in the matches as this is really not relevant. However
# it needs to be matched to prevent handling %%s as %s.
# Cache miss, so calculate value
if src_matches is None:
src_matches = [
self.cleanup_string(x[0])
for x in self.regexp.findall(source)
if x[0] != '%'
]
if src_matches:
uses_position = max(
[self.is_position_based(x) for x in src_matches]
)
self.set_cache(unit, (uses_position, src_matches), cache_slot)
# Calculate value
src_matches = [
self.cleanup_string(x[0])
for x in self.regexp.findall(source)
if x[0] != '%'
]
if src_matches:
uses_position = max(
[self.is_position_based(x) for x in src_matches]
)
tgt_matches = [
self.cleanup_string(x[0])
......
......@@ -49,12 +49,8 @@ class BBCodeCheck(TargetCheck):
severity = 'warning'
def check_single(self, source, target, unit, cache_slot):
# Try geting source parsing from cache
src_match = self.get_cache(unit, cache_slot)
# Cache miss
if src_match is None:
src_match = BBCODE_MATCH.findall(source)
self.set_cache(unit, src_match, cache_slot)
# Parse source
src_match = BBCODE_MATCH.findall(source)
# Any BBCode in source?
if len(src_match) == 0:
return False
......@@ -86,28 +82,16 @@ class XMLTagsCheck(TargetCheck):
return cElementTree.fromstring('<weblate>%s</weblate>' % text)
def check_single(self, source, target, unit, cache_slot):
# Try getting source string data from cache
source_tags = self.get_cache(unit, cache_slot)
# Source is not XML
if source_tags == []:
# Quick check if source looks like XML
if '<' not in source or len(XML_MATCH.findall(source)) == 0:
return False
# Check if source is XML
try:
source_tree = self.parse_xml(source)
source_tags = [x.tag for x in source_tree]
except SyntaxError:
# Source is not valid XML, we give up
return False
# Do we need to process source (cache miss)
if source_tags is None:
# Quick check if source looks like XML
if '<' not in source or len(XML_MATCH.findall(source)) == 0:
self.set_cache(unit, [], cache_slot)
return False
# Check if source is XML
try:
source_tree = self.parse_xml(source)
source_tags = [x.tag for x in source_tree]
self.set_cache(unit, source_tags, cache_slot)
except SyntaxError:
# Source is not valid XML, we give up
self.set_cache(unit, [], cache_slot)
return False
# Check target
try:
......
......@@ -1106,15 +1106,9 @@ class SameCheck(TargetCheck):
'''
Check whether given unit should be ignored.
'''
# Use cache if available
result = self.get_cache(unit, cache_slot)
if result is not None:
return result
# Ignore some docbook tags
if unit.comment.startswith('Tag: '):
if unit.comment[5:] in DB_TAGS:
self.set_cache(unit, True, cache_slot)
return True
# Lower case source
......@@ -1141,9 +1135,6 @@ class SameCheck(TargetCheck):
return False
return True
# Store in cache
self.set_cache(unit, result, cache_slot)
return result
def check_single(self, source, target, unit, cache_slot):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment