国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

用Python的Tornado框架結合memcached頁面改善博客性能

2019-11-25 17:40:03
字體:
來源:轉載
供稿:網友

原因

Blog是一個更新并不很頻繁的一套系統,但是每次刷新頁面都要更新數據庫反而很浪費資源,添加靜態頁面生成是一個解決辦法,同時緩存是一個更好的主意,可以結合Memcached添加少量的代碼進行緩存,而且免去去了每次更新文章都要重新生成靜態頁面,特別當頁面特別多時.
實現

主要通過頁面的uri進行緩存,結合tornado.web.RequestHandler的prepare和on_finish方法函數, prepare 主要是請求前執行,on_finish()是請求結束之前執行.在渲染模板時緩存頁面內容,然后在請求前檢測是否有緩存,如果有直接輸出緩存,結束請求,在POST提交之后清空所有緩存,重新生成緩存,從而保證內容實時性.由于登錄用戶和普通用戶的頁面不相同,所以不緩存登錄用戶頁面(代碼中沒有體現,請自行實現).主要python代碼(省略了模板渲染的代碼):

#!/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#### 省略Cache類定義 #####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):  """ 繼承tornado請求基類,重寫 prepare和on_finish方法 """  cache = Memcached  def render(self, template_path, *args, **kwargs):    """ 渲染模板 """    # 省略渲染模板代碼    content = ''   # 渲染模板后的內容    if self.request.method == "GET" and CACHED and /      not self.request.path.startswith("/admin"):      self.cache.set(self.request.uri, content) # 將渲染后的內容緩存起來    self.write(content)  def prepare(self):    super(BaseHandler, self).prepare()    # 如果請求是GET方法,而且不是請求后臺    if self.request.method == "GET" and CACHED and /      not self.request.path.startswith("/admin"):      # 嘗試獲取當前頁面的緩存      cache = self.cache.get(self.request.uri)      # 獲取緩存則輸出頁面,結束請求      if cache:        return self.finish(cache)  def on_finish(self):    """ 重寫結束請求前的方法函數 """    if self.request.method == "POST":      # 如果遇到POST提交則清空緩存      self.cache.flush()

緩存系統在redis和Memcached選擇了很久,因為只是單純的緩存頁面所以最后選擇了memcached,使用pylibmc python庫.
測試

使用webbench 網站壓力測試對比了緩存前后的結果: 使用緩存前

$ 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.

使用緩存后:

$ 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.

明顯快了很多...

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彭州市| 大足县| 芮城县| 河曲县| 嘉荫县| 顺义区| 杨浦区| 乡城县| 屯门区| 天峻县| 荔浦县| 辉南县| 宿迁市| 克东县| 凌海市| 永仁县| 德庆县| 景德镇市| 平定县| 宁城县| 独山县| 德惠市| 合水县| 慈溪市| 英山县| 普格县| 刚察县| 岳普湖县| 南康市| 三门峡市| 眉山市| 筠连县| 全椒县| 枣庄市| 镇江市| 子洲县| 武川县| 达拉特旗| 张家界市| 泾川县| 云龙县|