Skip to content

Instantly share code, notes, and snippets.

@lihuanshuai
Created September 4, 2019 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lihuanshuai/e0abecf2f2c826bf993bad2589b4bfca to your computer and use it in GitHub Desktop.
Save lihuanshuai/e0abecf2f2c826bf993bad2589b4bfca to your computer and use it in GitHub Desktop.
from collections import OrderedDict
class LRUCache(object):
def __init__(self, size=5):
self.size = size
self.cache = OrderedDict()
def get(self, key):
if key in self.cache:
val = self.cache.pop(key)
self.cache[key] = val
else:
val = None
return val
def set(self, key, val):
if key in self.cache:
val = self.cache.pop(key)
self.cache[key] = val
else:
if len(self.cache) == self.size:
self.cache.popitem(last=False)
self.cache[key] = val
else:
self.cache[key] = val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment