This project contains tasks for learning to cache data.
-
0. Basic dictionary
0-basic_cache.py contains a Python classBasicCache
that inherits fromBaseCaching
and is a caching system:- You must use
self.cache_data
- dictionary from the parent classBaseCaching
. - This caching system doesn't have limit.
def put(self, key, item):
:- Must assign to the dictionary
self.cache_data
theitem
value for the keykey
. - If
key
oritem
isNone
, this method should not do anything.
- Must assign to the dictionary
def get(self, key):
:- Must return the value in
self.cache_data
linked tokey
. - If
key
isNone
or if thekey
doesn't exist inself.cache_data
, returnNone
.
- Must return the value in
- You must use
-
1. FIFO caching
1-fifo_cache.py contains a Python classFIFOCache
that inherits fromBaseCaching
and is a caching system:- You must use
self.cache_data
- dictionary from the parent classBaseCaching
. - You can overload
def __init__(self):
but don't forget to call the parent init:super().__init__()
. def put(self, key, item):
:- Must assign to the dictionary
self.cache_data
theitem
value for the keykey
. - If
key
oritem
isNone
, this method should not do anything. - If the number of items in
self.cache_data
is higher thanBaseCaching.MAX_ITEMS
:- You must discard the first item put in cache (FIFO algorithm).
- You must print
DISCARD:
with the key discarded and following by a new line.
- Must assign to the dictionary
def get(self, key):
:- Must return the value in
self.cache_data
linked tokey
. - If
key
isNone
or if thekey
doesn't exist inself.cache_data
, returnNone
.
- Must return the value in
- You must use
-
2. LIFO Caching
2-lifo_cache.py contains a Python classLIFOCache
that inherits fromBaseCaching
and is a caching system:- You must use
self.cache_data
- dictionary from the parent classBaseCaching
. - You can overload
def __init__(self):
but don't forget to call the parent init:super().__init__()
. def put(self, key, item):
:- Must assign to the dictionary
self.cache_data
theitem
value for the keykey
. - If
key
oritem
isNone
, this method should not do anything. - If the number of items in
self.cache_data
is higher thanBaseCaching.MAX_ITEMS
:- You must discard the last item put in cache (LIFO algorithm).
- You must print
DISCARD:
with thekey
discarded and following by a new line.
- Must assign to the dictionary
def get(self, key):
:- Must return the value in
self.cache_data
linked to key. - If
key
isNone
or if thekey
doesn't exist inself.cache_data
, returnNone
.
- Must return the value in
- You must use
-
3. LRU Caching
3-lru_cache.py contains a Python classLRUCache
that inherits fromBaseCaching
and is a caching system:- You must use
self.cache_data
- dictionary from the parent classBaseCaching
. - You can overload
def __init__(self):
but don't forget to call the parent init:super().__init__()
. def put(self, key, item):
:- Must assign to the dictionary
self.cache_data
theitem
value for the keykey
. - If
key
oritem
isNone
, this method should not do anything. - If the number of items in
self.cache_data
is higher thanBaseCaching.MAX_ITEMS
:- You must discard the least recently used item (LRU algorithm).
- You must print
DISCARD:
with thekey
discarded and following by a new line.
- Must assign to the dictionary
def get(self, key):
:- Must return the value in
self.cache_data
linked tokey
. - If
key
isNone
or if thekey
doesn't exist inself.cache_data
, returnNone
.
- Must return the value in
- You must use
-
4. MRU Caching
4-mru_cache.py contains a Python classMRUCache
that inherits fromBaseCaching
and is a caching system:- You must use
self.cache_data
- dictionary from the parent classBaseCaching
. - You can overload
def __init__(self):
but don't forget to call the parent init:super().__init__()
. def put(self, key, item):
:- Must assign to the dictionary
self.cache_data
theitem
value for the keykey
. - If
key
oritem
isNone
, this method should not do anything. - If the number of items in
self.cache_data
is higher thanBaseCaching.MAX_ITEMS
:- You must discard the most recently used item (MRU algorithm).
- You must print
DISCARD:
with thekey
discarded and following by a new line
- Must assign to the dictionary
def get(self, key):
:- Must return the value in
self.cache_data
linked tokey
. - If
key
isNone
or if thekey
doesn't exist inself.cache_data
, returnNone
.
- Must return the value in
- You must use
-
5. LFU Caching
100-lfu_cache.py contains a Python class LFUCache that inherits fromBaseCaching
and is a caching system:- You must use
self.cache_data
- dictionary from the parent classBaseCaching
. - You can overload
def __init__(self):
but don't forget to call the parent init:super().__init__()
. def put(self, key, item):
:- Must assign to the dictionary
self.cache_data
theitem
value for the keykey
. - If
key
oritem
isNone
, this method should not do anything. - If the number of items in
self.cache_data
is higher thanBaseCaching.MAX_ITEMS
:- You must discard the least frequency used item (LFU algorithm).
- If you find more than 1 item to discard, you must use the LRU algorithm to discard only the least recently used.
- You must print
DISCARD:
with thekey
discarded and following by a new line.
- Must assign to the dictionary
def get(self, key):
:- Must return the value in
self.cache_data
linked tokey
. - If
key
isNone
or if thekey
doesn't exist inself.cache_data
, returnNone
.
- Must return the value in
- You must use