Back to index

plone3  3.1.7
cachingadapter.py
Go to the documentation of this file.
00001 from time import time
00002 from interfaces import ICachedResource
00003 from zope.interface import implements
00004 
00005 try:
00006     from zope.datetime import rfc1123_date
00007 except ImportError:
00008     # Zope < 2.10
00009     from zope.app.datetimeutils import rfc1123_date
00010 
00011 
00012 class CachedResource(object):
00013     'Adapts a ContextFile to a cached resource'
00014     implements(ICachedResource)
00015 
00016     def __init__(self, context):
00017         self.context = context
00018         self.lmt_last_checked = 0
00019         self.data_last_fetched = 0
00020     
00021     def _fetchdata(self):
00022         try:
00023             result = self._contents
00024             ##print "*****Resource from cached"
00025         except AttributeError:
00026             result = self._contents = self.context.getContents()
00027             self.data_last_fetched = time()
00028             ##print "*****Resource kooked"
00029         return result
00030 
00031     def _deldata(self):
00032         del self._contents
00033 
00034     def purgeData(self):
00035         'Force file contents to be reloaded'
00036         ##print "***** PURGE", self.context.__name__
00037         try:
00038             self._deldata()
00039         except AttributeError:
00040             pass
00041 
00042     # Once fetched, data is cached in the object until
00043     # explicitely deleted.
00044     data = property(lambda self: self._fetchdata()['data'], None, _deldata)
00045     content_type = property(lambda self: self._fetchdata()['content_type'], None, _deldata)
00046 
00047     # Last modified time is calculated on demand
00048     # but never more often then lmt_check_period
00049     def _fetchlm(self):
00050         now = time()
00051         if now - self.lmt_last_checked > self.lmt_check_period:
00052             self.lmt_last_checked = now
00053             lmt = float(self.context.getLastMod()) or now
00054             lmh = rfc1123_date(lmt)
00055             d = self._last_mod = dict(lmt = lmt, lmh = lmh)
00056             ##print "***** LMT reread", d
00057         else:
00058             d = self._last_mod
00059         return d
00060             
00061     lmt = property(lambda self: self._fetchlm()['lmt'])
00062     lmh = property(lambda self: self._fetchlm()['lmh'])
00063 
00064     caching = property(lambda self: self.context.caching)
00065     lmt_check_period = property(lambda self: self.context.lmt_check_period)