From 0c85093937829c0c626456a00a30d0a23c8cbcfd Mon Sep 17 00:00:00 2001 From: Vic Bartel Date: Mon, 11 Apr 2022 10:35:39 +0200 Subject: [PATCH] Add memoize implementation for settings purposes --- openpype/modules/shotgrid/lib/settings.py | 6 +++--- openpype/modules/shotgrid/lib/tools.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 openpype/modules/shotgrid/lib/tools.py diff --git a/openpype/modules/shotgrid/lib/settings.py b/openpype/modules/shotgrid/lib/settings.py index 4c21501b99..4a772de5b7 100644 --- a/openpype/modules/shotgrid/lib/settings.py +++ b/openpype/modules/shotgrid/lib/settings.py @@ -1,9 +1,9 @@ import os -# from functools import lru_cache from pymongo import MongoClient from openpype.api import get_system_settings, get_project_settings from openpype.modules.shotgrid.lib.const import MODULE_NAME +from openpype.modules.shotgrid.lib.tools import memoize def get_project_list(): @@ -13,12 +13,12 @@ def get_project_list(): return db.list_collection_names() -# @lru_cache(maxsize=64) +@memoize def get_shotgrid_project_settings(project): return get_project_settings(project).get(MODULE_NAME, {}) -# @lru_cache(maxsize=64) +@memoize def get_shotgrid_settings(): return get_system_settings().get("modules", {}).get(MODULE_NAME, {}) diff --git a/openpype/modules/shotgrid/lib/tools.py b/openpype/modules/shotgrid/lib/tools.py new file mode 100644 index 0000000000..6305e9ca50 --- /dev/null +++ b/openpype/modules/shotgrid/lib/tools.py @@ -0,0 +1,16 @@ +from functools import wraps + + +def memoize(function): + memo = {} + + @wraps(function) + def wrapper(*args): + try: + return memo[args] + except KeyError: + rv = function(*args) + memo[args] = rv + return rv + + return wrapper