cachetools 2.1.0

This commit is contained in:
Jay Lee
2018-07-04 19:51:43 -04:00
parent 26ebf30da7
commit 518e506df4
7 changed files with 39 additions and 25 deletions

View File

@@ -16,7 +16,7 @@ __all__ = (
'cached', 'cachedmethod'
)
__version__ = '2.0.1'
__version__ = '2.1.0'
if hasattr(functools.update_wrapper(lambda f: f(), lambda: 42), '__wrapped__'):
_update_wrapper = functools.update_wrapper

View File

@@ -1,7 +1,6 @@
from __future__ import absolute_import
import collections
from abc import abstractmethod

View File

@@ -1,5 +1,7 @@
from __future__ import absolute_import
from warnings import warn
from .abc import DefaultMapping
@@ -14,16 +16,23 @@ class _DefaultSize(object):
return 1
_deprecated = object()
class Cache(DefaultMapping):
"""Mutable mapping to serve as a simple cache or cache base class."""
__size = _DefaultSize()
def __init__(self, maxsize, missing=None, getsizeof=None):
if missing:
self.__missing = missing
def __init__(self, maxsize, missing=_deprecated, getsizeof=None):
if missing is not _deprecated:
warn("Cache constructor parameter 'missing' is deprecated",
DeprecationWarning, 3)
if missing:
self.__missing = missing
if getsizeof:
self.__getsizeof = getsizeof
self.getsizeof = getsizeof
if self.getsizeof is not Cache.getsizeof:
self.__size = dict()
self.__data = dict()
self.__currsize = 0
@@ -81,14 +90,6 @@ class Cache(DefaultMapping):
def __len__(self):
return len(self.__data)
@staticmethod
def __getsizeof(value):
return 1
@staticmethod
def __missing(key):
raise KeyError(key)
@property
def maxsize(self):
"""The maximum size of the cache."""
@@ -99,6 +100,11 @@ class Cache(DefaultMapping):
"""The current size of the cache."""
return self.__currsize
def getsizeof(self, value):
@staticmethod
def getsizeof(value):
"""Return the size of a cache element's value."""
return self.__getsizeof(value)
return 1
@staticmethod
def __missing(key):
raise KeyError(key)

View File

@@ -2,13 +2,13 @@ from __future__ import absolute_import
import collections
from .cache import Cache
from .cache import Cache, _deprecated
class LFUCache(Cache):
"""Least Frequently Used (LFU) cache implementation."""
def __init__(self, maxsize, missing=None, getsizeof=None):
def __init__(self, maxsize, missing=_deprecated, getsizeof=None):
Cache.__init__(self, maxsize, missing, getsizeof)
self.__counter = collections.Counter()

View File

@@ -2,13 +2,13 @@ from __future__ import absolute_import
import collections
from .cache import Cache
from .cache import Cache, _deprecated
class LRUCache(Cache):
"""Least Recently Used (LRU) cache implementation."""
def __init__(self, maxsize, missing=None, getsizeof=None):
def __init__(self, maxsize, missing=_deprecated, getsizeof=None):
Cache.__init__(self, maxsize, missing, getsizeof)
self.__order = collections.OrderedDict()

View File

@@ -2,16 +2,25 @@ from __future__ import absolute_import
import random
from .cache import Cache
from .cache import Cache, _deprecated
# random.choice cannot be pickled in Python 2.7
def _choice(seq):
return random.choice(seq)
class RRCache(Cache):
"""Random Replacement (RR) cache implementation."""
def __init__(self, maxsize, choice=random.choice, missing=None,
def __init__(self, maxsize, choice=random.choice, missing=_deprecated,
getsizeof=None):
Cache.__init__(self, maxsize, missing, getsizeof)
self.__choice = choice
# TODO: use None as default, assing to self.choice directly?
if choice is random.choice:
self.__choice = _choice
else:
self.__choice = choice
@property
def choice(self):

View File

@@ -3,7 +3,7 @@ from __future__ import absolute_import
import collections
import time
from .cache import Cache
from .cache import Cache, _deprecated
class _Link(object):
@@ -57,7 +57,7 @@ class _Timer(object):
class TTLCache(Cache):
"""LRU Cache implementation with per-item time-to-live (TTL) value."""
def __init__(self, maxsize, ttl, timer=time.time, missing=None,
def __init__(self, maxsize, ttl, timer=time.time, missing=_deprecated,
getsizeof=None):
Cache.__init__(self, maxsize, missing, getsizeof)
self.__root = root = _Link()