【发布时间】:2009-11-05 12:47:06
【问题描述】:
因为我们正在使用 django 开发一个基于 Web 的项目。我们缓存 db 操作以获得更好的性能。但我想知道我们是否需要缓存数组。
代码示例如下:
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)) # get the cached item if possible
if item:
return item
else:
item = self.data.get(str(id))
cache.set("item" + str(id), item)
return item
所以我想知道我们是否真的需要这样的缓存,因为 IMO 数组( ABigArray ) 将在尝试获取一项时加载到内存中。所以我们不需要在这种情况下使用缓存,对吧?还是我错了?
如果我错了,请纠正我。
谢谢。
【问题讨论】:
-
这与您几分钟前问的问题基本相同吗?
-
是的,差不多。但不同的问题。我想提一个问题,但似乎不太合适。