Skip to content

Instantly share code, notes, and snippets.

@arlolra
Created April 11, 2013 20:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arlolra/5367063 to your computer and use it in GitHub Desktop.
Save arlolra/5367063 to your computer and use it in GitHub Desktop.
#!/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