Skip to content

Instantly share code, notes, and snippets.

@mynameisfiber
Created February 4, 2013 21:11
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 mynameisfiber/4709753 to your computer and use it in GitHub Desktop.
Save mynameisfiber/4709753 to your computer and use it in GitHub Desktop.
Simple dictionary caching object that limits the number of cached entries beingstored.
"""
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