-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
32 lines (26 loc) · 1.06 KB
/
cache.py
File metadata and controls
32 lines (26 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Implements specialized container data types
import collections
# Set cache size
CACHE_SIZE = 16
# Creates a cache as a dequeue data type
class Cache:
# Initialization function - stored as dequeue of tuples and initialized as empty
def __init__(self):
self.cache = collections.deque(maxlen=CACHE_SIZE)
self.flush_cache()
# Replace all bits in the cache
def flush_cache(self):
for bit in range(CACHE_SIZE):
self.cache.append(("", ""))
print("Cache flushed")
# Write to cache by appending address/value tuple
def write_cache(self, address, value):
self.cache.append((address, value))
print("Wrote " + str(value) + " to cache at " + str(address))
# Search cache by address. Returns value if found or None if not
def search_cache(self, address):
for address in range(CACHE_SIZE):
if self.cache[address][0] == address:
print("Found " + str(self.cache[address][1]) + " at " + str(address))
return self.cache[address][1]
return None