From 35cb72f5aaf5485d0671ac9b600dac4713e7fb6f Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 7 Jun 2022 00:04:29 +0200 Subject: [PATCH] Do a lazy plugin initialization in the Variable class This is useful for two reason: 1. Slightly faster variable initialization times. 2. The cached variable object won't fail on the next `.get()`/`.set()` if the `db` or `redis` plugins have failed for some reason. --- platypush/context/__init__.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/platypush/context/__init__.py b/platypush/context/__init__.py index 0d6876376..d2421ac66 100644 --- a/platypush/context/__init__.py +++ b/platypush/context/__init__.py @@ -171,15 +171,17 @@ class Variable: def __init__(self, name: str, persisted: bool = True): self.name = name - plugin = get_plugin('variable') - self._get_action = getattr(plugin, 'get' if persisted else 'mget') - self._set_action = getattr(plugin, 'set' if persisted else 'mset') + self._persisted = persisted def get(self) -> Optional[Any]: - return self._get_action(self.name).output.get(self.name) + plugin = get_plugin('variable') + getter = getattr(plugin, 'get' if self._persisted else 'mget') + return getter(self.name).output.get(self.name) def set(self, value: Any): - self._set_action(**{self.name: value}) + plugin = get_plugin('variable') + setter = getattr(plugin, 'set' if self._persisted else 'mset') + setter(**{self.name: value}) # vim:sw=4:ts=4:et: