Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用memcache提高检索性能python appengine_Python_Google App Engine - Fatal编程技术网

使用memcache提高检索性能python appengine

使用memcache提高检索性能python appengine,python,google-app-engine,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()

你好,我想知道在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))

循环中有
memcache.set()
。这给
memcache
服务带来了很多不必要的流量。循环完成后再做一次

此外,按照您的编码方式,无需建立
大小

foosTab = []
for foo in foos:
    foosTab.append(foo)
或者,更习惯地说

foosTab = [foo for foo in foos]
这将节省您单独执行
count()

此代码块:

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)
可替换为以下(更简单)代码:


具体地说,它避免了对
count()
的不必要的调用(这会为您在获取结果时无论如何都会发现的东西发出一个昂贵的RPC),它只执行一次获取调用,而不是迭代结果并以20次为一批进行获取,并且只调用memcache set(一次!)如果它必须首先获取列表。

相对于什么慢?你能提供一些关于这个和类似的东西的时间数据吗?foo中有多少个对象?你是在你的开发机器上运行这个,还是部署了它?如果您在调用foo.count()之前执行一个fetch(1000),它会更快吗?您好,非常慢,大约50个对象4秒,本地有点奇怪,它会更快,@peter我会测试fetch您使用过appstats吗?它的设计就是为了回答这类问题。+1,回答得好。此外,我通常存储实体键而不是孔元素。迭代查询结果是一个坏主意-它会导致每20个结果调用一个RPC。
foos = memcache.get("all-foos")
if not foos:
  foos = Foo.all.fetch(1000) # At most 1000 foos
  memcache.set("all-foos", foos, 60)