Created
February 4, 2013 21:11
-
-
Save mynameisfiber/4709753 to your computer and use it in GitHub Desktop.
Simple dictionary caching object that limits the number of cached entries beingstored.
This file contains 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
""" | |
Simple dictionary caching object that limits the number of cached entries being | |
stored. | |
Micha Gorelick - http://micha.gd/ | |
""" | |
from collections import OrderedDict | |
class LimitedCache(OrderedDict): | |
def __init__(self, maxsize=250): | |
self.maxsize = maxsize | |
super(LimitedCache, self).__init__() | |
def __setitem__(self, key, value): | |
while len(self) > self.maxsize: | |
self.popitem(last=False) | |
super(LimitedCache, self).__setitem__(key, value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment