cache.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
  2. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
  3. #
  4. # This file is part of logilab-common.
  5. #
  6. # logilab-common is free software: you can redistribute it and/or modify it under
  7. # the terms of the GNU Lesser General Public License as published by the Free
  8. # Software Foundation, either version 2.1 of the License, or (at your option) any
  9. # later version.
  10. #
  11. # logilab-common is distributed in the hope that it will be useful, but WITHOUT
  12. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  14. # details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public License along
  17. # with logilab-common. If not, see <http://www.gnu.org/licenses/>.
  18. """Cache module, with a least recently used algorithm for the management of the
  19. deletion of entries.
  20. """
  21. __docformat__ = "restructuredtext en"
  22. from threading import Lock
  23. from logilab.common.decorators import locked
  24. _marker = object()
  25. class Cache(dict):
  26. """A dictionary like cache.
  27. inv:
  28. len(self._usage) <= self.size
  29. len(self.data) <= self.size
  30. """
  31. def __init__(self, size=100):
  32. """ Warning : Cache.__init__() != dict.__init__().
  33. Constructor does not take any arguments beside size.
  34. """
  35. assert size >= 0, 'cache size must be >= 0 (0 meaning no caching)'
  36. self.size = size
  37. self._usage = []
  38. self._lock = Lock()
  39. super(Cache, self).__init__()
  40. def _acquire(self):
  41. self._lock.acquire()
  42. def _release(self):
  43. self._lock.release()
  44. def _update_usage(self, key):
  45. if not self._usage:
  46. self._usage.append(key)
  47. elif self._usage[-1] != key:
  48. try:
  49. self._usage.remove(key)
  50. except ValueError:
  51. # we are inserting a new key
  52. # check the size of the dictionary
  53. # and remove the oldest item in the cache
  54. if self.size and len(self._usage) >= self.size:
  55. super(Cache, self).__delitem__(self._usage[0])
  56. del self._usage[0]
  57. self._usage.append(key)
  58. else:
  59. pass # key is already the most recently used key
  60. def __getitem__(self, key):
  61. value = super(Cache, self).__getitem__(key)
  62. self._update_usage(key)
  63. return value
  64. __getitem__ = locked(_acquire, _release)(__getitem__)
  65. def __setitem__(self, key, item):
  66. # Just make sure that size > 0 before inserting a new item in the cache
  67. if self.size > 0:
  68. super(Cache, self).__setitem__(key, item)
  69. self._update_usage(key)
  70. __setitem__ = locked(_acquire, _release)(__setitem__)
  71. def __delitem__(self, key):
  72. super(Cache, self).__delitem__(key)
  73. self._usage.remove(key)
  74. __delitem__ = locked(_acquire, _release)(__delitem__)
  75. def clear(self):
  76. super(Cache, self).clear()
  77. self._usage = []
  78. clear = locked(_acquire, _release)(clear)
  79. def pop(self, key, default=_marker):
  80. if key in self:
  81. self._usage.remove(key)
  82. #if default is _marker:
  83. # return super(Cache, self).pop(key)
  84. return super(Cache, self).pop(key, default)
  85. pop = locked(_acquire, _release)(pop)
  86. def popitem(self):
  87. raise NotImplementedError()
  88. def setdefault(self, key, default=None):
  89. raise NotImplementedError()
  90. def update(self, other):
  91. raise NotImplementedError()