Skip to content

Commit

Permalink
Adding logging to the LRU Cache implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ShreyTiwari committed Apr 13, 2021
1 parent 771e57f commit 2fa442d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
22 changes: 20 additions & 2 deletions LRU_Cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,27 @@
# Importing the required modules
from Linked_List.doubly_linked_list import doubly_linked_list
from Helper.helper_functions import helper_functions
from Logger.logger import logger_wrapper

# Total Pages
TOTAL_PAGES = 10


# LRU Cache class implements the functionality of a LRU cache
class lru_cache:
def __init__(self, size):
def __init__(self, size, logger):
self.cache_size = size
self.ll = doubly_linked_list()
self.hashmap = [None] * TOTAL_PAGES
self.faults = 0
self.logger = logger

self.logger.info("LRU Cache Instance created and initialized")
self.logger.debug("LRU Cache Instance parameters are: Size - {}, Total Pages = {}".format(self.cache_size, TOTAL_PAGES))

def refer(self, page):
self.logger.info("Cache refer call was made")

if self.hashmap[page] is not None:
status = "Cache Hit!"
self.ll.move_node_back(self.hashmap[page])
Expand All @@ -42,19 +49,28 @@ def refer(self, page):
node = self.ll.add_node_back(page)
self.hashmap[page] = node

self.logger.debug("The page requested was {} and the refer call result was - {}".format(page, status))
return status

def display_cache(self):
self.logger.info("Display Cache call was made")

print("The total number of cache misses as far is: ", self.faults)
print("The present cache state is")
self.ll.display_list()

self.logger.debug("The total cache misses are - {}".format(self.faults))


# Code execution begins here
def main():
lw = logger_wrapper()
logger = lw.new_logger()

logger.debug("Program Execution Initiated")
print("------------------------------ Welcome ------------------------------")
print("Please specify the cache size...")
lru_cache_test = lru_cache(helper_functions.accept_int())
lru_cache_test = lru_cache(helper_functions.accept_int(), logger)

while True:
print("\n1. Refer Cache\n2. Display Cache\nPress any other key to exit...")
Expand All @@ -70,6 +86,8 @@ def main():
print("\n------------------------------ Thank You ------------------------------")
break

logger.debug("Program Execution Completed")


if __name__ == '__main__':
main()
7 changes: 5 additions & 2 deletions Logger/Logger.py → Logger/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ def new_logger(self):
def test():
log_wrapper = logger_wrapper()
logger = log_wrapper.new_logger()
logger.info("Hello")
logger.debug("debug")
logger.debug("Hello world!")
logger.info("This is a test message")
logger.warning("Being printed from the logger wrapper class")
logger.error("To ensure appropriate formatting of logs")
logger.critical("And the correctness of code.")


if __name__ == '__main__':
Expand Down

0 comments on commit 2fa442d

Please sign in to comment.