comparison MoinMoin/action/cache.py @ 3892:85c7b3b9c48c

cache action: use dict for metadata, store also original item name and last_modified (as UNIX timestamp)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 20 Jul 2008 17:00:07 +0200
parents c8ffd029ab1f
children fec6c6ccfc0b 78c452cf0257
comparison
equal deleted inserted replaced
3890:a96636a03024 3892:85c7b3b9c48c
105 def put(request, key, data, 105 def put(request, key, data,
106 filename=None, 106 filename=None,
107 content_type=None, 107 content_type=None,
108 content_disposition=None, 108 content_disposition=None,
109 content_length=None, 109 content_length=None,
110 last_modified=None): 110 last_modified=None,
111 original=None):
111 """ 112 """
112 Put an object into the cache to send it with cache action later. 113 Put an object into the cache to send it with cache action later.
113 114
114 @param request: the request object 115 @param request: the request object
115 @param key: non-guessable key into cache (str) 116 @param key: non-guessable key into cache (str)
118 content_type (unicode, default: None) 119 content_type (unicode, default: None)
119 @param content_type: content-type header value (str, default: autodetect from filename) 120 @param content_type: content-type header value (str, default: autodetect from filename)
120 @param content_disposition: type for content-disposition header (str, default: None) 121 @param content_disposition: type for content-disposition header (str, default: None)
121 @param content_length: data length for content-length header (int, default: autodetect) 122 @param content_length: data length for content-length header (int, default: autodetect)
122 @param last_modified: last modified timestamp (int, default: autodetect) 123 @param last_modified: last modified timestamp (int, default: autodetect)
124 @param original: location of original object (default: None) - this is just written to
125 the metadata cache "as is" and could be used for cache cleanup,
126 use (wikiname, itemname, attachname or None))
123 """ 127 """
124 import os.path 128 import os.path
125 from MoinMoin.util import timefuncs 129 from MoinMoin.util import timefuncs
126 130
127 if filename: 131 if filename:
140 data_cache = caching.CacheEntry(request, cache_arena, key+'.data', cache_scope, do_locking=do_locking) 144 data_cache = caching.CacheEntry(request, cache_arena, key+'.data', cache_scope, do_locking=do_locking)
141 data_cache.update(data) 145 data_cache.update(data)
142 content_length = content_length or data_cache.size() 146 content_length = content_length or data_cache.size()
143 last_modified = last_modified or data_cache.mtime() 147 last_modified = last_modified or data_cache.mtime()
144 148
145 last_modified = timefuncs.formathttpdate(int(last_modified)) 149 httpdate_last_modified = timefuncs.formathttpdate(int(last_modified))
146 headers = ['Content-Type: %s' % content_type, 150 headers = ['Content-Type: %s' % content_type,
147 'Last-Modified: %s' % last_modified, 151 'Last-Modified: %s' % httpdate_last_modified,
148 'Content-Length: %s' % content_length, 152 'Content-Length: %s' % content_length,
149 ] 153 ]
150 if content_disposition and filename: 154 if content_disposition and filename:
151 # TODO: fix the encoding here, plain 8 bit is not allowed according to the RFCs 155 # TODO: fix the encoding here, plain 8 bit is not allowed according to the RFCs
152 # There is no solution that is compatible to IE except stripping non-ascii chars 156 # There is no solution that is compatible to IE except stripping non-ascii chars
153 filename = filename.encode(config.charset) 157 filename = filename.encode(config.charset)
154 headers.append('Content-Disposition: %s; filename="%s"' % (content_disposition, filename)) 158 headers.append('Content-Disposition: %s; filename="%s"' % (content_disposition, filename))
155 159
156 meta_cache = caching.CacheEntry(request, cache_arena, key+'.meta', cache_scope, do_locking=do_locking, use_pickle=True) 160 meta_cache = caching.CacheEntry(request, cache_arena, key+'.meta', cache_scope, do_locking=do_locking, use_pickle=True)
157 meta_cache.update((last_modified, headers)) 161 meta_cache.update({
162 'httpdate_last_modified': httpdate_last_modified,
163 'last_modified': last_modified,
164 'headers': headers,
165 'original':original,
166 })
158 167
159 168
160 def exists(request, key, strict=False): 169 def exists(request, key, strict=False):
161 """ 170 """
162 Check if a cached object for this key exists. 171 Check if a cached object for this key exists.
194 203
195 204
196 def _get_headers(request, key): 205 def _get_headers(request, key):
197 """ get last_modified and headers cached for key """ 206 """ get last_modified and headers cached for key """
198 meta_cache = caching.CacheEntry(request, cache_arena, key+'.meta', cache_scope, do_locking=do_locking, use_pickle=True) 207 meta_cache = caching.CacheEntry(request, cache_arena, key+'.meta', cache_scope, do_locking=do_locking, use_pickle=True)
199 last_modified, headers = meta_cache.content() 208 meta = meta_cache.content()
200 return last_modified, headers 209 return meta['httpdate_last_modified'], meta['headers']
201 210
202 211
203 def _get_datafile(request, key): 212 def _get_datafile(request, key):
204 """ get an open data file for the data cached for key """ 213 """ get an open data file for the data cached for key """
205 data_cache = caching.CacheEntry(request, cache_arena, key+'.data', cache_scope, do_locking=do_locking) 214 data_cache = caching.CacheEntry(request, cache_arena, key+'.data', cache_scope, do_locking=do_locking)