OLD | NEW |
(Empty) | |
| 1 from __future__ import absolute_import |
| 2 |
| 3 import random |
| 4 |
| 5 from .cache import Cache |
| 6 |
| 7 |
| 8 class RRCache(Cache): |
| 9 """Random Replacement (RR) cache implementation.""" |
| 10 |
| 11 def __init__(self, maxsize, choice=random.choice, missing=None, |
| 12 getsizeof=None): |
| 13 Cache.__init__(self, maxsize, missing, getsizeof) |
| 14 self.__choice = choice |
| 15 |
| 16 @property |
| 17 def choice(self): |
| 18 """The `choice` function used by the cache.""" |
| 19 return self.__choice |
| 20 |
| 21 def popitem(self): |
| 22 """Remove and return a random `(key, value)` pair.""" |
| 23 try: |
| 24 key = self.__choice(list(self)) |
| 25 except IndexError: |
| 26 raise KeyError('%s is empty' % self.__class__.__name__) |
| 27 else: |
| 28 return (key, self.pop(key)) |
OLD | NEW |