diff options
author | Paul Ganssle <paul@ganssle.io> | 2018-09-24 12:22:12 -0400 |
---|---|---|
committer | Paul Ganssle <paul@ganssle.io> | 2018-10-16 14:21:08 -0400 |
commit | 1526a9d2ff4130f004546c8d5199624da9d5376f (patch) | |
tree | 20628528cfa615773412a7bd8eb469d84ff7ae37 /dateutil | |
parent | b01f2f3c4a6bf1b2121eed5e66fca650c9dc9f5f (diff) | |
download | dateutil-1526a9d2ff4130f004546c8d5199624da9d5376f.tar.gz |
Cleanup to strong cache addition
Diffstat (limited to 'dateutil')
-rw-r--r-- | dateutil/test/test_tz.py | 8 | ||||
-rw-r--r-- | dateutil/tz/_factories.py | 13 | ||||
-rw-r--r-- | dateutil/tz/tz.py | 2 |
3 files changed, 16 insertions, 7 deletions
diff --git a/dateutil/test/test_tz.py b/dateutil/test/test_tz.py index 1582af7..97a0c68 100644 --- a/dateutil/test/test_tz.py +++ b/dateutil/test/test_tz.py @@ -746,12 +746,12 @@ def test_tzoffset_weakref(): del UTC1 gc.collect() - assert UTC_ref() is not None # Because strong cache should keep it + assert UTC_ref() is not None # Should be in the strong cache assert UTC_ref() is tz.tzoffset('UTC', 0) # Fill the strong cache with other items for offset in range(5,15): - tz.tzoffset('UTC', offset) + tz.tzoffset('RandomZone', offset) gc.collect() assert UTC_ref() is None @@ -1149,7 +1149,7 @@ def test_gettz_weakref(): del NYC1 gc.collect() - assert NYC_ref() is not None # Because strong cache should keep it + assert NYC_ref() is not None # Should still be in the strong cache assert tz.gettz('America/New_York') is NYC_ref() # Populate strong cache with other timezones @@ -1158,7 +1158,7 @@ def test_gettz_weakref(): tz.gettz('Australia/Currie') gc.collect() - assert NYC_ref() is None # Because strong cache should keep it + assert NYC_ref() is None # Should have been pushed out assert tz.gettz('America/New_York') is not NYC_ref() class ZoneInfoGettzTest(GettzTest, WarningTestMixin): diff --git a/dateutil/tz/_factories.py b/dateutil/tz/_factories.py index 8da5e82..d2560eb 100644 --- a/dateutil/tz/_factories.py +++ b/dateutil/tz/_factories.py @@ -2,6 +2,7 @@ from datetime import timedelta import weakref from collections import OrderedDict + class _TzSingleton(type): def __init__(cls, *args, **kwargs): cls.__instance = None @@ -12,6 +13,7 @@ class _TzSingleton(type): cls.__instance = super(_TzSingleton, cls).__call__() return cls.__instance + class _TzFactory(type): def instance(cls, *args, **kwargs): """Alternate constructor that returns a fresh instance""" @@ -22,6 +24,7 @@ class _TzOffsetFactory(_TzFactory): def __init__(cls, *args, **kwargs): cls.__instances = weakref.WeakValueDictionary() cls.__strong_cache = OrderedDict() + cls.__strong_cache_size = 8 def __call__(cls, name, offset): if isinstance(offset, timedelta): @@ -36,7 +39,9 @@ class _TzOffsetFactory(_TzFactory): cls.__strong_cache[key] = cls.__strong_cache.pop(key, instance) - if len(cls.__strong_cache) == 9: #only to hold 8 items + # Remove an item if the strong cache is overpopulated + # TODO: Maybe this should be under a lock? + if len(cls.__strong_cache) > cls.__strong_cache_size: cls.__strong_cache.popitem(last=False) return instance @@ -46,6 +51,7 @@ class _TzStrFactory(_TzFactory): def __init__(cls, *args, **kwargs): cls.__instances = weakref.WeakValueDictionary() cls.__strong_cache = OrderedDict() + cls.__strong_cache_size = 8 def __call__(cls, s, posix_offset=False): key = (s, posix_offset) @@ -57,7 +63,10 @@ class _TzStrFactory(_TzFactory): cls.__strong_cache[key] = cls.__strong_cache.pop(key, instance) - if len(cls.__strong_cache) == 9: #only to hold 8 items + + # Remove an item if the strong cache is overpopulated + # TODO: Maybe this should be under a lock? + if len(cls.__strong_cache) > cls.__strong_cache_size: cls.__strong_cache.popitem(last=False) return instance diff --git a/dateutil/tz/tz.py b/dateutil/tz/tz.py index 41342b7..161f4d8 100644 --- a/dateutil/tz/tz.py +++ b/dateutil/tz/tz.py @@ -1565,7 +1565,7 @@ def __get_gettz(): self.__strong_cache[name] = self.__strong_cache.pop(name, rv) - if len(self.__strong_cache) == (self.__strong_cache_size + 1): + if len(self.__strong_cache) > self.__strong_cache_size: self.__strong_cache.popitem(last=False) return rv |