TurboGears的cache decorator

問題

今天在寫TurboGears網頁時,因為遇到用matplotlib產生的圖片

    @expose_matplot_figure
    def figure(self, id):
        return dict(figure=pieFigure(id), dpi=75)

如果每次都重新產生一張新圖片,流量大時對主機是件很浪費資源的事情,理所當然第一個想到的就是cache,而cache要能知道有什麼東西改變了是否需要重新產生,而最好能夠不用為了每個exposed的頁面寫一個cache,因此最好的辦法就是用decorator來使用,可以丟函數進去來判斷是否是一樣的東西,是否需要重新產生等等,正當我要開始寫時…

解決方案

發現網路上已經有寫好的解決方案

A turbogears caching decorator

寫得很不錯,這樣一來我就可以直接cache我的圖片

    def getFigureKey(self, id):
        return id
 
    def getFigureVersion(self, id):
        wangea = Wangea.get(id)
        return str([item.agree for item in wangea.items])
 
    @expose()
    @cache_result(key_fn=getFigureKey, version_fn=getFigureVersion)
    @expose_matplot_figure
    def figure(self, id):
        return dict(figure=pieFigure(id), dpi=75)
書籤:
  • Hemidemi
  • funp
  • MyShare
  • udn
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • YahooKimo
  • Haohao
  • TwitThis
This entry was posted in 中文文章, Python and tagged , . Bookmark the permalink.

發表迴響

您的電子郵件位址並不會被公開。 必要欄位標記為 *

*

您可以使用這些 HTML 標籤與屬性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">