[Zope-CVS] CVS: Products/FileCacheManager - FileCache.py:1.8
FileCacheManager.py:1.20
Chris McDonough
chrism at plope.com
Sat Oct 30 21:59:39 EDT 2004
Update of /cvs-repository/Products/FileCacheManager
In directory cvs.zope.org:/tmp/cvs-serv13074
Modified Files:
FileCache.py FileCacheManager.py
Log Message:
Add optional HTTP cache header management.
=== Products/FileCacheManager/FileCache.py 1.7 => 1.8 ===
--- Products/FileCacheManager/FileCache.py:1.7 Sun Aug 29 16:50:29 2004
+++ Products/FileCacheManager/FileCache.py Sat Oct 30 21:59:09 2004
@@ -23,6 +23,7 @@
from thread import allocate_lock
from AccessControl.SecurityManagement import getSecurityManager
+from App.Common import rfc1123_date
from BTrees import OOBTree
from ZPublisher.Iterators import filestream_iterator
from Products.StandardCacheManagers.RAMCacheManager import RAMCache
@@ -35,6 +36,8 @@
class FileCache(RAMCache):
""" A cache that caches rendered content to the filesystem """
_tempfile_path = ''
+ _http_caching_interval = 0
+ _http_caching_anonymous_only = True
def __init__(self, path='/tmp'):
# self.cache maps physical ZODB paths to disk files.
@@ -44,6 +47,8 @@
self._makeDirs()
self._naming_expr = None
self._tempfile_path = ''
+ self._http_caching_interval = 0
+ self._http_caching_anonymous_only = True
self.entries = {}
def _fileName(self, ob):
@@ -83,6 +88,12 @@
def setTempfileDir(self, path=''):
self._tempfile_path = path
+ def setHTTPCachingInterval(self, interval):
+ self._http_caching_interval = interval
+
+ def setHTTPCachingAnonymousOnly(self, flag):
+ self._http_caching_anonymous_only = not not flag
+
def getDir(self):
""" Retrieve the filesystem directory used for caching """
return self._dir
@@ -134,6 +145,8 @@
entry['hits'] += 1
self.entries[pp] = entry
+ self.setHTTPCachingHeaders(ob)
+
return fiter
def ZCache_set( self, ob, data=None, view_name=''
@@ -150,6 +163,34 @@
cache_entry = FileCacheEntry(self, ob, fname, self._tempfile_path)
cache_entry.write(data)
+ self.setHTTPCachingHeaders(ob)
+
+ def setHTTPCachingHeaders(self, ob):
+ if not hasattr(ob, 'REQUEST'):
+ return
+ if not self._http_caching_interval:
+ return
+
+ request = ob.REQUEST
+ response = request.RESPONSE
+
+ u = request.get('AUTHENTICATED_USER', None)
+ if u is not None:
+ if (u.getUserName() != 'Anonymous User' and
+ self._http_caching_anonymous_only):
+ return
+
+ # manage HTTP caching headers
+ if hasattr(ob, '_p_mtime'):
+ last_mod = ob._p_mtime
+ else:
+ last_mod = time.time()
+
+ seconds = self._http_caching_interval
+ response.setHeader('Last-Modified',rfc1123_date(last_mod))
+ response.setHeader('Cache-Control', 'max-age=%d' % seconds)
+ expires=rfc1123_date(time.time() + seconds)
+ response.setHeader('Expires', expires)
def record(self, state):
pp = '/'.join(state.ob.getPhysicalPath())
=== Products/FileCacheManager/FileCacheManager.py 1.19 => 1.20 ===
--- Products/FileCacheManager/FileCacheManager.py:1.19 Sun Aug 29 20:38:49 2004
+++ Products/FileCacheManager/FileCacheManager.py Sat Oct 30 21:59:09 2004
@@ -42,6 +42,8 @@
)
_tempfile_path = ''
+ http_caching_interval = 0
+ http_caching_anonymous_only = True
manage_stats = PageTemplateFile('www/statsFCM', globals())
manage_main = PageTemplateFile('www/propsFCM', globals())
@@ -69,10 +71,14 @@
return cache.values()
def manage_editProps(self, title, settings=None, REQUEST=None):
- 'Changes the cache settings.'
+ 'Changes the cache properties.'
if settings is None:
settings = REQUEST
self.title = str(title)
+ interval = settings.get('http_caching_interval', 0)
+ anonymous_only = settings.get('http_caching_anonymous_only', False)
+ self.setHTTPCachingInterval(interval)
+ self.setHTTPCachingAnonymousOnly(anonymous_only)
if REQUEST is not None:
return self.manage_main(
self, REQUEST, manage_tabs_message='Properties changed.')
@@ -89,6 +95,9 @@
cache.setDir(self._dir)
cache.setTempfileDir(self._tempfile_path)
cache.setNamingExpression(self._naming_expr_text)
+ cache.setHTTPCachingInterval(self.http_caching_interval)
+ cache.setHTTPCachingAnonymousOnly(
+ self.http_caching_anonymous_only)
caches[cacheid] = cache
return cache
@@ -136,13 +145,47 @@
cache.setNamingExpression(naming_expression_text)
self._naming_expr_text = naming_expression_text
+ security.declareProtected(view_management_screens,
+ 'getHTTPCachingAnonymousOnly')
+ def getHTTPCachingAnonymousOnly(self):
+ """ Retrieve flag indicating that only anonymous accesses are
+ http-cached """
+ return self.http_caching_anonymous_only
+
+ security.declareProtected(change_cache_managers,
+ 'setHTTPCachingAnonymousOnly')
+ def setHTTPCachingAnonymousOnly(self, flag):
+ """ Set flag indicating that only anonymous accesses are
+ http-cached """
+ cache = self.ZCacheManager_getCache()
+ cache.setHTTPCachingAnonymousOnly(flag)
+ self.http_caching_anonymous_only = flag
+
+ security.declareProtected(view_management_screens,
+ 'getHTTPCachingInterval')
+ def getHTTPCachingInterval(self):
+ """ Retrieve HTTP caching interval """
+ return self.http_caching_interval
+
+ security.declareProtected(change_cache_managers,
+ 'setHTTPCachingInterval')
+ def setHTTPCachingInterval(self, interval):
+ """ Set the HTTP caching interval """
+ cache = self.ZCacheManager_getCache()
+ cache.setHTTPCachingInterval(interval)
+ self.http_caching_interval = interval
+
security.declareProtected(change_cache_managers, 'manage_edit')
def manage_edit(self, path='/tmp', tpath='',
- naming_expression_text='', REQUEST=None):
+ naming_expression_text='', http_caching_interval=0,
+ http_caching_anonymous_only=True,
+ REQUEST=None):
""" Edit the filesystem-related values """
self.setNamingExpression(naming_expression_text)
self.setDir(path)
self.setTempfileDir(tpath)
+ self.setHTTPCachingInterval(http_caching_interval)
+ self.setHTTPCachingAnonymousOnly(http_caching_anonymous_only)
if REQUEST is not None:
return self.manage_fs(
More information about the Zope-CVS
mailing list