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.
This commit is contained in:
Fabio Manganiello 2022-06-07 00:04:29 +02:00
parent 115bed7d8b
commit 35cb72f5aa
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -171,15 +171,17 @@ class Variable:
def __init__(self, name: str, persisted: bool = True): def __init__(self, name: str, persisted: bool = True):
self.name = name self.name = name
plugin = get_plugin('variable') self._persisted = persisted
self._get_action = getattr(plugin, 'get' if persisted else 'mget')
self._set_action = getattr(plugin, 'set' if persisted else 'mset')
def get(self) -> Optional[Any]: 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): 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: # vim:sw=4:ts=4:et: