# HG changeset patch # User Paul Boddie # Date 1390671572 -3600 # Node ID e675cfc8739569626dfdcc0880e015ce5ffd6345 # Parent c7fd6b245e138f29304a7d84f633981065981d1d Added page-related caching functions. diff -r c7fd6b245e13 -r e675cfc87395 MoinSupport.py --- a/MoinSupport.py Tue Jan 21 18:26:01 2014 +0100 +++ b/MoinSupport.py Sat Jan 25 18:39:32 2014 +0100 @@ -4,7 +4,7 @@ @copyright: 2008, 2009, 2010, 2011, 2012, 2013, 2014 by Paul Boddie @copyright: 2000-2004 Juergen Hermann - 2004 by Florian Festi + 2004,2006 by Florian Festi 2006 by Mikko Virkkil 2005-2008 MoinMoin:ThomasWaldmann 2007 MoinMoin:ReimarBauer @@ -15,7 +15,8 @@ from DateSupport import * from MoinMoin.parser import text_moin_wiki from MoinMoin.Page import Page -from MoinMoin import config, search, wikiutil +from MoinMoin.support.python_compatibility import hash_new +from MoinMoin import caching, config, search, wikiutil from shlex import shlex import re import time @@ -810,6 +811,64 @@ return {"timestamp" : DateTime(time.gmtime(mtime)[:6] + ("UTC",)), "comment" : comment} +# Page caching functions. + +def getPageCacheKey(page, request): + + """ + Return a cache key for the given 'page' using information in the 'request'. + """ + + if hasattr(page, "getCacheKey"): + return page.getCacheKey(request) + + key = getPageFormatterName(page, request) + if request.args: + args = request.args.items() + args.sort() + key_args = [] + for k, v in args: + key_args.append("%s=%s" % (k, wikiutil.url_quote(v))) + arg_str = "&".join(key_args) + key = "%s:%s" % (key, hash_new('sha1', arg_str).hexdigest()) + return key + +def enforcePageCacheLimit(page, request): + + """ + Prevent too many cache entries being stored for the given 'page', using the + 'request' to obtain cache items and configuration details. + """ + + if hasattr(page, "enforceCacheLimit"): + page.enforceCacheLimit(request) + + keys = caching.get_cache_list(request, page, 'item') + try: + cache_limit = int(getattr(request.cfg, 'page_cache_limit', "10")) + except ValueError: + cache_limit = 10 + + if len(keys) >= cache_limit: + items = [caching.CacheEntry(request, page, key, scope='item') for key in keys] + item_ages = [(item.mtime(), item) for item in items] + item_ages.sort() + for item_age, item in item_ages[:-cache_limit]: + item.remove() + +def getPageFormatterName(page, request=None): + + """ + Return a formatter name as used in the caching system for the given 'page' + or using information provided by an optional 'request'. + """ + + formatter = getattr(page, 'formatter', None) or request and getattr(request, 'formatter', None) + if not formatter: + return '' + module = formatter.__module__ + return module[module.rfind('.') + 1:] + # Page parsing and formatting of embedded content. def getOutputTypes(request, format): diff -r c7fd6b245e13 -r e675cfc87395 README.txt --- a/README.txt Tue Jan 21 18:26:01 2014 +0100 +++ b/README.txt Sat Jan 25 18:39:32 2014 +0100 @@ -73,6 +73,7 @@ tokeniser. * Added RFC 2822 datetime formatting. * Added a "raw" parser which just formats its input as text. + * Added page-related caching functions. New in MoinSupport 0.4.1 (Changes since MoinSupport 0.4) -------------------------------------------------------- diff -r c7fd6b245e13 -r e675cfc87395 docs/COPYING.txt --- a/docs/COPYING.txt Tue Jan 21 18:26:01 2014 +0100 +++ b/docs/COPYING.txt Sat Jan 25 18:39:32 2014 +0100 @@ -1,14 +1,14 @@ Licence Agreement ----------------- -Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013 Paul Boddie +Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Paul Boddie Some pieces of MoinMoin code were used in this work - typically pieces which demonstrate how to perform certain common tasks - and are thus covered by the following copyrights: Copyright (C) 2000-2004 Juergen Hermann -Copyright (C) 2004 by Florian Festi +Copyright (C) 2004, 2006 by Florian Festi Copyright (C) 2005-2008 MoinMoin:ThomasWaldmann Copyright (C) 2006 by Mikko Virkkil Copyright (C) 2007 MoinMoin:ReimarBauer