在appengine中配置自动重新加载模板并为jinja2启用字节码缓存

时间:2014-01-24 10:28:38

标签: python google-app-engine jinja2

如何在Appengine中将jinja2配置为:

  1. 模板更新时自动重新加载。
  2. 启用字节码缓存,因此可以在每个实例之间共享。我更喜欢jinja2在编译模板时生成字节码,并将其存储到数据存储区。因此,下一个实例将加载字节码而不是重复编译模板。

2 个答案:

答案 0 :(得分:3)

我已经使用app引擎memcache Client()::

添加了这样的密件抄送
loader = dynloaders.DynLoader()     # init Function loader
bcc = MemcachedBytecodeCache(memcache.Client(), prefix='jinja2/bytecode/', timeout=None)
return Environment(auto_reload=True, cache_size=100, loader=FunctionLoader(loader.load_dyn_all),
                   bytecode_cache=bcc)

我的函数加载器:

def html(self, cid):

    def _html_txt_up_to_date():  # closure to check if template is up to date

        return CMSUpdates.check_no_update(cid, template.modified)

    template = ndb.Key('Templates', cid, parent=self.parent_key).get()
    if not template:
        logging.error('DynLoader (HTML/TXT): %s' % cid)
        return None              # raises TemplateNotFound exception

    return template.content, None, _html_txt_up_to_date

模板模型使用template.modified:ndb.DateTimeProperty(auto_now = True)

关闭功能:

class CMSUpdates(ndb.Model):                                                                        
    updates = ndb.JsonProperty()

    @classmethod
    def check_no_update(cls, cid, cid_modified):                                                      

        cms_updates = cls.get_or_insert('cms_updates', updates=dict()).updates
        if cid in cms_updates:   # cid modified has dt microseconds
            if cid_modified >= datetime.strptime(cms_updates[cid], '%Y-%m-%d %H:%M:%S'):
                if (datetime.now() - timedelta(days=1)) > cid_modified:
                    del cms_updates[cid]
                    cls(id='cms_updates', updates=cms_updates).put_async()
                return True
            return False         # reload the template
        return True                    

答案 1 :(得分:1)

几周后我一直在寻找解决方案。最后我想出来了,我想为大家分享我的代码。我的代码中有4个python源文件。

TemplateEngine.py,ContentRenderer.py,TestContent.py& Update_Template.py

文件:TemplateEngine.py

注意:

我使用 now = datetime.utcnow()+ timedelta(hours = 8),因为我的时区是GMT + 8

你必须使用ndb.BlobProperty来存储字节码,ndb.TextProperty将不起作用!

from google.appengine.ext import ndb
from datetime import datetime,timedelta

class SiteTemplates(ndb.Model):
  name = ndb.StringProperty(indexed=True, required=True)
  data = ndb.TextProperty()
  uptodate = ndb.BooleanProperty(required=True)

class SiteTemplateBytecodes(ndb.Model):
  key = ndb.StringProperty(indexed=True, required=True)
  data = ndb.BlobProperty(required=True)
  mod_datetime = ndb.DateTimeProperty(required=True)

class LocalCache(jinja2.BytecodeCache):

  def load_bytecode(self, bucket):
    q = SiteTemplateBytecodes.query(SiteTemplateBytecodes.key == bucket.key)
    if q.count() > 0:
      r = q.get()
      bucket.bytecode_from_string(r.data)

  def dump_bytecode(self, bucket):
    now = datetime.utcnow() + timedelta(hours=8)
    q = SiteTemplateBytecodes.query(SiteTemplateBytecodes.key == bucket.key)
    if q.count() > 0:
      r = q.get()
      r.data = bucket.bytecode_to_string()
      r.mod_datetime = now
    else:
      r = SiteTemplateBytecodes(key=bucket.key, data=bucket.bytecode_to_string(), mod_datetime=now)
    r.put()

def Update_Template_Source(tn, source):
  try:
    q = SiteTemplates.query(SiteTemplates.name == tn)
    if q.count() == 0:
      u = mkiniTemplates(name=tn, data=source, uptodate=False)
    else:
      u = q.get()
      u.name=tn
      u.data=source
      u.uptodate=False
    u.put()
    return True
  except Exception,e:
    logging.exception(e)
    return False

def Get_Template_Source(tn):
  uptodate = False

  def Template_Uptodate():
    return uptodate

  try:
    q = SiteTemplates.query(SiteTemplates.name == tn)
    if q.count() > 0:
      r = q.get()
      uptodate = r.uptodate

      if r.uptodate == False:
        r.uptodate=True
        r.put()

      return r.data, tn, Template_Uptodate
    else:
      return None
  except Exception,e:
    logging.exception(e)
    return None

文件:ContentRenderer.py

注意:设置 cache_size = 0 非常重要,否则将禁用字节码缓存功能。我不明白为什么。

from TemplateEngine import Get_Template_Source
import jinja2 

def Render(tn,tags):
  global te
  return te.Render(tn, tags)

bcc = LocalCache()
te = jinja2.Environment(loader=jinja2.FunctionLoader(Get_Template_Source), cache_size=0, extensions=['jinja2.ext.autoescape'], bytecode_cache=bcc)

文件:Update_Template.py

注意:使用 Update_Template_Source()将模板源更新为数据存储区。

from TemplateEngine import Update_Template_Source
template_source = '<html><body>hello word to {{title}}!</body></html>'
if Update_Template_Source('my-template.html', template_source):
  print 'template is updated'
else:
  print 'error when updating template source'

文件:TestContent.py

注意:做一些测试

from ContentRenderer import Render
print Render('my-template.htmnl', {'title':'human'})
'hello world to human!'

您会意识到,即使您的应用程序中有超过20个实例,即使您更新模板,延迟时间也不会增加。模板源将在5到10秒内更新。