How set X-Accel-Expires in add_view, add_static_view

53 views
Skip to first unread message

Grigorii Eremeev

unread,
Jul 20, 2014, 11:34:48 AM7/20/14
to pylons-...@googlegroups.com
Hello

I want use cache nginx and cache browser.

For managing cache nginx i use X-Accel-Expires.
For managing cache browser i use Cache-Control.

Cache-Control is simple, need set parameters http_cache in add_view and cache_max_age in add_static_view.

But how set X-Accel-Expires in same level?(as a parameter in add_view)

In my opinion such parameter does not exists or am I mistaken?
if so, how to pass this parameter to "tween"?
So I installed it there:

config.add_view(
        cache_view,
        route_name='cache_view',
        renderer='test_cache_nginx:templates/index.jinja2',
        http_cache=10,
        x_accel_expires=100,
    )

#tween:
def simple_tween_factory(handler, registry):

    def simple_tween(request):

        response = handler(request)
        #x_accel_expires = ... here i get parameter ... 

        if not x_accel_expires:
            x_accel_expires = registry.settings.get('cache.x_accel_expires', 0)
        response.headers.add('X-Accel-Expires', x_accel_expires)

        return response

    return simple_tween

Michael Merickel

unread,
Jul 28, 2014, 10:33:44 AM7/28/14
to Pylons
Your setting to `add_view` is currently being dropped. It's not valid
to pass arbitrary things to `add_view`. A good way would be to mutate
the response via a decorator, adding the header in yourself. No need
for a tween.

def x_accel_expires(max_age):
def decorator(view):
def wrapper(context, request):
response = view(context, request)
response.headers.add('X-Accel-Expires', max_age)
return response
return wrapper
return decorator

config.add_view(
cache_view,
route_name='cache_view',
renderer='test_cache_nginx:templates/index.jinja2',
http_cache=10,
decorator=x_accel_expires(100)
)
> --
> 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 pylons-discus...@googlegroups.com.
> To post to this group, send email to pylons-...@googlegroups.com.
> Visit this group at http://groups.google.com/group/pylons-discuss.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages