Use Python Tornado framework and memcached page to improve blog performance. tornadomemcached

Source: Internet
Author: User

Use Python Tornado framework and memcached page to improve blog performance. tornadomemcached

Cause

Blog is a set of systems that are not updated frequently, but every time you refresh the page, updating the database is a waste of resources. Adding static page generation is a solution, caching is also a better idea. You can use Memcached to add a small amount of code for caching, and avoid generating static pages every time you update an article, especially when there are many pages.
Implementation

The cache is mainly based on the page uri, combined with tornado. web. the prepare and on_finish methods of RequestHandler are mainly executed before the request, and on_finish () is executed before the request ends. cache the page content when rendering the template, and check whether there is a cache before the request. If there is a direct output cache, end the request, clear all the caches after POST submission, and regenerate the cache, this ensures real-time content. because the login user page is different from the normal user page, the login user page is not cached (the code is not reflected, please implement it yourself ). main python code (the template rendering code is omitted ):

#! /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 #### omitting the definition of the Cache class #### 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 @ 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): timeout = timeout if timeout 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 the tornado request base class, and overrides the prepare and on_finish methods "cache = Memcached def render (self, template_path, * args, ** kwargs): "rendering template" "# omitting the rendering template code content ='' # rendering template content if self. request. method = "GET" and CACHED and \ not self. request. path. startswith ("/admin"): self. cache. set (self. request. uri, content) # cache the rendered content self. write (content) def prepare (self): super (BaseHandler, self ). prepare () # if the request is a GET method and is not a request background if self. request. method = "GET" and CACHED and \ not self. request. path. startswith ("/admin"): # Try to get the cache of the current page = self. cache. get (self. request. uri) # obtain the cache and output the page. End the request if cache: return self. finish (cache) def on_finish (self): "override the method function before the end of the request" if self. request. method = "POST": # Clear cache self if POST is submitted. cache. flush ()

The cache system has been selected for a long time in redis and Memcached. Because it is only a simple cache page, memcached is selected at last, and the python library of pylibmc is used.
Test

The results before and after the cache are compared using the webbench website stress test: before using the cache

$ webbench -c 500 -t 30 http://www.linuxzen.com/Webbench - Simple Web Benchmark 1.5Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.Benchmarking: GET http://www.linuxzen.com/500 clients, running 30 sec.Speed=54 pages/min, 38160 bytes/sec.Requests: 27 susceed, 0 failed.

After cache is used:

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