[web2py] Re: Is there a way to cache images from "download" function?

2020-06-25 Thread Andrew Rogers

Hi
I tried this from the book, but got  an error:
@cache.action(time_expire=874, cache_model=cache.ram, session=True, vars=True, 
public=True)

So i tried the ideas in this post and they had miscellaneous errors. I 
thought  things have probably changed since 2014. 

Then i started removing parameters from the book's suggestion and found 
that if I didn't include 'cache_model=cache.ram' it worked and set the 
expire time as i expected. So i am happy.

But I'm not sure if i should be happy because i know so little about 
caching that maybe leaving that parameter out is bad. Anyhow, thought this 
might be helpful to someone else who knows as little about this as I do :)
On Wednesday, 29 January 2014 at 18:20:21 UTC+10 Niphlod wrote:

> server-side it doesn't make any sense, so there's no native way to do it. 
> It's not going to be faster than serving a static-file.
>
>
> On Wednesday, January 29, 2014 12:53:00 AM UTC+1, James Burke wrote:
>>
>> I'm exploring options. 
>>
>> I want to be able to cache my images. Client side, server side it doesn't 
>> matter.  
>>
>> What is the best method to go about this?
>>
>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/web2py/2092b4d5-b7be-48ce-b44a-522895b299f9n%40googlegroups.com.


[web2py] Re: Is there a way to cache images from download function?

2014-01-29 Thread Niphlod
server-side it doesn't make any sense, so there's no native way to do it. 
It's not going to be faster than serving a static-file.

On Wednesday, January 29, 2014 12:53:00 AM UTC+1, James Burke wrote:

 I'm exploring options. 

 I want to be able to cache my images. Client side, server side it doesn't 
 matter.  

 What is the best method to go about this?



-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Is there a way to cache images from download function?

2014-01-28 Thread Niphlod
lots and lots of time passed under the bridge. Now there's cache.action 
...




-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Is there a way to cache images from download function?

2014-01-28 Thread James Burke
Ok I tried:

@cache.action(time_expire=1200, cache_model=cache.memcache, quick='SVL')
def fast_download():
import time, os
import contenttype as c


# very basic security:
if not request.args(0).startswith(file.file):
return download()

file_id = request.args(-1)
myfile = db.file(db.file.file==file_id)


filename, file = db.file.file.retrieve(myfile.file)
response.headers[Content-Type] = c.contenttype(file_id)
response.headers[Content-Disposition] = attachment; filename=%s 
%filename
#response.headers['Last-Modified'] = time.strftime(%a, %d %b %Y 
%H:%M:%S +, time.localtime(myfile.last_modified))
stream = response.stream(file, chunk_size=64*1024, request=request)
raise HTTP(200, stream, **response.headers)

Now I'm getting an error:

PicklingError: Can't pickle type 'generator': attribute lookup 
__builtin__.generator failed

Is there a way to response.render the download?


On Wednesday, January 29, 2014 2:30:37 AM UTC+13, Niphlod wrote:

 lots and lots of time passed under the bridge. Now there's cache.action 
 ...




-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Is there a way to cache images from download function?

2014-01-28 Thread Niphlod
wasn't the original point just to add the proper headers ?
Of course you can't cache a generator (that is what response.stream 
returns) 
It makes no sense at all to store the actual contents of the file on 
memcache

On Tuesday, January 28, 2014 7:46:18 PM UTC+1, James Burke wrote:

 Ok I tried:

 @cache.action(time_expire=1200, cache_model=cache.memcache, quick='SVL')
 def fast_download():
 import time, os
 import contenttype as c


 # very basic security:
 if not request.args(0).startswith(file.file):
 return download()
 
 file_id = request.args(-1)
 myfile = db.file(db.file.file==file_id)


 filename, file = db.file.file.retrieve(myfile.file)
 response.headers[Content-Type] = c.contenttype(file_id)
 response.headers[Content-Disposition] = attachment; filename=%s 
 %filename
 #response.headers['Last-Modified'] = time.strftime(%a, %d %b %Y 
 %H:%M:%S +, time.localtime(myfile.last_modified))
 stream = response.stream(file, chunk_size=64*1024, request=request)
 raise HTTP(200, stream, **response.headers)

 Now I'm getting an error:

 PicklingError: Can't pickle type 'generator': attribute lookup 
 __builtin__.generator failed

 Is there a way to response.render the download?


 On Wednesday, January 29, 2014 2:30:37 AM UTC+13, Niphlod wrote:

 lots and lots of time passed under the bridge. Now there's cache.action 
 ...




