Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callback function register on construct #48

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cachetools/base.py
Original file line number Diff line number Diff line change
@@ -4,10 +4,11 @@
class Cache(collections.MutableMapping):
"""Mutable mapping to serve as a simple cache or cache base class."""

def __init__(self, maxsize, missing=None, getsizeof=None):
def __init__(self, maxsize, missing=None, getsizeof=None, callback=None):
self.__data = dict()
self.__currsize = 0
self.__maxsize = maxsize
self.callback = callback
if missing:
self.__missing = missing
if getsizeof:
@@ -35,7 +36,9 @@ def __setitem__(self, key, value):
raise ValueError('value too large')
if key not in data or data[key][1] < size:
while self.__currsize + size > maxsize:
self.popitem()
pkey, pvalue = self.popitem()
if self.callback is not None:
self.callback(pkey, pvalue)
if key in data:
diffsize = size - data[key][1]
else:
4 changes: 2 additions & 2 deletions cachetools/lfu.py
Original file line number Diff line number Diff line change
@@ -7,8 +7,8 @@
class LFUCache(Cache):
"""Least Frequently Used (LFU) cache implementation."""

def __init__(self, maxsize, missing=None, getsizeof=None):
Cache.__init__(self, maxsize, missing, getsizeof)
def __init__(self, maxsize, missing=None, getsizeof=None, callback=None):
Cache.__init__(self, maxsize, missing, getsizeof, callback)
self.__counter = collections.Counter()

def __getitem__(self, key, cache_getitem=Cache.__getitem__):
4 changes: 2 additions & 2 deletions cachetools/lru.py
Original file line number Diff line number Diff line change
@@ -15,8 +15,8 @@ def unlink(self):
class LRUCache(Cache):
"""Least Recently Used (LRU) cache implementation."""

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

4 changes: 2 additions & 2 deletions cachetools/rr.py
Original file line number Diff line number Diff line change
@@ -7,8 +7,8 @@ class RRCache(Cache):
"""Random Replacement (RR) cache implementation."""

def __init__(self, maxsize, choice=random.choice, missing=None,
getsizeof=None):
Cache.__init__(self, maxsize, missing, getsizeof)
getsizeof=None, callback=None):
Cache.__init__(self, maxsize, missing, getsizeof, callback)
self.__choice = choice

def popitem(self):
4 changes: 2 additions & 2 deletions cachetools/ttl.py
Original file line number Diff line number Diff line change
@@ -59,8 +59,8 @@ 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):
Cache.__init__(self, maxsize, missing, getsizeof)
getsizeof=None, callback=None):
Cache.__init__(self, maxsize, missing, getsizeof, callback)
root = self.__root = _Link()
root.ttl_prev = root.ttl_next = root
root.lru_prev = root.lru_next = root