Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import collections
- import time
- import pytest
- class LRUCacheWithTTL:
- def __init__(self, capacity, ttl):
- if capacity<0 or ttl<0:
- raise ValueError("Capacity and Time To Live (TTL) must be positive integers.")
- self.capacity = capacity
- self.cache = collections.OrderedDict()
- self.ttlItems = {}
- self.ttl=ttl
- def lookup(self, key):
- try:
- value = self.cache.pop(key)
- self.cache[key] = value
- self.ttlItems[key] = time.time()
- return value
- except KeyError:
- raise KeyError("Key not found in cache")
- #return -1
- def put(self, key, value):
- try:
- self.cache.pop(key)
- except KeyError:
- if len(self.cache) >= self.capacity:
- keyValPair=self.cache.popitem(last=False)
- del self.ttlItems[keyValPair[0]]
- self.cache[key] = value
- self.ttlItems[key]=time.time()
- def validate(self):
- currentTime=time.time()
- for key in self.cache:
- lastModTime=self.ttlItems[key]
- if((currentTime-lastModTime)>self.ttl):
- #del self.cache[key]
- #print(key,self.cache[key])
- #print(key,self.ttlItems[key])
- del self.ttlItems[key]
- del self.cache[key]
- def __iter__(self):
- #l = collections.OrderedDict(reversed(list(self.cache.items())))
- #l = reversed(list(self.cache.items()))
- cache = reversed(self.cache.items())
- return iter(cache)
- cache=LRUCacheWithTTL(10,10)
- cache.put(4,23)
- cache.put(5,19)
- cache.put(6,28)
- cache.put(7,27)
- cache.put(8,26)
- cache.put(9,25)
- cache.put("fasoli",24)
- cache.put(11,22)
- cache.put(12,21)
- cache.put(13,16)
- for e in cache:
- print(e)
- """
- cache=LRUCacheWithTTL(1,10)
- cache.put("test",12)
- time.sleep(4)
- cache.ttlItems["test"]
- cache.lookup("test")
- time.sleep(11)
- cache.ttlItems["test"]
- cache.validate()
- cache.lookup("test")
- """
- """
- cache.set(1,16)
- cache.set(2,17)
- cache.set(3,18)
- cache.set(4,23)
- cache.set(5,19)
- cache.set(6,28)
- cache.set(7,27)
- cache.set(8,26)
- cache.set(9,25)
- cache.set(10,24)
- cache.set(11,22)
- cache.set(12,21)
- cache.set(13,16)
- cache.set(14,17)
- cache.set(15,18)
- cache.set(16,23)
- cache.set(17,19)
- cache.set(18,28)
- cache.set(19,27)
- cache.set(20,26)
- cache.set(21,25)
- cache.set(22,24)
- cache.set(23,22)
- cache.set(24,21)
- cache.set(25,22)
- cache.set(26,21)
- #cache.get(34)
- print (cache.cache)
- print("\n")
- cache.get(10)
- print(cache.cache)
- print("\n")
- #if(cache.get(34) == -1):
- # print("Error key not found\n")
- cache.set(28,877)
- cache.get(21)
- print(cache.cache)
- print("\n")
- print(len(cache.cache))
- time.sleep(4)
- cache.validate()
- for e,i in cache.cache:
- print(e)
- """
- def test_cache_put(capsys):
- cache = LRUCacheWithTTL(2, 100)
- cache.put("hi", 20)
- cache.put("hello", 10)
- assert cache.lookup("hi") == 20
- assert cache.lookup("hello") == 10
- def test_cache_shiftwork(capsys):
- cache = LRUCacheWithTTL(1, 100)
- cache.put("hi", 20)
- cache.put("hello", 10)
- assert cache.lookup("hello") == 10
- def test_cache_iter(capsys):
- cache = LRUCacheWithTTL(2, 100)
- cache.put("hi", 20)
- cache.put("hello", 10)
- def test_Validate(capsys):
- cache=LRUCacheWithTTL(1,10)
- cache.put("test",12)
- time.sleep(4)
- assert cache.lookup("test") == 12
- time.sleep(11)
- cache.validate()
- with pytest.raises(KeyError):
- assert cache.lookup("test") == 12
- def test_keyTTL(capsys):
- with pytest.raises(ValueError):
- assert LRUCacheWithTTL(-1,20)
- assert LRUCacheWithTTL(1,-20)
- """
- for e in cache:
- print(e)
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement