-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare Command.CommandResult for reuse, start work on CommandResultC…
…ache
- Loading branch information
1 parent
06c8d32
commit b26fa2e
Showing
3 changed files
with
84 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import time | ||
|
||
import mailpile.util | ||
from mailpile.i18n import gettext as _ | ||
from mailpile.i18n import ngettext as _n | ||
from mailpile.util import * | ||
|
||
|
||
class CommandResultCache(object): | ||
# | ||
# This is a persistent cache of commands and results we may want | ||
# to refresh in the background and/or reuse. | ||
# | ||
# The way this works: | ||
# - Cache-able commands generate a fingerprint describing themselves. | ||
# - If the fingerprint is found in cache, reuse the result object. | ||
# - Otherwise, run, generate a list of requirements and cache all of: | ||
# the command object itself, the requirements, the result object. | ||
# - Internal state changes (tag ops, new mail, etc.) call mark_dirty() | ||
# describing which assets (requirements) have changed. | ||
# - Periodically, the cache is refreshed, which re-runs any dirtied | ||
# commands and fires events notifying the UI about changes. | ||
# | ||
|
||
def __init__(self): | ||
self.cache = {} | ||
self.dirty = set() | ||
|
||
def cache_result(self, fprint, expires, req, command_obj, result_obj): | ||
self.cache[fprint] = (expires, req, command_obj, result_obj) | ||
|
||
def mark_dirty(self, requirement): | ||
for fprint, (e, r, co, ro) in self.cache.iteritems(): | ||
if req in r: | ||
self.dirty.add(fprint) | ||
|
||
def refresh(self, extend=60): | ||
now = time.time() | ||
expired = set([f for f in self.cache if self.cache[f][0] < now]) | ||
for fp in expired: | ||
del self.cache[fp] | ||
|
||
dirty, self.dirty = self.dirty, set() | ||
for fprint in (dirty - expired): | ||
exp, req, co, ro = self.cache[fprint] | ||
ro = co.refresh() | ||
self.cache[fprint] = (exp + extend, req, co, ro) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters