From 794bf3d21d14e67f9643802e1ea408f49b7dfa10 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Sat, 22 Aug 2020 04:58:55 +0100 Subject: [PATCH] Setting max cache length as the input size given --- bot/libs/cache.py | 48 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/bot/libs/cache.py b/bot/libs/cache.py index 06da4bd8..43d58d85 100644 --- a/bot/libs/cache.py +++ b/bot/libs/cache.py @@ -16,6 +16,8 @@ # Built by karan#7508 # Edited by Hamothy#5619 +# TODO: UPDATE ALL COMMENTARY TO REFLECT OUR WORK + import threading @@ -30,9 +32,6 @@ class CachingCircularQueue: # The maximum size of the queue self.MAX_SIZE = size - def increase_size(self, size): - """Increase the size of the queue""" - def push(self, value): # thread safe with self.threadLock: @@ -66,7 +65,6 @@ class CachingCircularQueue: # PRECONDITION, VALUE EXISTS IN CACHE, SO SHOULD EXIST IN LIST if value in self.values: self.values.remove(value) - # As you said, to ensure concurrency, set the current size back to the length of the array class MyCoolCache: @@ -77,30 +75,30 @@ class MyCoolCache: self.queue = CachingCircularQueue(size) self.cache = {} - def decrease_size(self, size): - """Decrease the size of the array""" + def get_size(self): + """Return size of cache and queue""" + + return self.MAX_SIZE, len(self.cache), len(self.queue.values) def change_array_size(self, input_size): + """Dynamically change the size of the array""" + with self.threadLock: - try: - # Increase the size of the array and the cache to the new size - if input_size > self.MAX_SIZE: - self.queue.MAX_SIZE = input_size - self.MAX_SIZE = input_size - else: - # Split Array into 2 and iterate through the queue and delete things - for value in self.queue.values[input_size:]: - self.cache.pop(value) - - # Make sure only the records up until the size specified are stored - self.queue.values = self.queue.values[:input_size] - - # Set max size of queue and cache to be the length of the new queue - self.queue.MAX_SIZE = len(self.queue.values) - self.MAX_SIZE = len(self.queue.values) - - except Exception as e: - print(e) + # Increase the size of the array and the cache to the new size + if input_size > self.MAX_SIZE: + self.queue.MAX_SIZE = input_size + self.MAX_SIZE = input_size + else: + # Split Array into 2 and iterate through the queue and delete things + for value in self.queue.values[input_size:]: + self.cache.pop(value) + + # Make sure only the records up until the size specified are stored + self.queue.values = self.queue.values[:input_size] + + # Set max size of queue and cache to be the length of the new queue + self.queue.MAX_SIZE = len(self.queue.values) + self.MAX_SIZE = input_size def store_cache(self, key, dict_item): with self.threadLock: