Remove support for persistent memoization

A different form of persistent memoization should be reimplemented later.

Refs #159.
This commit is contained in:
Kim Silkebækken 2013-02-01 22:01:53 +01:00
parent 260e40a544
commit 4cda38e214
2 changed files with 4 additions and 28 deletions

View File

@ -1,11 +1,5 @@
# -*- coding: utf-8 -*-
try:
import cPickle as pickle
except ImportError:
import pickle
import os
import tempfile
import time
@ -13,11 +7,9 @@ class memoize(object):
'''Memoization decorator with timeout.'''
_cache = {}
def __init__(self, timeout, additional_key=None, persistent=False, persistent_file=None):
def __init__(self, timeout, additional_key=None):
self.timeout = timeout
self.additional_key = additional_key
self.persistent = persistent
self.persistent_file = persistent_file or os.path.join(tempfile.gettempdir(), 'powerline-cache')
def __call__(self, func):
def decorated_function(*args, **kwargs):
@ -25,27 +17,11 @@ class memoize(object):
key = (func.__name__, args, tuple(kwargs.items()), self.additional_key())
else:
key = (func.__name__, args, tuple(kwargs.items()))
if self.persistent:
try:
with open(self.persistent_file, 'rb') as fileobj:
self._cache = pickle.load(fileobj)
except (IOError, EOFError, ValueError):
self._cache = {}
cached = self._cache.get(key, None)
if cached is None or time.time() - cached['time'] > self.timeout:
cached = self._cache[key] = {
'result': func(*args, **kwargs),
'time': time.time(),
}
if self.persistent:
try:
with open(self.persistent_file, 'wb') as fileobj:
pickle.dump(self._cache, fileobj)
except IOError:
# Unable to write to file
pass
except TypeError:
# Unable to pickle function result
pass
return cached['result']
return decorated_function

View File

@ -101,7 +101,7 @@ def date(format='%Y-%m-%d'):
return datetime.now().strftime(format)
@memoize(600, persistent=True)
@memoize(600)
def external_ip(query_url='http://ipv4.icanhazip.com/'):
return _urllib_read(query_url).strip()
@ -123,7 +123,7 @@ def uptime(format='{days:02d}d {hours:02d}h {minutes:02d}m'):
return format.format(days=int(days), hours=hours, minutes=minutes)
@memoize(1800, persistent=True)
@memoize(1800)
def weather(unit='c', location_query=None):
import json
@ -232,7 +232,7 @@ def virtualenv():
return os.path.basename(os.environ.get('VIRTUAL_ENV', '')) or None
@memoize(60, persistent=True)
@memoize(60)
def email_imap_alert(username, password, server='imap.gmail.com', port=993, folder='INBOX'):
import imaplib
import re