aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstuppie2026-10-05 12:38:37 -0600
committerstuppie2026-10-05 12:38:37 -0600
commit8c6b9cd45fbeb0400f51356e4540943c8d21a886 (patch)
tree924aa437bc8b94e552917634213b81049400684b
parente5a3e308a52496ccdc7faf90eec3d9e6aa9918d0 (diff)
downloadgeneralresearch-8c6b9cd45fbeb0400f51356e4540943c8d21a886.tar.gz
generalresearch-8c6b9cd45fbeb0400f51356e4540943c8d21a886.zip
cache balance and user_wallet_balance in redis hash separately for easier bulk retrieval
-rw-r--r--generalresearch/models/thl/product.py26
-rw-r--r--tests/models/thl/test_product.py12
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,