Skip to content

Commit

Permalink
Refactor getsizeof usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkem committed Jun 18, 2015
1 parent fee1023 commit 35ae4b8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
2 changes: 1 addition & 1 deletion cachetools/cache.py
Expand Up @@ -30,7 +30,7 @@ def __getitem__(self, key):
def __setitem__(self, key, value):
data = self.__data
maxsize = self.__maxsize
size = self.__getsizeof(value)
size = self.getsizeof(value)
if size > maxsize:
raise ValueError('value too large')
if key not in data or data[key][1] < size:
Expand Down
16 changes: 9 additions & 7 deletions cachetools/lru.py
Expand Up @@ -16,13 +16,8 @@ class LRUCache(Cache):
"""Least Recently Used (LRU) cache implementation."""

def __init__(self, maxsize, missing=None, getsizeof=None):
if getsizeof is not None:
getlinksize = lambda link: getsizeof(link.value)
Cache.__init__(self, maxsize, missing, getlinksize)
self.getsizeof = getsizeof
else:
Cache.__init__(self, maxsize, missing)
self.__root = root = Link()
Cache.__init__(self, maxsize, missing, getsizeof)
root = self.__root = Link()
root.prev = root.next = root

def __repr__(self, cache_getitem=Cache.__getitem__):
Expand Down Expand Up @@ -73,6 +68,13 @@ def __delitem__(self, key,
cache_delitem(self, key)
link.unlink()

def getsizeof(self, value):
"""Return the size of a cache element's value."""
if isinstance(value, Link):
return Cache.getsizeof(self, value.value)
else:
return Cache.getsizeof(self, value)

def popitem(self):
"""Remove and return the `(key, value)` pair least recently used."""
root = self.__root
Expand Down
65 changes: 32 additions & 33 deletions cachetools/ttl.py
Expand Up @@ -12,9 +12,6 @@ class Link(object):
'lru_prev', 'lru_next'
)

def getsize(self):
return self.size

def unlink(self):
ttl_next = self.ttl_next
ttl_prev = self.ttl_prev
Expand Down Expand Up @@ -61,17 +58,12 @@ def __setstate__(self, state):
class TTLCache(Cache):
"""LRU Cache implementation with per-item time-to-live (TTL) value."""

def __init__(self, maxsize, ttl, timer=time.time, missing=None,
getsizeof=None):
if getsizeof is not None:
Cache.__init__(self, maxsize, missing, Link.getsize)
self.getsizeof = getsizeof
else:
Cache.__init__(self, maxsize, missing)
self.__timer = NestedTimer(timer)
self.__root = root = Link()
def __init__(self, maxsize, ttl, timer=time.time, missing=None, getsizeof=None):
Cache.__init__(self, maxsize, missing, getsizeof)
root = self.__root = Link()
root.ttl_prev = root.ttl_next = root
root.lru_prev = root.lru_next = root
self.__timer = NestedTimer(timer)
self.__ttl = ttl

def __repr__(self, cache_getitem=Cache.__getitem__):
Expand Down Expand Up @@ -167,6 +159,27 @@ def __len__(self, cache_len=Cache.__len__):
head = head.ttl_next
return cache_len(self) - expired

@property
def currsize(self):
root = self.__root
head = root.ttl_next
expired = 0
with self.__timer as time:
while head is not root and head.expire < time:
expired += head.size
head = head.ttl_next
return super(TTLCache, self).currsize - expired

@property
def timer(self):
"""The timer function used by the cache."""
return self.__timer

@property
def ttl(self):
"""The time-to-live value of the cache's items."""
return self.__ttl

def expire(self, time=None):
"""Remove expired items from the cache."""
if time is None:
Expand All @@ -180,6 +193,13 @@ def expire(self, time=None):
head.unlink()
head = next

def getsizeof(self, value):
"""Return the size of a cache element's value."""
if isinstance(value, Link):
return value.size
else:
return Cache.getsizeof(self, value)

def popitem(self):
"""Remove and return the `(key, value)` pair least recently used that
has not already expired.
Expand All @@ -196,27 +216,6 @@ def popitem(self):
link.unlink()
return (key, link.value)

@property
def currsize(self):
root = self.__root
head = root.ttl_next
expired = 0
with self.__timer as time:
while head is not root and head.expire < time:
expired += head.size
head = head.ttl_next
return super(TTLCache, self).currsize - expired

@property
def timer(self):
"""The timer function used by the cache."""
return self.__timer

@property
def ttl(self):
"""The time-to-live value of the cache's items."""
return self.__ttl

# mixin methods

def __nested(method):
Expand Down

0 comments on commit 35ae4b8

Please sign in to comment.