Skip to content

Instantly share code, notes, and snippets.

@wrunk
Created June 20, 2012 06:04
Show Gist options
  • Save wrunk/2958375 to your computer and use it in GitHub Desktop.
Save wrunk/2958375 to your computer and use it in GitHub Desktop.
Python dumbed down lru cache class
import logging
'''
THIS IS A WORK IN PROGRESS. Do not use unless you fully understand this
code, and can make needed modifications.
'''
class LRUCache(object):
''' Dumb/fake least recently used cache to just run in memory.
'''
def __init__(self, total_size=500):
'''Pass the size in megabytes. We want provide a low ball estimation.
'''
self._objs = {}
self.lru_queue = []
self.bytes_free = total_size * 1000000.0
self.total_size = total_size * 1000000.0
def __contains__(self, item):
'So you can do "my_key in lru"'
return item in self._objs
def info(self):
return {'used_megs': "%.4f" %( (self.total_size - self.bytes_free) / 1000000.0 ),
'remaining_megs': "%.4f"%( (self.bytes_free / 1000000.0) )}
def add(self, name, obj, bytes_estimation):
assert bytes_estimation <= self.total_size,\
"The LRU cache cannot exceed %i" % self.total_size
if name in self._objs:
logging.warn("[LRUCache] (%s) was already in cache", name)
return
while self.bytes_free < bytes_estimation:
logging.warn("[LRUCache] Only have (%i) bytes free, need (%i), popping an item", self.bytes_free, bytes_estimation)
self._del_first_item()
logging.warn("[LRUCache] Adding object of name (%s) with estimated bytes (%i)", name, bytes_estimation)
self._objs[name] = {'name': name, 'bytes_estimation': bytes_estimation, 'obj': obj}
self.lru_queue.append(name)
self.bytes_free -= bytes_estimation
logging.warn("[LRUCache] Is now holding (%i) bytes", self.total_size - self.bytes_free)
def get(self, name):
if name in self._objs:
return self._objs[name]['obj']
def _del_first_item(self):
# Pop first item
i = self.lru_queue.pop(0)
logging.warn("[LRUCache] Removing (%s) from the cache", i)
self.bytes_free += self._objs[i]['bytes_estimation']
del self._objs[i]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment