Skip to content

Commit

Permalink
optionally, cache page text until next edit operation
Browse files Browse the repository at this point in the history
Store the results of page.text() operations in a simple cache
dict. This avoids unnecessary remote roundtrips. Cache is
cleared on each successful page.save() operation. cache
argument can be set to 'False' to disable use of the cache.
  • Loading branch information
AdamWill committed Aug 29, 2015
1 parent dffd7ba commit a8512f3
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions mwclient/page.py
Expand Up @@ -14,6 +14,7 @@ def __init__(self, site, name, info=None, extra_properties=None):
return self.__dict__.update(name.__dict__)
self.site = site
self.name = name
self._textcache = {}

if not info:
if extra_properties:
Expand Down Expand Up @@ -125,14 +126,19 @@ def edit(self, *args, **kwargs):
category=DeprecationWarning, stacklevel=2)
return self.text(*args, **kwargs)

def text(self, section=None, expandtemplates=False):
def text(self, section=None, expandtemplates=False, cache=None):
"""
Get the current wikitext of the page, or of a specific section.
If the page does not exist, an empty string is returned.
If the page does not exist, an empty string is returned. By
default, results will be cached and if you call text() again
with the same section and expandtemplates the result will come
from the cache. The cache is stored on the instance, so it
lives as long as the instance does.
Args:
section (int): numbered section or `None` to get the whole page (default: `None`)
expandtemplates (bool): set to `True` to expand templates (default: `False`)
cache (bool): set to `False` to disable caching (default: `True`)
"""

if not self.can('read'):
Expand All @@ -142,6 +148,10 @@ def text(self, section=None, expandtemplates=False):
if section is not None:
section = text_type(section)

key = hash((section, expandtemplates))
if cache and key in self._textcache:
return self._textcache[key]

revs = self.revisions(prop='content|timestamp', limit=1, section=section, expandtemplates=expandtemplates)
try:
rev = revs.next()
Expand All @@ -152,6 +162,9 @@ def text(self, section=None, expandtemplates=False):
self.last_rev_time = None
if not expandtemplates:
self.edit_time = time.gmtime()

if cache:
self._textcache[key] = text
return text

def save(self, text, summary=u'', minor=False, bot=True, section=None, **kwargs):
Expand Down Expand Up @@ -211,6 +224,9 @@ def do_edit():
# 'newtimestamp' is not included if no change was made
if 'newtimestamp' in result['edit'].keys():
self.last_rev_time = parse_timestamp(result['edit'].get('newtimestamp'))

# clear the page text cache
self._textcache = {}
return result['edit']

def handle_edit_error(self, e, summary):
Expand Down

0 comments on commit a8512f3

Please sign in to comment.