In: Computer Science
(python please)
The Cache Directory (Hash Table):The class Cache()is the data structure you will use to store the three other caches (L1, L2, L3). It stores anarray of 3 CacheLists, which are the linked list implementations of the cache (which will be explained later). Each CacheList has been already initialized to a size of 200. Do not change this. There are 3 functions you must implement yourself:
•def hashFunc(self, contentHeader)oThis function determines which cache level your content should go in. Create a hash function that sums the ASCII values of each content header, takes the modulus of that sum with the size of the Cache hash table, and accordingly assigns an index for that content –corresponding to the L1, L2, or L3 cache. So, for example, let’sassume the header “Content-Type: 2”sumsto an ASCII value of 1334. 1334% 3 = 2. So, that content would go in the L3cache. You should notice a very obvious pattern in which contentheaders correspond to which caches. The hash function should be used by your insert and retrieveContent functions.
•def insert(self, content, evictionPolicy)oOnce a content object is created, call the cache directory’s insert function to insert it into the proper cache. This function should call the linked list’s implementation of the insert function to actually insert into the cache itself. The eviction policywill be a string –either ‘lru’or ‘mru’. These eviction policies will be explained later. oThis function should return a message including the attributes of the content object inserted into the cache. This is shown in the doctests.
•def retrieveContent(self, content)oThis function should take a content object, determine which level it is in, and then return the object if it is in the cache at that level. If not, it should return a message indicating that it was not found. This is known as a “cache miss”. Accordingly, finding content in a cache is known as a “cache hit”. The doctests show the format of the return statements.
Code:
class Cache:
def __init__(self):
self.L1cache = []
self.L2cache = []
self.L3cache = []
for i in range(200):
L1cache.append("")
L2cache.append("")
L3cache.append("")
def hashFunc(self,contentHeader):
temp = 0
for i in contentHeader:
temp += ord(i)
return temp%3
def insert(self,content,evictionPolicy):
ci = hashFunc(content)
ei = findIndex(evictionPolicy,ci)
if(ci==0):
L3cache[ei] = content
elif(ci == 1):
L1cache[ei] = content
else:
L2cache[ei] = content
return "The content: "+ content + " has been inserted
successfully."
def retrieveContent(self,content):
ci = hashFunc(self,content)
if(ci == 0):
for cont in self.L3cache:
if cont==content:
return "Cache hit" + content
return "Cache Miss"
elif(ci == 1):
for cont in self.L1cache:
if cont==content:
return "Cache hit" + content
return "Cache Miss"
else:
for cont in self.L2cache:
if cont == content:
return "Cache hit" + content
return "Cache Miss"
Note: I used an example Cache() class with given specifications. You'll have to change the variable names and add the function definition to your own Cache() class. If you want answer according to your Cache() class, please post the entire code provided to you. We'd be happy to help :)