【问题标题】:Should I use memcache?我应该使用内存缓存吗?
【发布时间】:2011-04-17 07:04:59
【问题描述】:

我的代码从 Google App Engine 生成一个 zip file 用于在浏览器中加载,即 10 KB

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 并创建实际文件的部分:

class KMLHandler(webapp.RequestHandler):
 def get(self):   
    self.response.headers["Cache-Control"] = "public,max-age=%s" % 86400
    start=datetime.datetime.now()-timedelta(days=20)
    count = int(self.request.get('count')) if not self.request.get('count')=='' else 1000        
    from google.appengine.api import memcache
    memcache_key = "ads"
    data = memcache.get(memcache_key)
    if data is None:
      a= Ad.all().filter("modified >", start).filter("url IN", ['www.koolbusiness.com']).filter("published =", True).order("-modified").fetch(count)
      memcache.set("ads", a)  
    else:
      a = data
    dispatch='templates/kml.html'
    template_values = {'a': a , 'request':self.request,}
    path = os.path.join(os.path.dirname(__file__), dispatch)
    output = template.render(path, template_values)
    self.response.headers['Content-Type'] = 'application/vnd.google-earth.kml+xml'
    self.response.headers['Content-Length'] = len(output)
    self.response.out.write(output)

感谢您的回答

【问题讨论】:

  • 我们应该知道您的文件实际有多大吗?如果您不提供有关文件大小的最明显信息,应该如何给出建议?
  • 我链接到大小可能不同的 zip 文件。这是一个用于在浏览器中加载的 KMZ 文件,我想您可能知道在这些情况下文件的最大值。

标签: python google-app-engine kmz


【解决方案1】:

如果文件是under 1mb,并且不会因每个请求而更改,那么对 KMZ 进行内存缓存可能会减少资源使用量。

KMZ 文件尚未在内存缓存中,但它可能在前端缓存中。当您生成 KML 时,您正在缓存查询的结果(有关 how to memcache entities 的描述,请参阅 Nick 的博客),但缓存不考虑 count 或 start 的不同值。 tf 那不重要,你也可以考虑直接memcaching KML(或KMZ)文件;如果它很重要,您需要修复缓存策略。

【讨论】:

  • 很高兴认识罗伯特。感谢您提供信息丰富的答案。
猜你喜欢
  • 2016-03-08
  • 2023-03-03
  • 1970-01-01
  • 2017-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-29
  • 2016-08-15
相关资源
最近更新 更多