Setting max cache length as the input size given

pull/8/head
sgoudham 4 years ago
parent 9aedb34069
commit 794bf3d21d

@ -16,6 +16,8 @@
# Built by karan#7508 # Built by karan#7508
# Edited by Hamothy#5619 # Edited by Hamothy#5619
# TODO: UPDATE ALL COMMENTARY TO REFLECT OUR WORK
import threading import threading
@ -30,9 +32,6 @@ class CachingCircularQueue:
# The maximum size of the queue # The maximum size of the queue
self.MAX_SIZE = size self.MAX_SIZE = size
def increase_size(self, size):
"""Increase the size of the queue"""
def push(self, value): def push(self, value):
# thread safe # thread safe
with self.threadLock: with self.threadLock:
@ -66,7 +65,6 @@ class CachingCircularQueue:
# PRECONDITION, VALUE EXISTS IN CACHE, SO SHOULD EXIST IN LIST # PRECONDITION, VALUE EXISTS IN CACHE, SO SHOULD EXIST IN LIST
if value in self.values: if value in self.values:
self.values.remove(value) self.values.remove(value)
# As you said, to ensure concurrency, set the current size back to the length of the array
class MyCoolCache: class MyCoolCache:
@ -77,30 +75,30 @@ class MyCoolCache:
self.queue = CachingCircularQueue(size) self.queue = CachingCircularQueue(size)
self.cache = {} self.cache = {}
def decrease_size(self, size): def get_size(self):
"""Decrease the size of the array""" """Return size of cache and queue"""
return self.MAX_SIZE, len(self.cache), len(self.queue.values)
def change_array_size(self, input_size): def change_array_size(self, input_size):
"""Dynamically change the size of the array"""
with self.threadLock: with self.threadLock:
try: # Increase the size of the array and the cache to the new size
# Increase the size of the array and the cache to the new size if input_size > self.MAX_SIZE:
if input_size > self.MAX_SIZE: self.queue.MAX_SIZE = input_size
self.queue.MAX_SIZE = input_size self.MAX_SIZE = input_size
self.MAX_SIZE = input_size else:
else: # Split Array into 2 and iterate through the queue and delete things
# Split Array into 2 and iterate through the queue and delete things for value in self.queue.values[input_size:]:
for value in self.queue.values[input_size:]: self.cache.pop(value)
self.cache.pop(value)
# Make sure only the records up until the size specified are stored
# Make sure only the records up until the size specified are stored self.queue.values = self.queue.values[:input_size]
self.queue.values = self.queue.values[:input_size]
# Set max size of queue and cache to be the length of the new queue
# Set max size of queue and cache to be the length of the new queue self.queue.MAX_SIZE = len(self.queue.values)
self.queue.MAX_SIZE = len(self.queue.values) self.MAX_SIZE = input_size
self.MAX_SIZE = len(self.queue.values)
except Exception as e:
print(e)
def store_cache(self, key, dict_item): def store_cache(self, key, dict_item):
with self.threadLock: with self.threadLock:

Loading…
Cancel
Save