3
from flexget.plugin import register_plugin
5
log = logging.getLogger('cached')
11
Implements transparent caching decorator @cached for inputs.
13
Decorator has two parameters
15
:name: in which the configuration is present in feeds configuration.
16
:key: in which the configuration has the cached resource identifier (ie. url). If the :key: is not
17
given or present in the configuration :name: is expected to be a cache name (ie. url)
19
Configuration assumptions may make this unusable in some (future) inputs
24
def __init__(self, name, key=None):
28
def __call__(self, func):
30
def wrapped_func(*args, **kwargs):
32
# get feed from method parameters
35
# get name for a cache from feeds configuration
36
if not self.name in feed.config:
37
raise Exception('@cache config name %s is not configured in feed %s' % (self.name, feed.name))
38
config = feed.config[self.name]
40
log.log(5, 'config: %s' % config)
41
log.log(5, 'self.name: %s' % self.name)
42
log.log(5, 'self.key: %s' % self.key)
44
if isinstance(config, dict) and self.key in config:
45
name = feed.config[self.name][self.key]
47
name = feed.config[self.name]
49
log.debug('cache name: %s (has: %s)' % (name, ', '.join(self.cache.keys())))
51
if name in self.cache:
52
log.log(5, 'cache hit')
54
for entry in self.cache[name]:
55
fresh = copy.deepcopy(entry)
56
feed.entries.append(fresh)
59
feed.verbose_progress('Restored %s entries from cache' % count, log)
61
log.log(5, 'cache miss')
64
# store results to cache
65
log.debug('storing to cache %s %s entries' % (name, len(feed.entries)))
66
self.cache[name] = copy.deepcopy(feed.entries)
73
def on_process_start(self, feed):
74
"""Internal. Clears the input cache on every process"""
75
# as flexget runs only once process per run this is not necessary,
76
# will be needed in the future tough
80
register_plugin(CacheClearer, 'cache_clearer', builtin=True)