Commit 121b3882 authored by Julien Muchembled's avatar Julien Muchembled

Merge "client: fix accounting of cache size"

parents f2070ca4 5b02f44b
...@@ -135,13 +135,16 @@ class ClientCache(object): ...@@ -135,13 +135,16 @@ class ClientCache(object):
if level: if level:
item.expire = self._time + self._life_time item.expire = self._time + self._life_time
else: else:
self._size -= len(item.data) self._empty(item)
item.data = None
self._history_size += 1 self._history_size += 1
if self._max_history_size < self._history_size: if self._max_history_size < self._history_size:
self._remove(head) self._remove(head)
self._remove_from_oid_dict(head) self._remove_from_oid_dict(head)
def _empty(self, item):
self._size -= len(item.data)
item.data = None
def _remove(self, item): def _remove(self, item):
level = item.level level = item.level
if level is not None: if level is not None:
...@@ -176,6 +179,7 @@ class ClientCache(object): ...@@ -176,6 +179,7 @@ class ClientCache(object):
if head.level or head.counter: if head.level or head.counter:
self._add(head) self._add(head)
else: else:
self._empty(head)
self._remove_from_oid_dict(head) self._remove_from_oid_dict(head)
break break
...@@ -260,6 +264,7 @@ class ClientCache(object): ...@@ -260,6 +264,7 @@ class ClientCache(object):
head.level = 0 head.level = 0
self._add(head) self._add(head)
else: else:
self._empty(head)
self._remove_from_oid_dict(head) self._remove_from_oid_dict(head)
if self._size <= max_size: if self._size <= max_size:
return return
...@@ -281,6 +286,8 @@ class ClientCache(object): ...@@ -281,6 +286,8 @@ class ClientCache(object):
for oid, item_list in self._oid_dict.items(): for oid, item_list in self._oid_dict.items():
item = item_list[-1] item = item_list[-1]
if item.next_tid is None: if item.next_tid is None:
if item.level:
self._empty(item)
self._remove(item) self._remove(item)
del item_list[-1] del item_list[-1]
# We don't preserve statistics of removed items. This could be # We don't preserve statistics of removed items. This could be
...@@ -307,10 +314,13 @@ def test(self): ...@@ -307,10 +314,13 @@ def test(self):
cache.store(1, *data) cache.store(1, *data)
self.assertEqual(cache.load(1, None), data) self.assertEqual(cache.load(1, None), data)
cache.clear_current() cache.clear_current()
self.assertEqual(cache._size, 1)
self.assertEqual(cache.load(1, None), None) self.assertEqual(cache.load(1, None), None)
cache.store(1, *data) cache.store(1, *data)
cache.invalidate(1, 20) cache.invalidate(1, 20)
self.assertEqual(cache._size, 3)
cache.clear_current() cache.clear_current()
self.assertEqual(cache._size, 3)
self.assertEqual(cache.load(1, 20), ('15', 15, 20)) self.assertEqual(cache.load(1, 20), ('15', 15, 20))
cache.store(1, '10', 10, 15) cache.store(1, '10', 10, 15)
cache.store(1, '20', 20, 21) cache.store(1, '20', 20, 21)
...@@ -322,8 +332,10 @@ def test(self): ...@@ -322,8 +332,10 @@ def test(self):
cache.clear() cache.clear()
cache.store(1, '10*', 10, None) cache.store(1, '10*', 10, None)
cache._max_size = cache._size cache._max_size = cache._size
cache.store(2, '10', 10, None) cache.store(2, '10', 10, 15)
self.assertEqual(cache._queue_list[0].oid, 1) self.assertEqual(cache._queue_list[0].oid, 1)
cache.store(2, '15', 15, None)
self.assertEqual(cache._queue_list[2].oid, 2)
data = '10', 10, 15 data = '10', 10, 15
cache.store(1, *data) cache.store(1, *data)
self.assertEqual(cache.load(1, 15), data) self.assertEqual(cache.load(1, 15), data)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment