AutoMatic cache for Modeling FrameWork in WebWare

This is my first hack over ZPTPage and Modeling to enable regerating of ZPTPage only when Modeling objects changed .. check out class A: (which is a sample fake Modeling Object)

import os,time

from ZPTPage import ZPTPage

class A:
    def __init__(self):
        self.a = 'B' * 50
        self.willChange()

    def getA(self):
        self.willRead()
        return self.a

    def setA(self,value):
        self.a=value
        self.willChange()

    def willRead(self):
        #print "Read %s" % self
        pass

    def willChange(self):
        self.lastChange = time.time()

class HereProxy(dict):
     def __init__(self,orig):
        print "New Here"
        self.orig = orig

     def __getitem__(self,key):
         result=getattr(self.orig,key)
         print "*** %-10s : %s " % ( key,result)
         cache = self.orig.application().cache[self.orig.hashCache()]

         if result not in cache:
             cache.update( {result:result.lastChange })
         return result

class Main(ZPTPage):
    def __init__(self):
        self.a = A()
        ZPTPage.__init__(self)
        self.here = HereProxy(self)
        self.validCache = False

    def writeHTML(self):
        if self.request().hasField('A'):
            self.a.setA(self.request().value('A') )

        cachedFileName=os.path.join('/tmp/w/',self.hashCache() +'.html')
        try:
            cache = self.application().cache[self.hashCache()]
        except KeyError:
            self.application().cache[self.hashCache()] = {}
            cache = self.application().cache[self.hashCache()]

        needUpdate = 0

        #import pdb;pdb.set_trace()
        for obj in cache.keys():
            try:
                print obj.lastChange
                if obj.lastChange !=  cache[obj]:
                    needUpdate = 1
                    break
            except AttributeError:
                needUpdate = 1
                break

        if needUpdate == 0:
            print "*** Serving cache %s ***"  % self.hashCache()
            try:
                data = open(cachedFileName,'r').read()
            except IOError:
                needUpdate = 1

        if needUpdate:
            print "*** Regerating %s ***"  % self.hashCache()
            self.buildContext()
            data = self.template.render()
            open(cachedFileName,'w').write(data)
        self.response().setHeader('Content-type', 'text/html; charset=UTF-8')
        self.writeln(data)

    def buildContext(self):
        self.updateContext('text','Toto')

    def getTime(self):
        import time
        return time.time()

    def getHere(self):
        #return {'a':A()}
        return self.here

    def sleep(self,context):
        print self.application().cache
        ZPTPage.sleep(self,context)

    ### cache ###
    def hashCache(self):
        return "MainTemplate"


Related Posts

2 thoughts on “AutoMatic cache for Modeling FrameWork in WebWare

  1.       Hey soif,

      You’d better not forget to call CustomObject.willChange() when overriding willChange() in real mdl-objects ;)

  2. Yes you’re right . but again this is a fake Modeling object :) It seems to work fine but i need again to look at this closely since i don’t know if import statement in ZPT don’t use getHere() too, and that should be a problem right now

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>