Untitled

 avatar
unknown
plain_text
7 days ago
649 B
5
Indexable
from collections import OrderedDict

class LRU:
    def __init__(self):
        self.cache = OrderedDict()
        self.cache_size = 10

    def get(self,key):
        if key in self.cache:
            value = self.cache.pop(key)
            self.cache[key] = value
            return value
        
        return -1
    
    def put(self,key,value):
        if len(self.cache) >= self.cache_size:
            self.cache.popitem()
            self.cache[key] = value
        else:
            if key in self.cache:
                self.cache.pop(key)
                self.cache[key] = value
            else:
                self.cache[key] = value
Leave a Comment