配置自动重新加载模板,并在附录中启用jinja2的字节码缓存



如何在附录中配置jinja2 to:

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

我使用App Engine Memcache Client()::

添加了BCC
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

模板模型使用模板。修改: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                    

我正在寻找解决方案几周。最后,我弄清楚了,我想为每个人分享我的代码。我的代码中有4个Python源文件。

TemplateEngine.py,contentRenderer.py,testcontent.py&update_template.py

文件:TemplateEngine.py

注意:

i使用 NOW = DateTime.utcnow() TimeDelta(小时= 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 非常重要,否则bytecode缓存函数将被禁用。我不知道为什么。

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秒内更新。

最新更新