Improve blog performance with Python's tornado framework combined with memcached pages

Source: Internet
Author: User
Tags benchmark
cause

Blog is an update is not very frequent system, but every time to refresh the page to update the database is rather a waste of resources, add static page generation is a solution, while the cache is a better idea, can be combined with memcached to add a small amount of code to cache, and eliminate the need to update each article to regenerate static pages, especially when the page is very long.
Realize

Mainly through the page URI caching, combined with Tornado.web.RequestHandler prepare and on_finish method functions, prepare is mainly the request before execution, On_finish () is executed before the request is finished. Caches page content when rendering a template, then detects if there is a cache before the request, if there is a direct output cache, ends the request, empties all caches after Post commits, regenerates the cache, and thus guarantees real-time content. Due to the different pages of the logged in user and the normal user, So do not cache the login user page (not reflected in the code, please do it yourself). Main Python code (code that omits template rendering):

#!/usr/bin/env python#-*-coding:utf-8-*-## author:cold# e-mail:wh_linux@126.com# DATE:13/01/14 09:57:31#  Desc: #import configimport pylibmcfrom tornado.web import requesthandler#### omit the Cache class definition # # # # # #class Memcached (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 @class    Method 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): Timeout = Timeout if time Out else config. Cache_timeout return Cls._mc.set (key, Value, TIMEOUT) @classmethod def delete (CLS, key): Return Cls._mc.delete (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 (RequestHandler): "" "Inherits Tornado request base class, overrides prepare and On_finish method" "" CAC he = Memcached def render (self, template_path, *args, **kwargs): "" "Render Template" "" # Omit render template code content = ' # after rendering template Content if Self.request.method = = "GET" and CACHED and \ Not Self.request.path.startswith ("/admin"): self.cache.se T (Self.request.uri, content) # caches the rendered content self.write (content) def prepare (self): Super (Basehandler, self). Prepare ( # If the request is a get method, and not a request background if Self.request.method = = "GET" and CACHED and \ Not Self.request.path.startswith ("/adm In "): # try to get cache of current Page cached = Self.cache.get (Self.request.uri) # Get cache then output page, end request if Cache:return Self.finish (Cache) def on_finish (self): "" "Override method function before end request" "" If Self.request.method = = "POST": # empty if a POST submission is encountered Cache Self.cache.flush ()

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

Using the Webbench site stress tests compare the results before and after the cache: using the cache

$ webbench-c 500-t http://www.linuxzen.com/Webbench-Simple Web Benchmark 1.5Copyright (c) Radim Kolar 1997-2004, GP L Open Source Software.Benchmarking:GET http://www.linuxzen.com/500 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.5Copyright (c) Radim Kolar 1997-2004, GP L Open Source Software.Benchmarking:GET http://www.linuxzen.com/500 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.