class LRUTestCase(unittest.TestCase): def setUp(self): """Call before every test case.""" # self.file = open( "blah", "r" ) def tearDown(self): """Call after every test case.""" # self.file.close() def testA(self): """Test case A. note that all test method names must begin with 'test.'""" self.cache = LRUCache(capacity=2) self.cache.put(key=2, value=2) self.cache.put(key=3, value=3) self.cache.put(key=4, value=3) self.assertEqual(len(self.cache), 2, "cache size not correct") # assert self.cache.size() == 2, "exepected value is 2" def testB(self): """test case B""" self.cache = LRUCache(capacity=2) self.cache.put(key=2, value=2) self.cache.put(key=3, value=3) self.cache.put(key=4, value=3) self.assertEqual(self.cache.get(key=2), -1) def testC(self): """test case C"""
def cached_fn(*args): key = "" data_bytes = "" #print(func.__name__) cache = LRUCache(size) fname = func.__name__ if (len(args) == 1): key = args[0] #print(key) if (len(args) == 2): key = args[0] data_bytes = args[1] if (fname == 'put' or fname == 'fibonacci'): temp = LRUCacheItem(key, data_bytes) cache.insertItem(temp) print("LRU--->Number of Users Cached=" + str(len(item_list))) retval = func(*args) if (fname == 'get' or fname == 'get_data'): hashval = cache.get() if key in hashval: item = hashval[key] data_bytes, key = serialize_GET(item) print("LRU Get-->") print(key) retval = key print(deserialize(data_bytes)) else: retval = func(*args) if (retval != None): print("Server Get --> Success") print(key) else: print("Server Get-->" + key + '--->KEY NOT FOUND') if (fname == 'delete'): if (cache.remove(key)): print("LRU Delete-->" + key) else: print("LRU Delete-->" + key + '--->KEY NOT FOUND') retval = func(*args) print("Server Delete-->" + key) return retval
#!/usr/bin/env python # encoding=utf-8 from lru import LRUCache, LRUCacheOrdered from lfu import LFUCache import time if __name__ == '__main__': lru = LRUCache(2) lru.set(1, '1') lru.set(2, '2') lru.set(3, '3') print lru.get(1) n = 10**5 lru = LRUCache(n / 2) start = time.time() for i in xrange(n): lru.set(i, None) end = time.time() print 'total time:%.3f' % (end - start) print '%.3f / second' % (n / (end - start)) start = time.time() for i in xrange(n): lru.get(i) end = time.time() print 'total time:%.3f' % (end - start) print '%.3f / second' % (n / (end - start)) a = raw_input()
class TinyLFU: def __init__(self, size=1000000, sample=100000, false_positive=0.01): self.__age = 0 self.__sample = sample self.counter = CM4(size) self.doorkeeper = Doorkeeper(sample, false_positive) if size <= 1: size = 2 # percentage from https://arxiv.org/abs/1512.00727 lru_percent = 1 lru_size = (lru_percent * size) / 100 if lru_size < 1: lru_size = 1 self.lru = LRUCache(cache_size=lru_size) slru_size = math.ceil(((100.0 - lru_percent) / 100.0) * size) slru20_size = math.ceil(slru_size * 0.2) if slru20_size < 1: slru20_size = 1 self.slru = SLRUCache(probation_cap=slru20_size, protect_cap=slru_size - slru20_size) def __len__(self) -> int: return len(self.lru) + len(self.slru) def __contains__(self, key) -> bool: return key in self.lru or key in self.slru def get(self, key: str): # for tinylfu aging, reset only admission self.__age += 1 if self.__age == self.__sample: self.counter.reset() self.doorkeeper.reset() self.__age = 0 self.counter.add(key) value = self.lru.get(key) if value != None: # timeout return value value = self.slru.get(key) if value != None: # timeout return value def set(self, key: str, value): if key in self.slru: self.slru.remove(key) old_key, old_value, evicted = self.lru.set(key, value) if not evicted: return victim_key = self.slru.victim() if victim_key == None: self.slru.set(old_key, old_value) return if not self.doorkeeper.allow(old_key): # on evict return victim_count = self.counter.estimate(victim_key) item_count = self.counter.estimate(old_key) if victim_count < item_count: self.slru.set(old_key, old_value) else: # on evict return def remove(self, key: str) -> object: value = self.lru.remove(key) if value != None: return value value = self.slru.remove(key) if value != None: return value
#!/usr/bin/env python # encoding=utf-8 from lru import LRUCache, LRUCacheOrdered from lfu import LFUCache import time if __name__ == '__main__': lru = LRUCache(2) lru.set(1, '1') lru.set(2, '2') lru.set(3, '3') print lru.get(1) n = 10**5 lru = LRUCache(n/2) start = time.time() for i in xrange(n): lru.set(i, None) end = time.time() print 'total time:%.3f' % (end - start) print '%.3f / second' % (n/(end-start)) start = time.time() for i in xrange(n): lru.get(i) end = time.time() print 'total time:%.3f' % (end - start) print '%.3f / second' % (n/(end-start)) a = raw_input()
f = open('names_1.txt', 'r') names_1 = f.read().split("\n") # List containing 10000 names O(n) f.close() f = open('names_2.txt', 'r') names_2 = f.read().split("\n") # List containing 10000 names O(n) f.close() duplicates = [] # Return the list of duplicates in this data structure # Replace the nested for loops below with your improvements all_names = names_1 + names_2 lrucache = LRUCache(20000) for name in all_names: # if the name is already in the cache add to duplicates if lrucache.get(name): duplicates.append(name) lrucache.set(name, name) # for name in names_1: # if lrucache.get(name): # duplicates.append(name) # lrucache.set(name, name) # for name in names_2: # if lrucache.get(name): # duplicates.append(name) # lrucache.set(name, name) # 4.5 seconds # dll = DoublyLinkedListMod()
from lru import LRUCache if __name__ == '__main__': cache_limit = 4 cache = LRUCache(cache_limit) for i in range(0, 10): cache.set(i, '{0}00'.format(i)) if i % 2 == 0: print('Get a key', cache.get(0)) print(cache.cache) # STD_OUT # Get a key 000 # OrderedDict([(0, '000')]) # OrderedDict([(0, '000'), (1, '100')]) # Get a key 000 # OrderedDict([(1, '100'), (2, '200'), (0, '000')]) # OrderedDict([(1, '100'), (2, '200'), (0, '000'), (3, '300')]) # Get a key 000 # OrderedDict([(2, '200'), (3, '300'), (4, '400'), (0, '000')]) # OrderedDict([(3, '300'), (4, '400'), (0, '000'), (5, '500')]) # Get a key 000 # OrderedDict([(4, '400'), (5, '500'), (6, '600'), (0, '000')]) # OrderedDict([(5, '500'), (6, '600'), (0, '000'), (7, '700')]) # Get a key 000 # OrderedDict([(6, '600'), (7, '700'), (8, '800'), (0, '000')]) # OrderedDict([(7, '700'), (8, '800'), (0, '000'), (9, '900')])