3

私のコードは、ブラウザにロードするためにGoogle App Engineからzipファイルを生成します(10 KB<サイズ<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を使用して実際のファイルを作成する部分は次のとおりです。

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)

答えてくれてありがとう

4

1 に答える 1

2

ファイルが1mb 未満で、リクエストごとに変更されない場合、KMZ を memcaching するとリソースの使用量が削減される可能性があります。

KMZ ファイルはまだ memcache にありませんが、フロントエンド キャッシュにある可能性があります。KML を生成するときに、クエリの結果を memcaching しています (エンティティを memcache する方法countの説明については、Nick のブログを参照してください) が、キャッシュは、またはの異なる値startを考慮しません。これは重要ではありませんが、KML (または KMZ) ファイルを直接 memcaching することも検討してください。重要な場合、キャッシュ戦略を修正する必要があります。

于 2011-04-17T14:08:30.570 に答える