Improve blogging performance with the Python Tornado framework combined with memcached pages _python

Source: Internet
Author: User
Tags benchmark class definition flush memcached prepare

Reason

Blog is an update is not very frequent a set of systems, but each refresh the page to update the database instead of a waste of resources, add static page generation is a solution, while caching is a better idea, can be combined with memcached add a small amount of code to cache, and remove every update article to regenerate static pages, especially when the page is particularly long.
Implement

Mainly through the URI of the page cache, combined with the Tornado.web.RequestHandler prepare and On_finish method functions, prepare mainly for the implementation before the request, On_finish () is executed before the end of the request. Caching the page content while rendering the template, and then detecting if there is a cache before the request, if there is direct output caching, ending the request, emptying all the caches after post submission, and rebuilding the cache to ensure the content is real-time. Because the pages of the logged-in user and the normal user are different, So do not cache the login user page (not reflected in the code, please do it yourself). Main Python code (omitted template render code):

#!/usr/bin/env python #-*-coding:utf-8-*-# author:cold # e-mail:wh_linux@126.com # DATE:13/01/14 09:57 : # Desc: # import config import pylibmc from tornado.web import RequestHandler # Omit cache class definition ##### class MEMCAC Hed (object): _MC = pylibmc.client.Client (config. Cache_host, binary = True) def __enter__ (self): if config.

  Cached:return Memcached Else:return Cache () def __exit__ (self, exc_type, Exc_val, EXC_TB): Pass @classmethod def get_cache (CLS): Return CLS._MC @classmethod def get (CLS, key, default = None): R = Cls._ Mc.get (key) if not r:r = default return R @classmethod def set (CLS, key, value, timeout = 0): Time out = Timeout If timeout else config. Cache_timeout return Cls._mc.set (key, Value, TIMEOUT) @classmethod def delete (CLS, key): Return Cls._mc.delet E (key) @classmethod def Flush (CLS): Return Cls._mc.flush_all () def __getattr__ (self, key): Return MeMcached.get (Key) def __setattr__ (self, Key, value): Return Memcached.set (key, Value) class Basehandler (Requesthan Dler): "" "Inherits Tornado request base class, overrides prepare and On_finish method" "cache = Memcached def render (self, template_path, *args, **kwar
      GS): "" "" "" Render Template "" "# Omit render template code content = ' # after rendering template contents if Self.request.method = =" Get "and CACHED and \ Not Self.request.path.startswith ("/admin"): Self.cache.set (Self.request.uri, content) # caches the rendered content Self.wri Te (content) def prepare (self): Super (Basehandler, self). Prepare () # If the request is a get method and is not a request background if Self.request.met Hod = = "Get" and CACHED and/Not Self.request.path.startswith ("/admin"): # Attempt to obtain cached cache for current page = self.ca 
    Che.get (Self.request.uri) # Get Cache Output page, end request if Cache:return Self.finish (cache) def on_finish (self):

 "" To override the method function before the end request "" If Self.request.method = "POST": # Empty Cache Self.cache.flush If a POST commit is encountered ()

The caching system has been chosen for a long time in Redis and memcached, because it was simply caching the page, so the memcached was chosen and the PYLIBMC Python library was used.
Test

Use the Webbench site stress tests to compare the results before and after caching: Before using the cache

$ webbench-c 500-t http://www.linuxzen.com/
webbench-simple Web Benchmark 1.5
Copyright (c) Radim Kolar 199 7-2004, GPL Open Source Software.

Benchmarking:get http://www.linuxzen.com/
Clients, running sec.

speed=54 pages/min, 38160 bytes/sec.
Requests:27 susceed, 0 failed.

After using the cache:

$ webbench-c 500-t http://www.linuxzen.com/
webbench-simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1 997-2004, GPL Open Source Software.

Benchmarking:get http://www.linuxzen.com/
Clients, running sec.

speed=256 pages/min, 238544 bytes/sec.
requests:128 susceed, 0 failed.

Obviously a lot faster ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.