annotate MoinMoin/action/sendcached.py @ 4235:a6c315ff8d66

Make more use of werkzeugs Href object for URL-generation in MoinMoin
author Florian Krupicka <florian.krupicka@googlemail.com>
date Sun, 20 Jul 2008 18:08:13 +0200
parents 1ca179133f01
children
rev   line source
3785
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
2 """
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
3 MoinMoin - Send a raw object from the caching system
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
4
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
5 This can be used e.g. for all image generating extensions:
3798
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
6 E.g. a thumbnail generating extension just uses sendcached.put_cache to
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
7 write the thumbnails into the cache and emits <img src="sendcached_url">
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
8 to display them. sendcached_url is returned by put_cache or get_url.
3785
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
9
3798
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
10 IMPORTANT: use some non-guessable key derived from your source content.
3785
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
11
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
12 TODO:
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
13 * add error handling
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
14 * maybe use page local caching, not global:
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
15 + smaller directories
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
16 - but harder to clean
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
17 - harder to backup data_dir
3798
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
18 * move file-like code to caching module
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
19 * add auto-key generation?
3785
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
20
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
21 @copyright: 2008 MoinMoin:ThomasWaldmann
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
22 @license: GNU GPL, see COPYING for details.
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
23 """
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
24
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
25 from MoinMoin import log
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
26 logging = log.getLogger(__name__)
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
27
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
28 from MoinMoin import config, caching
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
29
3798
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
30 # keep both imports below as they are, order is important:
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
31 from MoinMoin import wikiutil
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
32 import mimetypes
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
33
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
34 action_name = 'sendcached'
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
35
3785
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
36 # Do NOT get this directly from request.form or user would be able to read any cache!
3798
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
37 sendcached_arena = action_name
3785
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
38 sendcached_scope = 'wiki'
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
39 do_locking = False
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
40
3798
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
41 def put_cache(request, key, data,
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
42 filename=None,
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
43 content_type=None,
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
44 content_disposition=None,
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
45 content_length=None,
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
46 last_modified=None,
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
47 bufsize=8192):
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
48 """
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
49 Cache an object to send with sendcached action later.
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
50
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
51 @param request: the request object
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
52 @param key: non-guessable key into sendcached cache (str)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
53 @param data: content data (str or open file-like obj)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
54 @param filename: filename for content-disposition header and for autodetecting
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
55 content_type (unicode, default: None)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
56 @param content_disposition: type for content-disposition header (str, default: None)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
57 @param content_type: content-type header value (str, default: autodetect from filename)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
58 @param last_modified: last modified timestamp (int, default: autodetect)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
59 @param content_length: data length for content-length header (int, default: autodetect)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
60 @return: URL of cached object
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
61 """
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
62 import os.path
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
63 from MoinMoin.util import timefuncs
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
64
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
65 if filename:
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
66 # make sure we just have a simple filename (without path)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
67 filename = os.path.basename(filename)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
68
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
69 if content_type is None:
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
70 # try autodetect
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
71 mt, enc = mimetypes.guess_type(filename)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
72 if mt:
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
73 content_type = mt
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
74
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
75 if content_type is None:
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
76 content_type = 'application/octet-stream'
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
77
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
78 data_cache = caching.CacheEntry(request, sendcached_arena, key+'.data',
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
79 sendcached_scope, do_locking=do_locking)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
80 data_cache_fname = data_cache._filename()
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
81
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
82 if hasattr(data, 'read'):
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
83 import shutil
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
84 data_cache_file = open(data_cache_fname, 'wb')
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
85 shutil.copyfileobj(data, data_cache_file)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
86 data_cache_file.close()
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
87 else:
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
88 data_cache.update(data)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
89
3799
738bd5aec961 sendcached: minor bugfix, added unit tests
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3798
diff changeset
90 content_length = content_length or os.path.getsize(data_cache_fname)
3798
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
91 last_modified = last_modified or os.path.getmtime(data_cache_fname)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
92
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
93 last_modified = timefuncs.formathttpdate(int(last_modified))
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
94 headers = ['Content-Type: %s' % content_type,
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
95 'Last-Modified: %s' % last_modified,
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
96 'Content-Length: %s' % content_length,
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
97 ]
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
98 if content_disposition and filename:
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
99 # TODO: fix the encoding here, plain 8 bit is not allowed according to the RFCs
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
100 # There is no solution that is compatible to IE except stripping non-ascii chars
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
101 filename = filename.encode(config.charset)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
102
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
103 headers.append(
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
104 'Content-Disposition: %s; filename="%s"' % (content_disposition, filename)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
105 )
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
106
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
107 meta_cache = caching.CacheEntry(request, sendcached_arena, key+'.meta',
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
108 sendcached_scope, do_locking=do_locking, use_pickle=True)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
109 meta_cache.update((last_modified, headers))
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
110
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
111 return get_url(request, key)
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
112
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
113
3801
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
114 def is_cached(request, key, strict=False):
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
115 """
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
116 Check if we have already cached an object for this key.
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
117
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
118 @param request: the request object
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
119 @param key: non-guessable key into sendcached cache (str)
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
120 @param strict: if True, also check the data cache, not only meta (bool, default: False)
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
121 @return: is object cached? (bool)
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
122 """
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
123 if strict:
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
124 data_cache = caching.CacheEntry(request, sendcached_arena, key+'.data',
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
125 sendcached_scope, do_locking=do_locking)
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
126 data_cached = data_cache.exists()
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
127 else:
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
128 data_cached = True # we assume data will be there if meta is there
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
129
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
130 meta_cache = caching.CacheEntry(request, sendcached_arena, key+'.meta',
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
131 sendcached_scope, do_locking=do_locking, use_pickle=True)
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
132 meta_cached = meta_cache.exists()
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
133
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
134 return meta_cached and data_cached
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
135
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
136
3798
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
137 def get_url(request, key):
3800
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
138 """ get URL for the object cached for key """
4235
a6c315ff8d66 Make more use of werkzeugs Href object for URL-generation in MoinMoin
Florian Krupicka <florian.krupicka@googlemail.com>
parents: 4212
diff changeset
139 return request.href(action=action_name, key=key)
3798
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
140
3800
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
141 def get_cache_headers(request, key):
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
142 """ get last_modified and headers cached for key """
3785
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
143 meta_cache = caching.CacheEntry(request, sendcached_arena, key+'.meta',
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
144 sendcached_scope, do_locking=do_locking, use_pickle=True)
3798
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
145 last_modified, headers = meta_cache.content()
3800
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
146 return last_modified, headers
3785
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
147
3800
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
148
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
149 def get_cache_datafile(request, key):
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
150 """ get an open data file for the data cached for key """
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
151 data_cache = caching.CacheEntry(request, sendcached_arena, key+'.data',
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
152 sendcached_scope, do_locking=do_locking)
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
153 data_file = open(data_cache._filename(), 'rb')
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
154 return data_file
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
155
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
156
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
157 def send_cached(request, key):
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
158 """ send a complete http response with headers/data cached for key """
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
159 last_modified, headers = get_cache_headers(request, key)
3785
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
160 if request.if_modified_since == last_modified:
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
161 request.emit_http_headers(["Status: 304 Not modified"])
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
162 else:
3798
17e94abc5320 sendcached: added a put_cache and get_url function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3785
diff changeset
163 request.emit_http_headers(headers)
3800
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
164 request.send_file(get_cache_datafile(request, key))
3785
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
165
651f505bd9f1 sendcached action (completely untested)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
166
3800
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
167 def execute(pagename, request):
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
168 key = request.form.get('key', [None])[0]
3801
7eb4a14108de sendcached: add missing request param, add is_cached() function
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3800
diff changeset
169 send_cached(request, key)
3800
07ea9c357b2a sendcached: made some functions for usage from other modules, added comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3799
diff changeset
170