You can use exceptions to bypass the 'normal' view processing and
rendering. Just like
HTTP error exceptions but raising a valid response with status 200, body,
headers and so
on.
I have never used this approach for view caching but it should be quite
easy to build a generic
cache lookup and make it raise such a Response. Though as you mentioned I
had to create a
custom response too.
Something like ...
from zope.interface import implementer
from pyramid.interfaces import IExceptionResponse
from pyramid.httpexceptions import HTTPOk
@implementer(IExceptionResponse)
class CachedResponse(HTTPOk):
def __init__(self, headers=None, **kw):
super(HTTPOk,self).__init__(headers=headers, **kw)
def __str__(self):
return self.detail
def __call__(self, environ, start_response):
return Response.__call__(self, environ, start_response)
def cache_lookup(request):
...
if found_in_cache:
headers={}
raise CachedResponse(content_type="text/html", body=page_html,
headers=headers)
return None
Hth, Arndt.
2015-03-11 18:02 GMT+01:00 Jonathan Vanasco <[email protected]>:
> I have a few views that are high traffic and too intensive for normal
> processing. I need to cache them.
>
> I tried an inner generator pattern for the HTML, which works well, but
> generating the response is ugly:
>
> @view_config(route="index")
> def my_view(self):
> """the core data is cached within the view.
> """
> @cacheable_decorator
> def _generate():
> as_html = pyramid.renderers.render('/index.mako', {}, self.request)
> metadata = self.request.page_metadata # add_request_method property,
> details about the page
> return (as_html, metadata)
> (page_html, page_metadata) = _generate()
> response = pyramid.response.Response(
> content_type = 'text/html',
> body = page_html,
> status = 200,
> )
> return response
>
> Is there a better pattern or approach for this?
>
> Caching views is popular in a lot of frameworks, but the only Pyramid
> stuff i found were some problems on StackOverflow.
>
> Having to create a response -- or define a custom renderer for it --
> suggests that I'm missing something obvious. The built-in "string"
> renderer won't work -- it sets a 'text/plain' header. There is no 'html'
> renderer.
>
> Anyone have advice?
>
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/pylons-discuss.
> For more options, visit https://groups.google.com/d/optout.
>
Arndt Droullier / nive.io
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.