使用memcache增加检索perf python appengine

Chr*_*ove 2 python google-app-engine

您好我想知道如果在appengine环境中我做的事情是有用的因为我不知道为什么这个页面很慢.

class Foo(db.Model):
    id = db.StringProperty(multiline=True)
    name = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(auto_now_add=True)
    description = db.TextProperty()    
    carac = db.StringProperty(multiline=True)



class FoosPage(webapp.RequestHandler):
  def get(self):
    foos =  memcache.get("all-foos")
    if foos is None:
        foos = Foo.all()
        size = foo.count()
    else :
        size = len(foo)

    foosTab=[]
    for i in range(size) :
        foosTab.insert(i,foos[i])
        memcache.set("all-foos", foosTab, 60)
    template_values = {
        'foos': foos
            }
    path = os.path.join(os.path.dirname(__file__) + '/../templates/', 'foos.html')
    self.response.out.write(template.render(path, template_values))
Run Code Online (Sandbox Code Playgroud)

Dav*_*ith 5

你有memcache.set()循环内部.这对服务来说是很多不必要的流量memcache.在循环之后执行一次.

此外,您编码的方式,没有必要建立size.

foosTab = []
for foo in foos:
    foosTab.append(foo)
Run Code Online (Sandbox Code Playgroud)

或者,更具有惯用力

foosTab = [foo for foo in foos]
Run Code Online (Sandbox Code Playgroud)

这将节省你做一个单独的count().