|
|
@@ -8,7 +8,7 @@ from hashlib import md5
|
|
|
from copy import copy
|
|
|
|
|
|
|
|
|
-ADMINISTRATION_FILE = 'administration.py'
|
|
|
+ADMINISTRATION_FILE = 'administration.pickle'
|
|
|
|
|
|
|
|
|
def to_dir(path):
|
|
|
@@ -34,7 +34,7 @@ class Cache:
|
|
|
a concatenation of the file list.
|
|
|
"""
|
|
|
|
|
|
- def __init__(self, root='', files=[], cached='cached',
|
|
|
+ def __init__(self, root='', files=[], cached='cached/',
|
|
|
expires={'days': 365}):
|
|
|
"""
|
|
|
The constructor takes the following arguments (all are optional):
|
|
|
@@ -45,19 +45,24 @@ class Cache:
|
|
|
4. A dictionary containing arguments to the timedelta constructor, that
|
|
|
indicates how long the cache object sould live.
|
|
|
"""
|
|
|
- self.root = to_dir(root + '/')
|
|
|
- self.cached = to_dir(cached + '/')
|
|
|
- self.files = map(lambda f: self.root + f, files)
|
|
|
+ self.root = to_dir(root)
|
|
|
+ self.cached = to_dir(cached)
|
|
|
+ self.files = []
|
|
|
self.expires = expires
|
|
|
|
|
|
+ for f in files:
|
|
|
+ self.add(f)
|
|
|
+
|
|
|
+ web.debug(str(self))
|
|
|
+
|
|
|
self.assert_files_exist()
|
|
|
|
|
|
def assert_files_exist(self):
|
|
|
map(assert_file_exists, self.files)
|
|
|
|
|
|
def __str__(self):
|
|
|
- return '<Cache filename=%s files=[%s]>' \
|
|
|
- % (self.filename(), ','.join(self.files))
|
|
|
+ return '<Cache root=%s filename=%s files=[%s]>' \
|
|
|
+ % (self.root, self.filename(), ','.join(self.files))
|
|
|
|
|
|
def add(self, path, absolute=False):
|
|
|
"""
|
|
|
@@ -68,6 +73,7 @@ class Cache:
|
|
|
if not absolute:
|
|
|
path = self.root + path
|
|
|
|
|
|
+ web.debug('Adding file "%s"...' % path)
|
|
|
assert_file_exists(path)
|
|
|
self.files.append(path)
|
|
|
|
|
|
@@ -169,7 +175,8 @@ class Cache:
|
|
|
admin = self.load_administration()
|
|
|
|
|
|
if not exists(path):
|
|
|
- web.debug('Cached file "%s" does not exist yet, generating it...')
|
|
|
+ web.debug('Cached file "%s" does not exist yet, generating it...' \
|
|
|
+ % path)
|
|
|
server_modified = True
|
|
|
else:
|
|
|
server_modified = False
|
|
|
@@ -191,14 +198,19 @@ class Cache:
|
|
|
f.close()
|
|
|
|
|
|
try:
|
|
|
- web.http.modified(seconds_to_datetime(last_modified), self.etag())
|
|
|
- web.http.expires(timedelta(**self.expires))
|
|
|
+ # Set headers
|
|
|
web.header('Cache-Control', 'private')
|
|
|
|
|
|
+ if not server_modified:
|
|
|
+ web.http.modified(seconds_to_datetime(last_modified),
|
|
|
+ self.etag())
|
|
|
+
|
|
|
+ web.http.expires(timedelta(**self.expires))
|
|
|
+
|
|
|
if not server_modified:
|
|
|
# Concatenated content has not been loaded yet, read the cached
|
|
|
# file
|
|
|
- web.debug('Cached file "%s" already exists, sending content...')
|
|
|
+ web.debug('Cached file "%s" already exists...' % path)
|
|
|
f = open(path, 'r')
|
|
|
content = f.read()
|
|
|
f.close()
|