开发者

Do we need cache for an array?

Since we're developing a web-based project using django. we cache the db operation to make a better performance. But I'm wondering whether we need cache the array.

the code sample like this:

ABigArray = {
  "1" : {
     "name" : "xx",
     "gender" "xxx",
     ...
   },
  "2" : {
     ...
   },
   ...
  }
 class Items:
     def __init__(self):
         self.data = ABigArray

     def get_item_by_id(sel开发者_StackOverflow社区f, 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

So I'm wondering whether we really need such cache, since IMO the array( ABigArray ) will be loaded in memory when trying to get one item. So we don't need use cache in such condition, right? Or I'm wrong?

Please correct me if I'm wrong.

Thanks.


You've cut out a bit too much information, but it looks like the "array" (actually a dictionary) is always the same - there's a single instance that is created when the module is first imported, and will be used by every Items object. So there's absolutely nothing to be gained by caching it - in fact you will lose by doing so, as you will introduce an unnecessary round trip to get the data from the cache.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