I've had this bookmarked and have been looking over it recently. I
added a c_download (cached download) function as described above to
allow local caching of files. The above code did not get me there
though I ended up using:
def c_download():
controller=request.vars.c
file=request.vars.f
response.headers['Cache-Control']='private'
del response.headers['Content-Type']
del response.headers['Pragma']
del response.headers['Expires']
filename = os.path.join(request.folder,'static',controller,file)
response.headers['Last-Modified'] = time.strftime("%a, %d %b %Y %H:
%M:%S +0000", time.localtime(os.path.getmtime(filename)))
return response.stream(open(filename,'rb'))
The key difference being I found I had to set the 'Cache-Control'
header, just deleting it didn't do the trick.
What I'm not clear on is why this is necessary. From the book:
When static files are downloaded, web2py does not create a session,
nor does it issue a cookie or execute the models. web2py always
streams static files in chunks of 1MB, and sends PARTIAL CONTENT when
the client sends a RANGE request for a subset of the file. web2py
also supports the IF_MODIFIED_SINCE protocol, and does not send the
file if it is already stored in the browser's cache and if the file
has not changed since that version.
Link:
http://web2py.com/book/default/section/4/2?search=supports+the+IF_MODIFIED_SINCE+protocol%2C+and+does+not+send+the+file+if+it+is+already+stored+in+the+browser%27s+cache+and+if+the+file+has+not+changed+since+that+version.
So then, if I serve a style.css file from static, or build a link from
URL() to a file in static. Why do these files get downloaded every
time the page is loaded?
Here's an example. Using
http://127.0.0.1:8080/welcome/static/menu.gif
running on the GAE development server I get:
Header:
HTTP/1.0 200
Server: Development/1.0
Date: Wed, 30 Jun 2010 18:37:05 GMT
Content-Type: image/gif
Cache-Control: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Content-Length: 264
Cache:
Last Modified Wed Jun 30 2010 13:37:06 GMT-0500 (Central Daylight
Time)
Last Fetched Wed Jun 30 2010 13:37:06 GMT-0500 (Central Daylight Time)
Expires Wed Dec 31 1969 18:00:00 GMT-0600 (Central Standard Time)
Data Size 264
Fetch Count 7
Device disk
Is this working as intended? I *can* wrap every single download in a
function call to c_download, but should that be necessary? Am I just
missing a configuration option somewhere? I feel like I'm re-
inventing the wheel since 'static' files were in my understanding not
meant to change often anyway.
On May 6, 8:15 am, mdipierro <
mdipie...@cs.depaul.edu> wrote:
> Can you provide an example of code that causes cache failure?
> Remember that you cannot @cache def download because of range
> requests.
>
> On May 6, 2:49 am, Iceberg <
iceb...@21cn.com> wrote:
>
> > It seems Mariano's story has a happy ending. Congratulations. But on a
> > second thought, can anyone explain why "if you quickly reload pages,
> > they fail" in the very first caching-download version? Caching
> > download can improve speed, can with a side effect of bypassing
> > priviledge check, but no matter what, it shall not cause content fail
> > to load.
>
> > I remember I once tried @cache(...) but encounter similar problems,
> > then I give up. :-( Nice to pick it up if someone can throw some
> > light. Thanks!
>
> > Regards,
> > iceberg
>
> > On May5, 11:39am, Mariano Reingart <
reing...@gmail.com> wrote:
>
> > > ...... after usingfast_download(changing headers and using
> > > >>> far,fast_downloadis working fine for me now :-)