Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkem committed Jun 18, 2015
1 parent 35ae4b8 commit 6e57364
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
8 changes: 4 additions & 4 deletions cachetools/lru.py
@@ -1,7 +1,7 @@
from .cache import Cache


class Link(object):
class _Link(object):

__slots__ = 'key', 'value', 'prev', 'next'

Expand All @@ -17,7 +17,7 @@ class LRUCache(Cache):

def __init__(self, maxsize, missing=None, getsizeof=None):
Cache.__init__(self, maxsize, missing, getsizeof)
root = self.__root = Link()
root = self.__root = _Link()
root.prev = root.next = root

def __repr__(self, cache_getitem=Cache.__getitem__):
Expand Down Expand Up @@ -48,7 +48,7 @@ def __setitem__(self, key, value,
oldlink = cache_getitem(self, key)
else:
oldlink = None
link = Link()
link = _Link()
link.key = key
link.value = value
cache_setitem(self, key, link)
Expand All @@ -70,7 +70,7 @@ def __delitem__(self, key,

def getsizeof(self, value):
"""Return the size of a cache element's value."""
if isinstance(value, Link):
if isinstance(value, _Link):
return Cache.getsizeof(self, value.value)
else:
return Cache.getsizeof(self, value)
Expand Down
20 changes: 11 additions & 9 deletions cachetools/ttl.py
Expand Up @@ -4,7 +4,7 @@
from .cache import Cache


class Link(object):
class _Link(object):

__slots__ = (
'key', 'value', 'expire', 'size',
Expand All @@ -24,7 +24,7 @@ def unlink(self):
lru_next.lru_prev = lru_prev


class NestedTimer(object):
class _NestedTimer(object):

def __init__(self, timer):
self.__timer = timer
Expand Down Expand Up @@ -58,12 +58,13 @@ 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):
def __init__(self, maxsize, ttl, timer=time.time, missing=None,
getsizeof=None):
Cache.__init__(self, maxsize, missing, getsizeof)
root = self.__root = Link()
root = self.__root = _Link()
root.ttl_prev = root.ttl_next = root
root.lru_prev = root.lru_next = root
self.__timer = NestedTimer(timer)
self.__timer = _NestedTimer(timer)
self.__ttl = ttl

def __repr__(self, cache_getitem=Cache.__getitem__):
Expand Down Expand Up @@ -94,18 +95,19 @@ def __getitem__(self, key,
def __setitem__(self, key, value,
cache_contains=Cache.__contains__,
cache_getitem=Cache.__getitem__,
cache_setitem=Cache.__setitem__):
cache_setitem=Cache.__setitem__,
cache_getsizeof=Cache.getsizeof):
with self.__timer as time:
self.expire(time)
if cache_contains(self, key):
oldlink = cache_getitem(self, key)
else:
oldlink = None
link = Link()
link = _Link()
link.key = key
link.value = value
link.expire = time + self.__ttl
link.size = self.getsizeof(value)
link.size = cache_getsizeof(self, value)
cache_setitem(self, key, link)
if oldlink:
oldlink.unlink()
Expand Down Expand Up @@ -195,7 +197,7 @@ def expire(self, time=None):

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

0 comments on commit 6e57364

Please sign in to comment.