-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Is there a way to cache images from download function?

2014-01-28 Thread James Burke
I'm exploring options. 

I want to be able to cache my images. Client side, server side it doesn't 
matter.  

What is the best method to go about this?

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Is there a way to cache images from download function?

2014-01-27 Thread Niphlod
probably you're not running a recent web2py release.

On Saturday, January 25, 2014 9:37:12 PM UTC+1, James Burke wrote:

 def fast_download():
 import time, os
 import contenttype as c
 
 cache.client(time_expire=604800, quick='SVL')(lambda: 0)()

 file_id = request.args(-1)
 myfile = db.file(db.file.file==file_id)


 filename, file = db.file.file.retrieve(myfile.file)
 response.headers[Content-Type] = c.contenttype(file_id)
 response.headers[Content-Disposition] = attachment; filename=%s 
 %filename


 stream = response.stream(file, chunk_size=64*1024, request=request)
 raise HTTP(200, stream, **response.headers)


 I tried using the above code, but results in the follow error message:

 cache.client(time_expire=604800, quick='SVL')(lambda: 0)()

 AttributeError: 'Cache' object has no attribute 'client'


 Am I missing something?

 Cheers

 -James

 On Monday, April 15, 2013 1:21:18 AM UTC+12, Niphlod wrote:

 ok. So, basically the problem is that response.stream is a special kind 
 of function.
 It raises HTTP(200, content_of_the_file) instead of returning it, and 
 raising an HTTP(200) is a smart way to do it.
 Unfortunately, this means that
 def download():
   return response.stream()

 basically doesn't return from download, it raises an exception inside 
 response.stream and the execution is cutted of right in the response.stream 
 function.

 A decorator outside download() doesn't work, because it doesn't have 
 the chance to execute that function completely.
 Now, on the bright side, the download() function should be the only one 
 behaving in this way, so the cache.client implementation shouldn't change.

 I'll see if we can use a public function just to adjust headers 
 beforehand without requiring for the actual function.
 For the time being, this works ok.

  def download():
 cache.client(time_expire=604800, quick='SVL')(lambda: 0)()
 
 allows downloading of uploaded files
 http:///[app]/default/download/[filename]
 
 return response.download(request, db)

 basically because cache.client is coded to be a decorator, you have to 
 pass it a function.
 In this case, a dummy lambda:0 is passed. To fire the actual 
 calculations of the cache decorator, you have to call it (and that's why 
 there's an empty () at the end). The headers are then manipulated in the 
 current response, so response.download pick it up where headers are already 
 set, and when it returns the image, the headers are shipped with the 
 response.

 If you have any doubts, please ask.



-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Is there a way to cache images from download function?

2014-01-27 Thread James Burke
Version 2.7.4-stable+timestamp.2013.10.14.15.16.29 recent enough?

It's after your original post.

On Tuesday, January 28, 2014 2:49:52 AM UTC+13, Niphlod wrote:

 probably you're not running a recent web2py release.

 On Saturday, January 25, 2014 9:37:12 PM UTC+1, James Burke wrote:

 def fast_download():
 import time, os
 import contenttype as c
 
 cache.client(time_expire=604800, quick='SVL')(lambda: 0)()

 file_id = request.args(-1)
 myfile = db.file(db.file.file==file_id)


 filename, file = db.file.file.retrieve(myfile.file)
 response.headers[Content-Type] = c.contenttype(file_id)
 response.headers[Content-Disposition] = attachment; filename=%s 
 %filename


 stream = response.stream(file, chunk_size=64*1024, request=request)
 raise HTTP(200, stream, **response.headers)


 I tried using the above code, but results in the follow error message:

 cache.client(time_expire=604800, quick='SVL')(lambda: 0)()

 AttributeError: 'Cache' object has no attribute 'client'


 Am I missing something?

 Cheers

 -James

 On Monday, April 15, 2013 1:21:18 AM UTC+12, Niphlod wrote:

 ok. So, basically the problem is that response.stream is a special 
 kind of function.
 It raises HTTP(200, content_of_the_file) instead of returning it, and 
 raising an HTTP(200) is a smart way to do it.
 Unfortunately, this means that
 def download():
   return response.stream()

 basically doesn't return from download, it raises an exception inside 
 response.stream and the execution is cutted of right in the response.stream 
 function.

 A decorator outside download() doesn't work, because it doesn't have 
 the chance to execute that function completely.
 Now, on the bright side, the download() function should be the only one 
 behaving in this way, so the cache.client implementation shouldn't change.

 I'll see if we can use a public function just to adjust headers 
 beforehand without requiring for the actual function.
 For the time being, this works ok.

  def download():
 cache.client(time_expire=604800, quick='SVL')(lambda: 0)()
 
 allows downloading of uploaded files
 http:///[app]/default/download/[filename]
 
 return response.download(request, db)

 basically because cache.client is coded to be a decorator, you have to 
 pass it a function.
 In this case, a dummy lambda:0 is passed. To fire the actual 
 calculations of the cache decorator, you have to call it (and that's why 
 there's an empty () at the end). The headers are then manipulated in the 
 current response, so response.download pick it up where headers are already 
 set, and when it returns the image, the headers are shipped with the 
 response.

 If you have any doubts, please ask.



-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Is there a way to cache images from download function?

2014-01-25 Thread James Burke
I tried using the above code, but results in the follow error message:

cache.client(time_expire=604800, quick='SVL')(lambda: 0)()

AttributeError: 'Cache' object has no attribute 'client'


Am I missing something?

Cheers

-James

On Monday, April 15, 2013 1:21:18 AM UTC+12, Niphlod wrote:

 ok. So, basically the problem is that response.stream is a special kind 
 of function.
 It raises HTTP(200, content_of_the_file) instead of returning it, and 
 raising an HTTP(200) is a smart way to do it.
 Unfortunately, this means that
 def download():
   return response.stream()

 basically doesn't return from download, it raises an exception inside 
 response.stream and the execution is cutted of right in the response.stream 
 function.

 A decorator outside download() doesn't work, because it doesn't have the 
 chance to execute that function completely.
 Now, on the bright side, the download() function should be the only one 
 behaving in this way, so the cache.client implementation shouldn't change.

 I'll see if we can use a public function just to adjust headers 
 beforehand without requiring for the actual function.
 For the time being, this works ok.

  def download():
 cache.client(time_expire=604800, quick='SVL')(lambda: 0)()
 
 allows downloading of uploaded files
 http:///[app]/default/download/[filename]
 
 return response.download(request, db)

 basically because cache.client is coded to be a decorator, you have to 
 pass it a function.
 In this case, a dummy lambda:0 is passed. To fire the actual 
 calculations of the cache decorator, you have to call it (and that's why 
 there's an empty () at the end). The headers are then manipulated in the 
 current response, so response.download pick it up where headers are already 
 set, and when it returns the image, the headers are shipped with the 
 response.

 If you have any doubts, please ask.



-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


[web2py] Re: Is there a way to cache images from download function?

2013-04-14 Thread BlueShadow
So I changed my function to this:

#7days
@cache.client(time_expire=604800, quick='SVL')
def fast_download():
   session.forget(response)
   # very basic security (only allow fast_download on 
your_table.upload_field):
   if not request.args(0).startswith(db.Images):
   return download()
   # remove/add headers that prevent/favors client-side caching
   #del response.headers['Cache-Control']
   #del response.headers['Pragma']
   #del response.headers['Expires']
   filename = os.path.join(request.folder,'uploads',request.args(0))
   #response.headers['Last-Modified'] = time.strftime(%a, %d %b %Y 
%H:%M:%S +, time.localtime(os.path.getmtime(filename)))
   return response.stream(open(filename,'rb'))


developers.google.com/speed/pagespeed/
still says I should enable caching for those pictures.
any idea why its not working?

-- 

--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[web2py] Re: Is there a way to cache images from download function?

2013-04-14 Thread Niphlod
sorry for the misguidance  unfortunately response.stream doesn't return 
, it raises HTTP() so basically the cache decorator hasn't the possibility 
of completing. I'm looking into it right now.

-- 

--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[web2py] Re: Is there a way to cache images from download function?

2013-04-14 Thread Niphlod
ok. So, basically the problem is that response.stream is a special kind 
of function.
It raises HTTP(200, content_of_the_file) instead of returning it, and 
raising an HTTP(200) is a smart way to do it.
Unfortunately, this means that
def download():
  return response.stream()

basically doesn't return from download, it raises an exception inside 
response.stream and the execution is cutted of right in the response.stream 
function.

A decorator outside download() doesn't work, because it doesn't have the 
chance to execute that function completely.
Now, on the bright side, the download() function should be the only one 
behaving in this way, so the cache.client implementation shouldn't change.

I'll see if we can use a public function just to adjust headers 
beforehand without requiring for the actual function.
For the time being, this works ok.

 def download():
cache.client(time_expire=604800, quick='SVL')(lambda: 0)()

allows downloading of uploaded files
http:///[app]/default/download/[filename]

return response.download(request, db)

basically because cache.client is coded to be a decorator, you have to pass 
it a function.
In this case, a dummy lambda:0 is passed. To fire the actual 
calculations of the cache decorator, you have to call it (and that's why 
there's an empty () at the end). The headers are then manipulated in the 
current response, so response.download pick it up where headers are already 
set, and when it returns the image, the headers are shipped with the 
response.

If you have any doubts, please ask.

-- 

--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[web2py] Re: Is there a way to cache images from download function?

2013-04-14 Thread BlueShadow
It works like a charm. I used the stream function and that works too.
#7days
def fast_download():
   session.forget(response)
   cache.client(time_expire=604800)(lambda: 0)()
   # very basic security (only allow fast_download on 
your_table.upload_field):
   if not request.args(0).startswith(db.Images):
   return download()
   filename = os.path.join(request.folder,'uploads',request.args(0))
   return response.stream(open(filename,'rb'))

thanks Niphlod you are awesome.

On Sunday, April 14, 2013 3:21:18 PM UTC+2, Niphlod wrote:

 ok. So, basically the problem is that response.stream is a special kind 
 of function.
 It raises HTTP(200, content_of_the_file) instead of returning it, and 
 raising an HTTP(200) is a smart way to do it.
 Unfortunately, this means that
 def download():
   return response.stream()

 basically doesn't return from download, it raises an exception inside 
 response.stream and the execution is cutted of right in the response.stream 
 function.

 A decorator outside download() doesn't work, because it doesn't have the 
 chance to execute that function completely.
 Now, on the bright side, the download() function should be the only one 
 behaving in this way, so the cache.client implementation shouldn't change.

 I'll see if we can use a public function just to adjust headers 
 beforehand without requiring for the actual function.
 For the time being, this works ok.

  def download():
 cache.client(time_expire=604800, quick='SVL')(lambda: 0)()
 
 allows downloading of uploaded files
 http:///[app]/default/download/[filename]
 
 return response.download(request, db)

 basically because cache.client is coded to be a decorator, you have to 
 pass it a function.
 In this case, a dummy lambda:0 is passed. To fire the actual 
 calculations of the cache decorator, you have to call it (and that's why 
 there's an empty () at the end). The headers are then manipulated in the 
 current response, so response.download pick it up where headers are already 
 set, and when it returns the image, the headers are shipped with the 
 response.

 If you have any doubts, please ask.



-- 

--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[web2py] Re: Is there a way to cache images from download function?

2013-04-06 Thread BlueShadow
There supposedly is a way the fast download function(web2pyslices)
def fast_download():
   # very basic security (only allow fast_download on 
your_table.upload_field):
   if not request.args(0).startswith(db.your_table.your_field):
   return download()
   # remove/add headers that prevent/favors client-side caching
   #7days
   response.headers['Cache-Control'] = max-age=604800
   del response.headers['Pragma']
   del response.headers['Expires']
   filename = os.path.join(request.folder,'uploads',request.args(0))
   # send last modified date/time so client browser can enable client-side 
caching
   response.headers['Last-Modified'] = time.strftime(%a, %d %b %Y %H:%M:%S 
+, time.localtime(os.path.getmtime(filename)))
   
   return response.stream(open(filename,'rb'))

this is the version I'm experimenting with. the original had a del Cache 
Control instead of the max age. but google pagespeed still tells me that no 
expiration date is set.


On Saturday, April 6, 2013 4:42:45 AM UTC+2, Tito Garrido wrote:

 Hi!

 I was running page speed on my website and all image files from the 
 database (upload folder) are not cached...
 Is there a way to enable cache for them?

 Thanks!

 Tito

 -- 

 Linux User #387870
 .
  _/_õ|__|
 ..º[ .-.___.-._| . . . .
 .__( o)__( o).:___ 


-- 

--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[web2py] Re: Is there a way to cache images from download function?

2013-04-06 Thread Niphlod
there's a new @cache.client decorator to set expire headers in an easy way 

as soon as the book gets updated the docs will be here 
http://web2py.com/books/default/chapter/29/04#cache

On Saturday, April 6, 2013 11:58:17 AM UTC+2, BlueShadow wrote:

 There supposedly is a way the fast download function(web2pyslices)
 def fast_download():
# very basic security (only allow fast_download on 
 your_table.upload_field):
if not request.args(0).startswith(db.your_table.your_field):
return download()
# remove/add headers that prevent/favors client-side caching
#7days
response.headers['Cache-Control'] = max-age=604800
del response.headers['Pragma']
del response.headers['Expires']
filename = os.path.join(request.folder,'uploads',request.args(0))
# send last modified date/time so client browser can enable 
 client-side caching
response.headers['Last-Modified'] = time.strftime(%a, %d %b %Y 
 %H:%M:%S +, time.localtime(os.path.getmtime(filename)))

return response.stream(open(filename,'rb'))

 this is the version I'm experimenting with. the original had a del Cache 
 Control instead of the max age. but google pagespeed still tells me that no 
 expiration date is set.


 On Saturday, April 6, 2013 4:42:45 AM UTC+2, Tito Garrido wrote:

 Hi!

 I was running page speed on my website and all image files from the 
 database (upload folder) are not cached...
 Is there a way to enable cache for them?

 Thanks!

 Tito

 -- 

 Linux User #387870
 .
  _/_õ|__|
 ..º[ .-.___.-._| . . . .
 .__( o)__( o).:___ 



-- 

--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[web2py] Re: Is there a way to cache images from download function?

2013-04-06 Thread Massimo Di Pierro
Does that work for download? Download returns a stream not a string.

On Saturday, 6 April 2013 06:48:55 UTC-5, Niphlod wrote:

 there's a new @cache.client decorator to set expire headers in an easy way 
 
 as soon as the book gets updated the docs will be here 
 http://web2py.com/books/default/chapter/29/04#cache

 On Saturday, April 6, 2013 11:58:17 AM UTC+2, BlueShadow wrote:

 There supposedly is a way the fast download function(web2pyslices)
 def fast_download():
# very basic security (only allow fast_download on 
 your_table.upload_field):
if not request.args(0).startswith(db.your_table.your_field):
return download()
# remove/add headers that prevent/favors client-side caching
#7days
response.headers['Cache-Control'] = max-age=604800
del response.headers['Pragma']
del response.headers['Expires']
filename = os.path.join(request.folder,'uploads',request.args(0))
# send last modified date/time so client browser can enable 
 client-side caching
response.headers['Last-Modified'] = time.strftime(%a, %d %b %Y 
 %H:%M:%S +, time.localtime(os.path.getmtime(filename)))

return response.stream(open(filename,'rb'))

 this is the version I'm experimenting with. the original had a del Cache 
 Control instead of the max age. but google pagespeed still tells me that no 
 expiration date is set.


 On Saturday, April 6, 2013 4:42:45 AM UTC+2, Tito Garrido wrote:

 Hi!

 I was running page speed on my website and all image files from the 
 database (upload folder) are not cached...
 Is there a way to enable cache for them?

 Thanks!

 Tito

 -- 

 Linux User #387870
 .
  _/_õ|__|
 ..º[ .-.___.-._| . . . .
 .__( o)__( o).:___ 



-- 

--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




[web2py] Re: Is there a way to cache images from download function?

2013-04-06 Thread Niphlod
cache.client (when no cache_model is passed) just sets headers (that's the 
idea at the bottom). 
When you pass a cache_model, it sets headers AND cache the results (and 
that's the idea at the top).

cache.client was created in the first place to avoid having to set headers 
repeatedly.

There's no improvement to look for caching the results of a download() ^_^

On Saturday, April 6, 2013 3:23:22 PM UTC+2, Massimo Di Pierro wrote:

 Does that work for download? Download returns a stream not a string.

 On Saturday, 6 April 2013 06:48:55 UTC-5, Niphlod wrote:

 there's a new @cache.client decorator to set expire headers in an easy 
 way 
 as soon as the book gets updated the docs will be here 
 http://web2py.com/books/default/chapter/29/04#cache

 On Saturday, April 6, 2013 11:58:17 AM UTC+2, BlueShadow wrote:

 There supposedly is a way the fast download function(web2pyslices)
 def fast_download():
# very basic security (only allow fast_download on 
 your_table.upload_field):
if not request.args(0).startswith(db.your_table.your_field):
return download()
# remove/add headers that prevent/favors client-side caching
#7days
response.headers['Cache-Control'] = max-age=604800
del response.headers['Pragma']
del response.headers['Expires']
filename = os.path.join(request.folder,'uploads',request.args(0))
# send last modified date/time so client browser can enable 
 client-side caching
response.headers['Last-Modified'] = time.strftime(%a, %d %b %Y 
 %H:%M:%S +, time.localtime(os.path.getmtime(filename)))

return response.stream(open(filename,'rb'))

 this is the version I'm experimenting with. the original had a del Cache 
 Control instead of the max age. but google pagespeed still tells me that no 
 expiration date is set.


 On Saturday, April 6, 2013 4:42:45 AM UTC+2, Tito Garrido wrote:

 Hi!

 I was running page speed on my website and all image files from the 
 database (upload folder) are not cached...
 Is there a way to enable cache for them?

 Thanks!

 Tito

 -- 

 Linux User #387870
 .
  _/_õ|__|
 ..º[ .-.___.-._| . . . .
 .__( o)__( o).:___ 



-- 

--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [web2py] Re: Is there a way to cache images from download function?

2013-04-06 Thread Tito Garrido
Hi Folks!

Thanks for your answers but my implementation is a little bit different so
none of the solutions above worked, I have tried to adapt the slice
solution but it didn't work also, here is my download function, I am
using the name of the files instead of the hash name (I am also using
upload separated = True:

def images():
''' get the image using its name
param1 = table
param2 = type (image,thumb,minithumb,logo,banner)
param3 = name '''
import os,time
tabela =  request.args(0)
tipo = request.args(1)
nome_imagem = request.args(2)
if tabela == 'artista':
row = db.artista(image_filename=nome_imagem)
image=row[tipo]
elif tabela == 'personagem':
row = db.personagem(image_filename=nome_imagem)
image=row[tipo]
elif tabela == 'novela':
if tipo == 'logo':
row = db.novela(logo_filename=nome_imagem)
elif tipo == 'banner':
row = db.novela(banner_filename=nome_imagem)
image=row[tipo]
if not image:
raise HTTP(404)
else:
request.args.append(image)
response.headers['Cache-Control'] = max-age=604800 *# it is still
showing Cache-Control=0*
#del response.headers['Pragma'] *# error, Pragma key not available*
#del response.headers['Expires'] *# error, Expires key not available*
splits = image.split('.')
path = splits[0] + '.' + splits[1] + '/' + splits[2][:2] + '/'
global_path = path + image
filename = os.path.join(request.folder,'uploads',global_path)
# send last modified date/time so client browser can enable client-side
caching
response.headers['Last-Modified'] = time.strftime(%a, %d %b %Y
%H:%M:%S +, time.localtime(os.path.getmtime(filename)))
return response.download(request,db)

I am pretty syre that  I am missing something here... using @cache-client
raised an argument missing error, 0 given 1 expected...

How can I change my function to works with cache?

Thanks!

Tito



On Sat, Apr 6, 2013 at 11:19 AM, Niphlod niph...@gmail.com wrote:

 cache.client (when no cache_model is passed) just sets headers (that's the
 idea at the bottom).
 When you pass a cache_model, it sets headers AND cache the results (and
 that's the idea at the top).

 cache.client was created in the first place to avoid having to set headers
 repeatedly.

 There's no improvement to look for caching the results of a download()
 ^_^


 On Saturday, April 6, 2013 3:23:22 PM UTC+2, Massimo Di Pierro wrote:

 Does that work for download? Download returns a stream not a string.

 On Saturday, 6 April 2013 06:48:55 UTC-5, Niphlod wrote:

 there's a new @cache.client decorator to set expire headers in an easy
 way 
 as soon as the book gets updated the docs will be here
 http://web2py.com/books/**default/chapter/29/04#cachehttp://web2py.com/books/default/chapter/29/04#cache

 On Saturday, April 6, 2013 11:58:17 AM UTC+2, BlueShadow wrote:

 There supposedly is a way the fast download function(web2pyslices)
 def fast_download():
# very basic security (only allow fast_download on
 your_table.upload_field):
if not request.args(0).startswith(**db.your_table.your_field):
return download()
# remove/add headers that prevent/favors client-side caching
#7days
response.headers['Cache-**Control'] = max-age=604800
del response.headers['Pragma']
del response.headers['Expires']
filename = os.path.join(request.folder,'**uploads',request.args(0))
# send last modified date/time so client browser can enable
 client-side caching
response.headers['Last-**Modified'] = time.strftime(%a, %d %b %Y
 %H:%M:%S +, time.localtime(os.path.getmtim**e(filename)))

return response.stream(open(filename,**'rb'))

 this is the version I'm experimenting with. the original had a del
 Cache Control instead of the max age. but google pagespeed still tells me
 that no expiration date is set.


 On Saturday, April 6, 2013 4:42:45 AM UTC+2, Tito Garrido wrote:

 Hi!

 I was running page speed on my website and all image files from the
 database (upload folder) are not cached...
 Is there a way to enable cache for them?

 Thanks!

 Tito

 --

 Linux User #387870
 .
  _/_õ|__|
 ..º[ .-.___.-._| . . . .
 .__( o)__( o).:___

  --

 ---
 You received this message because you are subscribed to the Google Groups
 web2py-users group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to web2py+unsubscr...@googlegroups.com.
 For more options, visit https://groups.google.com/groups/opt_out.






-- 

Linux User #387870
.
 _/_õ|__|
..º[ .-.___.-._| . . . .
.__( o)__( o).:___

-- 

--- 
You received this message because you are subscribed to the Google Groups 
web2py-users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [web2py] Re: Is there a way to cache images from download function?

2013-04-06 Thread Tito Garrido
Now it works! I just changed the return function to return
response.stream(open(filename,'rb')) :D

Thanks folks!

PS: How to use @client-cache? :)

Regards,

Tito


On Sat, Apr 6, 2013 at 1:36 PM, Tito Garrido titogarr...@gmail.com wrote:

 Hi Folks!

 Thanks for your answers but my implementation is a little bit different so
 none of the solutions above worked, I have tried to adapt the slice
 solution but it didn't work also, here is my download function, I am
 using the name of the files instead of the hash name (I am also using
 upload separated = True:

 def images():
 ''' get the image using its name
 param1 = table
 param2 = type (image,thumb,minithumb,logo,banner)
 param3 = name '''
 import os,time
 tabela =  request.args(0)
 tipo = request.args(1)
 nome_imagem = request.args(2)
 if tabela == 'artista':
 row = db.artista(image_filename=nome_imagem)
 image=row[tipo]
 elif tabela == 'personagem':
 row = db.personagem(image_filename=nome_imagem)
 image=row[tipo]
 elif tabela == 'novela':
 if tipo == 'logo':
 row = db.novela(logo_filename=nome_imagem)
 elif tipo == 'banner':
 row = db.novela(banner_filename=nome_imagem)
 image=row[tipo]
 if not image:
 raise HTTP(404)
 else:
 request.args.append(image)
 response.headers['Cache-Control'] = max-age=604800 *# it is still
 showing Cache-Control=0*
 #del response.headers['Pragma'] *# error, Pragma key not available*
 #del response.headers['Expires'] *# error, Expires key not available*
 splits = image.split('.')
 path = splits[0] + '.' + splits[1] + '/' + splits[2][:2] + '/'
 global_path = path + image
 filename = os.path.join(request.folder,'uploads',global_path)
 # send last modified date/time so client browser can enable
 client-side caching
 response.headers['Last-Modified'] = time.strftime(%a, %d %b %Y
 %H:%M:%S +, time.localtime(os.path.getmtime(filename)))
 return response.download(request,db)

 I am pretty syre that  I am missing something here... using @cache-client
 raised an argument missing error, 0 given 1 expected...

 How can I change my function to works with cache?

 Thanks!

 Tito



 On Sat, Apr 6, 2013 at 11:19 AM, Niphlod niph...@gmail.com wrote:

 cache.client (when no cache_model is passed) just sets headers (that's
 the idea at the bottom).
 When you pass a cache_model, it sets headers AND cache the results (and
 that's the idea at the top).

 cache.client was created in the first place to avoid having to set
 headers repeatedly.

 There's no improvement to look for caching the results of a download()
 ^_^


 On Saturday, April 6, 2013 3:23:22 PM UTC+2, Massimo Di Pierro wrote:

 Does that work for download? Download returns a stream not a string.

 On Saturday, 6 April 2013 06:48:55 UTC-5, Niphlod wrote:

 there's a new @cache.client decorator to set expire headers in an easy
 way 
 as soon as the book gets updated the docs will be here
 http://web2py.com/books/**default/chapter/29/04#cachehttp://web2py.com/books/default/chapter/29/04#cache

 On Saturday, April 6, 2013 11:58:17 AM UTC+2, BlueShadow wrote:

 There supposedly is a way the fast download function(web2pyslices)
 def fast_download():
# very basic security (only allow fast_download on
 your_table.upload_field):
if not request.args(0).startswith(**db.your_table.your_field):
return download()
# remove/add headers that prevent/favors client-side caching
#7days
response.headers['Cache-**Control'] = max-age=604800
del response.headers['Pragma']
del response.headers['Expires']
filename = os.path.join(request.folder,'**uploads',request.args(0))
# send last modified date/time so client browser can enable
 client-side caching
response.headers['Last-**Modified'] = time.strftime(%a, %d %b %Y
 %H:%M:%S +, time.localtime(os.path.getmtim**e(filename)))

return response.stream(open(filename,**'rb'))

 this is the version I'm experimenting with. the original had a del
 Cache Control instead of the max age. but google pagespeed still tells me
 that no expiration date is set.


 On Saturday, April 6, 2013 4:42:45 AM UTC+2, Tito Garrido wrote:

 Hi!

 I was running page speed on my website and all image files from the
 database (upload folder) are not cached...
 Is there a way to enable cache for them?

 Thanks!

 Tito

 --

 Linux User #387870
 .
  _/_õ|__|
 ..º[ .-.___.-._| . . . .
 .__( o)__( o).:___

  --

 ---
 You received this message because you are subscribed to the Google Groups
 web2py-users group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to web2py+unsubscr...@googlegroups.com.
 For more options, visit https://groups.google.com/groups/opt_out.






 --

 Linux User #387870
 .
  _/_õ|__|
 ..º[ .-.___.-._| . . . .
 .__( o)__( o).:___




-- 

Linux User