我应该使用memcache吗?

2024-06-06 23:40:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码从googleappengine生成一个zip file,用于在浏览器中加载,即10kb<;size<;1000kb,我想知道我是否可以在这里使用memcache,或者文件是否太大或已经缓存。我已经使用了memcache并在第一次生成实际文件时设置了缓存控制。在

class KMZHandler(webapp.RequestHandler):
    def add_file(self, zip_file, url, file_name):
        """Fetch url, and add content as file_name to the zip file."""
        result = urlfetch.fetch(url)
        if not result.content:
            return
        zip_file.writestr(file_name, result.content)

    def get(self):
        """Attempt to create a zip file."""
        # you could set 'count' like this:
        # count = int(self.request.get('count', 1000))

        zipstream = StringIO.StringIO()
        zip_file = zipfile.ZipFile(zipstream, "w")

        # repeat this for every URL that should be added to the zipfile
        url = 'http://www.koolbusiness.com/list.kml'
        self.add_file(zip_file, url, "list.kml")

        # we have finished with the zip so package it up and write the directory
        zip_file.close()

        # set the headers...

        self.response.headers["Cache-Control"] = "public,max-age=%s" % 86400
        self.response.headers['Content-Type'] ='application/zip'
        self.response.headers['Content-Disposition'] = 'attachment;filename="list.kmz"'

        # create and return the output stream
        zipstream.seek(0)
        self.response.out.write(zipstream.read())
        zipstream.close()

以下是使用memcache并创建实际文件的部分:

^{pr2}$

谢谢你的回答


Tags: and文件thetonameselfaddurl
1条回答
网友
1楼 · 发布于 2024-06-06 23:40:30

如果文件是under 1mb,并且它不会因每个请求而更改,那么KMZ的memcaching可能会减少资源使用。

KMZ文件不在memcache中,尽管它可能在前端缓存中。当您生成KML时,您正在memcaching查询的结果(参见Nick的博客以获得关于how to memcache entities的描述),但是缓存没有考虑count或{}的不同值。如果这并不重要,您也可以考虑直接对KML(或KMZ)文件进行memcaching;如果重要,则需要修改缓存策略。

相关问题 更多 >