Since we are developing a web project using django. we cache the db operation to improve performance. But I'm wondering if we need a cache array.
sample code as follows:
ABigArray = {
"1" : {
"name" : "xx",
"gender" "xxx",
...
},
"2" : {
...
},
...
}
class Items:
def __init__(self):
self.data = ABigArray
def get_item_by_id(self, id):
item = cache.get("item" + str(id))
if item:
return item
else:
item = self.data.get(str(id))
cache.set("item" + str(id), item)
return item
So, I am wondering if we need such a cache, since the IMO array (ABigArray) will be loaded into memory when trying to get one element. So we don’t need to use the cache in this state, right? Or I'm wrong?
Please correct me if I am wrong.
Thanks.
source
share