Created
April 11, 2013 20:50
-
-
Save arlolra/5367063 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# requires python >= 2.7 | |
# see: https://pypi.python.org/pypi/ordereddict | |
from collections import OrderedDict | |
MAXLRU = 200 | |
class LRU(OrderedDict): | |
def __init__(self, *args, **kwds): | |
self.size = kwds.pop("size", MAXLRU) | |
OrderedDict.__init__(self, *args, **kwds) | |
self.limit() | |
def __setitem__(self, key, value): | |
if key in self: | |
del self[key] | |
OrderedDict.__setitem__(self, key, value) | |
self.limit() | |
def limit(self): | |
while len(self) > self.size: | |
self.popitem(last=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment