Skip to content
Extraits de code Groupes Projets
Valider 70f779ac rédigé par Cyril Bonté's avatar Cyril Bonté
Parcourir les fichiers

disable cache when OrderedDict is not available

Fallback to a non cached dates when OrderedDict is not available.
It can occur with python < 2.7 and Pypi OrderedDict is not installed.
parent 09c0246b
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -50,7 +50,10 @@ except ImportError: ...@@ -50,7 +50,10 @@ except ImportError:
try: try:
from collections import OrderedDict from collections import OrderedDict
except ImportError: except ImportError:
from ordereddict import OrderedDict try:
from ordereddict import OrderedDict
except ImportError:
print >> sys.stderr, 'ordereddict (https://pypi.python.org/pypi/ordereddict) is required to enable dates caching.'
## ##
...@@ -1595,7 +1598,10 @@ class Parser(object): ...@@ -1595,7 +1598,10 @@ class Parser(object):
resolver.check_format(format) resolver.check_format(format)
hits = [] hits = []
cache_dates = OrderedDict() try:
cache_dates = OrderedDict()
except NameError:
cache_dates = None
for lineno, line in enumerate(file): for lineno, line in enumerate(file):
try: try:
line = line.decode(config.options.encoding) line = line.decode(config.options.encoding)
...@@ -1675,13 +1681,14 @@ class Parser(object): ...@@ -1675,13 +1681,14 @@ class Parser(object):
# Parse date. # Parse date.
# We parse it after calling check_methods as it's quite CPU hungry, and # We parse it after calling check_methods as it's quite CPU hungry, and
# we want to avoid that cost for excluded hits. # we want to avoid that cost for excluded hits.
# To mitigate CPU usage, parsed dates are cached. if cache_dates is not None:
try: # To mitigate CPU usage, parsed dates are cached.
timezone_key = format.get('timezone') try:
except BaseFormatException: timezone_key = format.get('timezone')
timezone_key = '' except BaseFormatException:
date_key = format.get('date') + '|' + timezone_key timezone_key = ''
hit.date = cache_dates.get(date_key) date_key = format.get('date') + '|' + timezone_key
hit.date = cache_dates.get(date_key)
if not hit.date: if not hit.date:
date_string = format.get('date') date_string = format.get('date')
try: try:
...@@ -1702,9 +1709,10 @@ class Parser(object): ...@@ -1702,9 +1709,10 @@ class Parser(object):
if timezone: if timezone:
hit.date -= datetime.timedelta(hours=timezone/100) hit.date -= datetime.timedelta(hours=timezone/100)
if len(cache_dates) > 3600: if cache_dates is not None:
cache_dates.popitem(False) if len(cache_dates) > 3600:
cache_dates[date_key] = hit.date cache_dates.popitem(False)
cache_dates[date_key] = hit.date
if config.options.replay_tracking: if config.options.replay_tracking:
# we need a query string and we only consider requests with piwik.php # we need a query string and we only consider requests with piwik.php
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter