diff options
| -rw-r--r-- | generalresearch/models/thl/product.py | 26 | ||||
| -rw-r--r-- | tests/models/thl/test_product.py | 12 |
2 files changed, 37 insertions, 1 deletions
diff --git a/generalresearch/models/thl/product.py b/generalresearch/models/thl/product.py index 8f8cc4b..16288f6 100644 --- a/generalresearch/models/thl/product.py +++ b/generalresearch/models/thl/product.py @@ -84,6 +84,12 @@ if TYPE_CHECKING: from generalresearch.models.thl.ledger import LedgerAccount +PRODUCT_BALANCES_METRICS_CACHE_KEY = "metrics:product_balances" +PRODUCT_USER_WALLET_BALANCES_METRICS_CACHE_KEY = ( + "metrics:product_user_wallet_balances" +) + + # fmt: off GRS_SKINS = [ "mmfwcl.com", "profile.generalresearch.com", @@ -1475,8 +1481,26 @@ class Product(BaseModel, validate_assignment=True): # a delay in the incite merge file not being built yet. # bal = thl_lm.get_account_balance_timerange(time_end=) + assert self.balance is not None rc = redis_config.create_redis_client() - rc.set(name=self.cache_key, value=self.model_dump_json(), ex=timedelta(days=3)) + with rc.pipeline() as pipe: + pipe.set( + name=self.cache_key, + value=self.model_dump_json(), + ex=timedelta(days=3), + ) + pipe.hset( + name=PRODUCT_BALANCES_METRICS_CACHE_KEY, + key=self.uuid, + value=self.balance.model_dump_json(), + ) + if self.user_wallet_balance is not None: + pipe.hset( + name=PRODUCT_USER_WALLET_BALANCES_METRICS_CACHE_KEY, + key=self.uuid, + value=self.user_wallet_balance.model_dump_json(), + ) + pipe.execute() def determine_bp_payment(self, thl_net: Decimal) -> Decimal: """ diff --git a/tests/models/thl/test_product.py b/tests/models/thl/test_product.py index 5a3e488..e45bd42 100644 --- a/tests/models/thl/test_product.py +++ b/tests/models/thl/test_product.py @@ -1174,6 +1174,18 @@ class TestProductCache: assert p1.balance.retainer_usd_str == "$0.17" assert p1.balance.available_balance_usd_str == "$0.54" + from generalresearch.models.thl.product import ( + PRODUCT_BALANCES_METRICS_CACHE_KEY, + ) + + metrics_balance_json = rc.hget( + PRODUCT_BALANCES_METRICS_CACHE_KEY, + product.uuid, + ) + assert isinstance(metrics_balance_json, str) + metrics_balance = ProductBalances.model_validate_json(metrics_balance_json) + assert metrics_balance == p1.balance + def test_neg_balance_cache( self, product: Product, |
