Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4995847
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: Python/Ruby

2015-08-31 16:50:38

原因

Blog是一个更新并不很频繁的一套系统,但是每次刷新页面都要更新数据库反而很浪费资源,添加静态页面生成是一个解决办法,同时缓存是一个更好的主意,可以结合Memcached添加少量的代码进行缓存,而且免去去了每次更新文章都要重新生成静态页面,特别当页面特别多时.

实现

主要通过页面的uri进行缓存,结合tornado.web.RequestHandler的prepare和on_finish方法函数, prepare 主要是请求前执行,on_finish()是请求结束之前执行.在渲染模板时缓存页面内容,然后在请求前检测是否有缓存,如果有直接输出缓存,结束请求,在POST提交之后清空所有缓存,重新生成缓存,从而保证内容实时性.由于登录用户和普通用户的页面不相同,所以不缓存登录用户页面(代码中没有体现,请自行实现).主要python代码(省略了模板渲染的代码):


  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. #
  4. # Author : cold
  5. # E-mail : wh_linux@126.com
  6. # Date : 13/01/14 09:57:31
  7. # Desc :
  8. #
  9. import config
  10. import pylibmc
  11. from tornado.web import RequestHandler
  12. #### 省略Cache类定义 #####

  13. class Memcached(object):
  14.     _mc = pylibmc.client.Client(config.CACHE_HOST, binary = True)

  15.     def __enter__(self):
  16.         if config.CACHED:
  17.             return Memcached
  18.         else:
  19.             return Cache()

  20.     def __exit__(self, exc_type, exc_val, exc_tb):
  21.         pass

  22.     @classmethod
  23.     def get_cache(cls):
  24.         return cls._mc

  25.     @classmethod
  26.     def get(cls, key, default = None):
  27.         r = cls._mc.get(key)
  28.         if not r:
  29.             r = default
  30.         return r

  31.     @classmethod
  32.     def set(cls, key, value, timeout = 0):
  33.         timeout = timeout if timeout else config.CACHE_TIMEOUT
  34.         return cls._mc.set(key, value, timeout)

  35.     @classmethod
  36.     def delete(cls, key):
  37.         return cls._mc.delete(key)

  38.     @classmethod
  39.     def flush(cls):
  40.         return cls._mc.flush_all()

  41.     def __getattr__(self, key):
  42.         return Memcached.get(key)

  43.     def __setattr__(self, key, value):
  44.         return Memcached.set(key, value)


  45. class BaseHandler(RequestHandler):
  46.     """ 继承tornado请求基类,重写 prepare和on_finish方法 """
  47.     cache = Memcached

  48.     def render(self, template_path, *args, **kwargs):
  49.         """ 渲染模板 """
  50.         # 省略渲染模板代码
  51.         content = '' # 渲染模板后的内容
  52.         if self.request.method == "GET" and CACHED and \
  53.            not self.request.path.startswith("/admin"):
  54.             self.cache.set(self.request.uri, content) # 将渲染后的内容缓存起来
  55.         self.write(content)

  56.     def prepare(self):
  57.         super(BaseHandler, self).prepare()
  58.         # 如果请求是GET方法,而且不是请求后台
  59.         if self.request.method == "GET" and CACHED and \
  60.            not self.request.path.startswith("/admin"):

  61.             # 尝试获取当前页面的缓存
  62.             cache = self.cache.get(self.request.uri)
  63.             # 获取缓存则输出页面,结束请求
  64.             if cache:
  65.                 return self.finish(cache)

  66.     def on_finish(self):
  67.         """ 重写结束请求前的方法函数 """
  68.         if self.request.method == "POST":
  69.             # 如果遇到POST提交则清空缓存
  70.             self.cache.flush()
缓存系统在redisMemcached选择了很久,因为只是单纯的缓存页面所以最后选择了memcached,使用pylibmc python库.

测试

使用webbench 网站压力测试对比了缓存前后的结果: 使用缓存前


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

使用缓存后:

  1. $ webbench -c 500 -t 30
  2. Webbench - Simple Web Benchmark 1.5
  3. Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
  4. Benchmarking: GET
  5. 500 clients, running 30 sec.
  6. Speed=256 pages/min, 238544 bytes/sec.
  7. Requests: 128 susceed, 0 failed.

明显快了很多...

阅读(1382) | 评论(0) | 转发(0) |
0

上一篇:tornado缓存技术

下一篇:python-twisted系列(1)

给主人留下些什么吧!~~