Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. import collections
  2. import time
  3. import pytest
  4.  
  5. class LRUCacheWithTTL:
  6.     def __init__(self, capacity, ttl):
  7.         if capacity<0 or ttl<0:
  8.             raise ValueError("Capacity and Time To Live (TTL) must be positive integers.")
  9.         self.capacity = capacity
  10.         self.cache = collections.OrderedDict()
  11.         self.ttlItems = {}
  12.         self.ttl=ttl
  13.     def lookup(self, key):
  14.         try:
  15.             value = self.cache.pop(key)
  16.             self.cache[key] = value
  17.             self.ttlItems[key] = time.time()
  18.             return value
  19.         except KeyError:
  20.             raise KeyError("Key not found in cache")
  21.             #return -1
  22.  
  23.     def put(self, key, value):
  24.         try:
  25.             self.cache.pop(key)
  26.         except KeyError:
  27.             if len(self.cache) >= self.capacity:
  28.                 keyValPair=self.cache.popitem(last=False)
  29.                 del self.ttlItems[keyValPair[0]]
  30.         self.cache[key] = value
  31.         self.ttlItems[key]=time.time()
  32.  
  33.    
  34.     def validate(self):
  35.         currentTime=time.time()
  36.         for key in self.cache:
  37.             lastModTime=self.ttlItems[key]
  38.             if((currentTime-lastModTime)>self.ttl):
  39.                 #del self.cache[key]
  40.                 #print(key,self.cache[key])
  41.                 #print(key,self.ttlItems[key])
  42.                 del self.ttlItems[key]
  43.                 del self.cache[key]
  44.  
  45.     def __iter__(self):
  46.         #l = collections.OrderedDict(reversed(list(self.cache.items())))
  47.         #l = reversed(list(self.cache.items()))
  48.         cache = reversed(self.cache.items())
  49.         return iter(cache)
  50.  
  51.  
  52. cache=LRUCacheWithTTL(10,10)
  53.  
  54. cache.put(4,23)
  55. cache.put(5,19)
  56. cache.put(6,28)
  57. cache.put(7,27)
  58. cache.put(8,26)
  59. cache.put(9,25)
  60. cache.put("fasoli",24)
  61. cache.put(11,22)
  62. cache.put(12,21)
  63. cache.put(13,16)
  64.  
  65. for e in cache:
  66.     print(e)
  67.  
  68. """
  69. cache=LRUCacheWithTTL(1,10)
  70.  
  71. cache.put("test",12)
  72. time.sleep(4)
  73.  
  74. cache.ttlItems["test"]
  75. cache.lookup("test")
  76.  
  77. time.sleep(11)
  78.  
  79. cache.ttlItems["test"]
  80.  
  81. cache.validate()
  82.  
  83. cache.lookup("test")
  84. """
  85.  
  86. """
  87. cache.set(1,16)
  88. cache.set(2,17)
  89. cache.set(3,18)
  90. cache.set(4,23)
  91. cache.set(5,19)
  92. cache.set(6,28)
  93. cache.set(7,27)
  94. cache.set(8,26)
  95. cache.set(9,25)
  96. cache.set(10,24)
  97. cache.set(11,22)
  98. cache.set(12,21)
  99. cache.set(13,16)
  100. cache.set(14,17)
  101. cache.set(15,18)
  102. cache.set(16,23)
  103. cache.set(17,19)
  104. cache.set(18,28)
  105. cache.set(19,27)
  106. cache.set(20,26)
  107. cache.set(21,25)
  108. cache.set(22,24)
  109. cache.set(23,22)
  110. cache.set(24,21)
  111. cache.set(25,22)
  112. cache.set(26,21)
  113.  
  114. #cache.get(34)
  115.  
  116. print (cache.cache)
  117. print("\n")
  118.  
  119. cache.get(10)
  120. print(cache.cache)
  121. print("\n")
  122.  
  123.  
  124. #if(cache.get(34) == -1):
  125. #    print("Error key not found\n")
  126.  
  127.  
  128. cache.set(28,877)
  129. cache.get(21)
  130. print(cache.cache)
  131. print("\n")
  132.  
  133.  
  134. print(len(cache.cache))
  135. time.sleep(4)
  136. cache.validate()
  137.  
  138.  
  139. for e,i in cache.cache:
  140.    print(e)
  141. """
  142.  
  143.  
  144.  
  145. def test_cache_put(capsys):
  146.     cache = LRUCacheWithTTL(2, 100)
  147.     cache.put("hi", 20)
  148.     cache.put("hello", 10)
  149.  
  150.     assert cache.lookup("hi") == 20
  151.     assert cache.lookup("hello") == 10
  152.  
  153. def test_cache_shiftwork(capsys):
  154.     cache = LRUCacheWithTTL(1, 100)
  155.     cache.put("hi", 20)
  156.     cache.put("hello", 10)
  157.  
  158.     assert cache.lookup("hello") == 10
  159.  
  160. def test_cache_iter(capsys):
  161.     cache = LRUCacheWithTTL(2, 100)
  162.     cache.put("hi", 20)
  163.     cache.put("hello", 10)
  164.  
  165.  
  166. def test_Validate(capsys):
  167.     cache=LRUCacheWithTTL(1,10)
  168.     cache.put("test",12)
  169.     time.sleep(4)
  170.     assert cache.lookup("test") == 12
  171.     time.sleep(11)
  172.     cache.validate()
  173.     with pytest.raises(KeyError):
  174.          assert cache.lookup("test") == 12
  175.  
  176. def test_keyTTL(capsys):
  177.     with pytest.raises(ValueError):
  178.         assert LRUCacheWithTTL(-1,20)
  179.         assert LRUCacheWithTTL(1,-20)
  180.    
  181.  
  182.  
  183. """
  184.    for e in cache:
  185.        print(e)
  186. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement