首先,需要對上面幾篇介紹的2個callHandler (PerformanceCountCallHandler, CacheCallHandler)進行改寫。
代碼如下:
#-*- coding: UTF-8 -*-#-------------------------------------------------------------------------------# Name:        模組2# Purpose:## Author:      ankier## Created:     22-12-2012# Copyright:   (c) Ankier 2012# Licence:     <2012~2020>#-------------------------------------------------------------------------------import timefrom functools import wraps_Cache ={}def PerformanceCountCallHandler():    def _PerformanceCountCallHandler(originalFunc):        def __PerformanceCountCallHandler(fun):            def wrap(args, kw):                glos = originalFunc.func_globals                package = glos['__package__']                model = glos['__name__']                methodName = originalFunc.func_name                                       timeStart = time.time()                                result = fun(args, kw)                                               timeEnd = time.time()                                print 'package:',package,' , model:', model, ' , methodName:',methodName,'. ', timeEnd - timeStart, 's'                                return result             return wrap        return __PerformanceCountCallHandler    return _PerformanceCountCallHandler
 
#-*- coding: UTF-8 -*-#-------------------------------------------------------------------------------# Name:        模組2# Purpose:## Author:      ankier## Created:     22-12-2012# Copyright:   (c) Ankier 2012# Licence:     <2012~2020>#-------------------------------------------------------------------------------import timeimport hashlibimport pickleimport sysfrom functools import wraps_Cache ={}def __HashParamsKey(function, args, kw):    glos = function.func_globals    package = glos['__package__']    model = glos['__name__']    methodName = function.func_name        key = pickle.dumps((package, model, methodName, args, kw))    return hashlib.sha1(key).hexdigest()def __IsObsolete(entry, duration):    return time.time() - entry['time'] > durationdef CacheCallHandler(duration = sys.maxint):    def _CacheCallHandler(originalFunc):        def __CacheCallHandler(fun):            def wrap(args, kw):                key = __HashParamsKey(originalFunc, args, kw)                      if key not in _Cache or __IsObsolete(_Cache[key], duration):                    #儲存結果                                    result = fun(args, kw)                    _Cache[key] = {'value': result,                                   'time': time.time()}                                    return _Cache[key]['value']             return wrap        return __CacheCallHandler    return _CacheCallHandler
需要一個管理和註冊多種裝飾器的註冊裝飾器。
#-*- coding: UTF-8 -*-#-------------------------------------------------------------------------------# Name:        registerDecorators# Purpose:## Author:      ankier## Created:     23-12-2012# Copyright:   (c) Ankier 2012# Licence:     <2012~2020>#-------------------------------------------------------------------------------def RegisterDecorators(*_decorators):    def _RegisterDecorators(func):        wrap = func        for _deco in _decorators:            __deco = _deco(func)            wrap = __deco(wrap)        return wrap    return _RegisterDecorators
下面看如何使用這些類,cache 在前面, performance handler 在後面。
使用方式如下:
#-*- coding: UTF-8 -*-import timefrom performanceCountCallHandler import PerformanceCountCallHandlerfrom cacheCallHandler import CacheCallHandlerfrom registerDecorators import RegisterDecorators@RegisterDecorators(CacheCallHandler(10000), PerformanceCountCallHandler())def Sum(xx , yy ):    sum = xx + yy    print '------Execute Sum----- '    time.sleep(5)                return sum@RegisterDecorators(CacheCallHandler(1), PerformanceCountCallHandler())def Count(xx , yy ):    sum = xx + yy    print '------Execute Count----- '    time.sleep(5)                return sum
運行效果:
------Execute Sum----- package: None  , model: mainFrame  , methodName: Sum .  5.0 s9package: None  , model: mainFrame  , methodName: Sum .  0.0 s9------Execute Count----- package: None  , model: mainFrame  , methodName: Count .  5.0 s2------Execute Count----- package: None  , model: mainFrame  , methodName: Count .  5.0 s3------Execute Count----- package: None  , model: mainFrame  , methodName: Count .  5.01600003242 s2