Fixed some bugs
This commit is contained in:
Vendored
+17
-2
@@ -1,5 +1,7 @@
|
||||
from threading import RLock
|
||||
|
||||
MAX_INITIALIZED_KEY = 100
|
||||
|
||||
|
||||
class BaseCache:
|
||||
"""
|
||||
@@ -15,11 +17,15 @@ class BaseCache:
|
||||
self._extend_exists = extend_exists # search in remote
|
||||
self._lock = RLock()
|
||||
self._current_size = 0
|
||||
self._initialized_keys = set()
|
||||
self._initialized_keys = set() # to keep the list of the keys already requested (using get())
|
||||
|
||||
self.to_add = set()
|
||||
self.to_remove = set()
|
||||
|
||||
# Explanation on _initialized_keys
|
||||
# everytime you try to get an item, its key is added to _initialized_keys
|
||||
# If the item is found, the entru is i
|
||||
|
||||
def __len__(self):
|
||||
"""
|
||||
Return the number of items in the cache
|
||||
@@ -78,7 +84,6 @@ class BaseCache:
|
||||
:return:
|
||||
"""
|
||||
with self._lock:
|
||||
self._initialized_keys.add(key)
|
||||
return self._get(key)
|
||||
|
||||
def inner_get(self, key):
|
||||
@@ -165,6 +170,9 @@ class BaseCache:
|
||||
self._initialized_keys.remove(key)
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
self._current_size -= len(to_delete)
|
||||
|
||||
return len(to_delete)
|
||||
|
||||
def clear(self):
|
||||
@@ -201,6 +209,7 @@ class BaseCache:
|
||||
for key in keys:
|
||||
if key not in self._initialized_keys and self._default:
|
||||
# to keep sync with the remote repo is needed
|
||||
# first check self._initialized_keys to prevent infinite loop
|
||||
self.get(key)
|
||||
|
||||
def _add_to_add(self, key):
|
||||
@@ -221,7 +230,12 @@ class BaseCache:
|
||||
try:
|
||||
value = self._cache[key]
|
||||
except KeyError:
|
||||
if len(self._initialized_keys) == MAX_INITIALIZED_KEY:
|
||||
self._initialized_keys.clear()
|
||||
if callable(self._default):
|
||||
if key in self._initialized_keys:
|
||||
return None
|
||||
|
||||
value = self._default(key)
|
||||
if value is not None:
|
||||
self._cache[key] = value
|
||||
@@ -233,6 +247,7 @@ class BaseCache:
|
||||
self._current_size += 1
|
||||
else:
|
||||
value = self._default
|
||||
self._initialized_keys.add(key)
|
||||
|
||||
return value
|
||||
|
||||
|
||||
Reference in New Issue
Block a user