Here's my nginx configuration for SSI + Memcahced (nginx.conf):

location / {
        ssi on;

        proxy_set_header X_FORWARDED_PROTO $scheme;
        proxy_set_header X_FORWARDED_HOST $server_name;
        proxy_set_header X_FORWARDED_FOR $remote_addr;

        if ($request_method = POST) {
                proxy_pass http://pylons;
        }

        set             $memcached_key  $request_uri;
        memcached_pass  localhost:11211;
        default_type    text/html;
        error_page      404 = /dynamic;
}

location /dynamic {
        ssi on;

        proxy_set_header X_FORWARDED_PROTO $scheme;
        proxy_set_header X_FORWARDED_HOST $server_name;
        proxy_set_header X_FORWARDED_FOR $remote_addr;

        proxy_pass http://pylons;
}

This is the cache class for storing in memcached:

class Cache(object):
    def __init__(self, url, timeout=None, prefix=None):
        self.mc = memcache.Client([url])
        self.timeout = timeout or 300
        self.prefix = prefix
    def get(self, key):
        return self.mc.get(key)
    def set(self, key, value, timeout=None):
        if self.prefix:
            key = self.prefix + key
        self.mc.set(key, value, timeout or self.timeout)

You can initialize it in lib/app_globals.py:

class Globals(object):
    def __init__(self):
        self.cache = Cache(url='127.0.0.1:11211')

Here's the dynamic controller used by SSI (controllers/dynamic.py):

class DynamicController(BaseController):
    def display(self):
        return render('/dynamic/' + str(request.params['template']))

Here's the SSI helper and cache decorator (lib/helpers.py):

def ssi(action=None, template=None):
    if template:
        qry_str = '?template=' + template + '&$args'
    else:
        qry_str = '?$args'
        if not action:
            action = request.urlvars['action']
    uri = '/dynamic/' + (action or 'display') + qry_str
    return '<!--# include virtual="' + uri + '" -->'

def cache(timeout=None):
    def wrapper(func, *args, **kwargs):
        request = args[0]._py_object.request
        content = func(*args, **kwargs)
            key = request.path_qs
            g.cache.set(key, str(content), timeout)
        return content
    return decorator(wrapper)

Here's the action that renders and caches a page (controllers/
page.py):

    @h.cache(600)
    def view(self):
        c.name = db.lookup(params['info'])
        return render('view.html')

Here's the template for the above action (view.html) - it includes a
dynamic part using the ssi helper:

    ${h.ssi(template='user.html')|n}
    Page name:  ${c.name}

Here's a dynamic template (user.html) used by the cached page
view.html:

% if session.has_key('user'):
    ${session.get('user')}
% else:
    ${h.link_to(u'signin', h.url_for('/account/signin'))}
% endif



On Jan 9, 4:29 am, mk <[email protected]> wrote:
> Tycon wrote:
> > So the bottom line is that for each page only the dynamic parts of the
> > page will be rendered on each user request (dynamic parts are the ones
> > that can be different even for request for the same URL).
>
> I would only like to say that if you get such configuration working,
> please be sure to post [ gory details | ideally, howto ] here or on some
> blog and let us know here. At least I would be very interested to see
> how such solution is built.
>
> Regards,
> mk
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to