Skip to content
Snippets Groups Projects

#397: Replace singletons with @functools.lru_cache() decorator

Merged #397: Replace singletons with @functools.lru_cache() decorator
Merged Moul requested to merge 397_rm_singleton into main
22 files
+ 152
191
Compare changes
  • Side-by-side
  • Inline
Files
22
+ 13
30
@@ -13,42 +13,25 @@
@@ -13,42 +13,25 @@
# You should have received a copy of the GNU Affero General Public License
# You should have received a copy of the GNU Affero General Public License
# along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
# along with Silkaj. If not, see <https://www.gnu.org/licenses/>.
from duniterpy.api.bma import blockchain
import functools
from silkaj.network_tools import ClientInstance
class BlockchainParams:
__instance = None
def __new__(cls):
from duniterpy.api.bma import blockchain
if BlockchainParams.__instance is None:
BlockchainParams.__instance = object.__new__(cls)
return BlockchainParams.__instance
def __init__(self):
self.params = self.get_params()
def get_params(self):
client = ClientInstance().client
return client(blockchain.parameters)
 
from silkaj.network_tools import client_instance
class HeadBlock:
__instance = None
def __new__(cls):
@functools.lru_cache(maxsize=1)
if HeadBlock.__instance is None:
def get_blockchain_parameters():
HeadBlock.__instance = object.__new__(cls)
client = client_instance()
return HeadBlock.__instance
return client(blockchain.parameters)
def __init__(self):
self.head_block = self.get_head()
def get_head(self):
@functools.lru_cache(maxsize=1)
client = ClientInstance().client
def get_head_block():
return client(blockchain.current)
client = client_instance()
 
return client(blockchain.current)
 
@functools.lru_cache(maxsize=1)
def get_currency():
def get_currency():
return (HeadBlock().head_block)["currency"]
return get_head_block()["currency"]
Loading