From: Mike Bayer Date: Sat, 28 Jan 2012 02:05:17 +0000 (-0500) Subject: - [bug] Changed LRUCache, used by the mapper X-Git-Tag: rel_0_7_5~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=07130c7a8b5c58e744c27c7b1baabeda7034f05b;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - [bug] Changed LRUCache, used by the mapper to cache INSERT/UPDATE/DELETE statements, to use an incrementing counter instead of a timestamp to track entries, for greater reliability versus using time.time(), which can cause test failures on some platforms. [ticket:2379] --- diff --git a/CHANGES b/CHANGES index bab93885c3..35c61afd40 100644 --- a/CHANGES +++ b/CHANGES @@ -119,6 +119,15 @@ CHANGES list of oracle "connection lost" errors [ticket:2388] +- core + - [bug] Changed LRUCache, used by the mapper + to cache INSERT/UPDATE/DELETE statements, + to use an incrementing counter instead + of a timestamp to track entries, for greater + reliability versus using time.time(), which + can cause test failures on some platforms. + [ticket:2379] + - Py3K - [bug] Fixed inappropriate usage of util.py3k flag and renamed it to util.py3k_warning, since diff --git a/lib/sqlalchemy/util/_collections.py b/lib/sqlalchemy/util/_collections.py index 1b8decf59f..1c407324ce 100644 --- a/lib/sqlalchemy/util/_collections.py +++ b/lib/sqlalchemy/util/_collections.py @@ -769,10 +769,15 @@ class LRUCache(dict): def __init__(self, capacity=100, threshold=.5): self.capacity = capacity self.threshold = threshold + self._counter = 0 + + def _inc_counter(self): + self._counter += 1 + return self._counter def __getitem__(self, key): item = dict.__getitem__(self, key) - item[2] = time_func() + item[2] = self._inc_counter() return item[1] def values(self): @@ -788,7 +793,7 @@ class LRUCache(dict): def __setitem__(self, key, value): item = dict.get(self, key) if item is None: - item = [key, value, time_func()] + item = [key, value, self._inc_counter()] dict.__setitem__(self, key, item) else: item[1] = value @@ -796,10 +801,10 @@ class LRUCache(dict): def _manage_size(self): while len(self) > self.capacity + self.capacity * self.threshold: - bytime = sorted(dict.values(self), + by_counter = sorted(dict.values(self), key=operator.itemgetter(2), reverse=True) - for item in bytime[self.capacity:]: + for item in by_counter[self.capacity:]: try: del self[item[0]] except KeyError: