This commit is contained in:
hjlarry 2026-01-18 10:12:43 +08:00
parent 224f426765
commit 51c96b0b7e
2 changed files with 20 additions and 23 deletions

View File

@ -71,6 +71,10 @@ class WorkflowComment(Base):
return self._created_by_account_cache
return db.session.get(Account, self.created_by)
def cache_created_by_account(self, account: Account | None) -> None:
"""Cache creator account to avoid extra queries."""
self._created_by_account_cache = account
@property
def resolved_by_account(self):
"""Get resolver account."""
@ -80,6 +84,10 @@ class WorkflowComment(Base):
return db.session.get(Account, self.resolved_by)
return None
def cache_resolved_by_account(self, account: Account | None) -> None:
"""Cache resolver account to avoid extra queries."""
self._resolved_by_account_cache = account
@property
def reply_count(self):
"""Get reply count."""
@ -152,6 +160,10 @@ class WorkflowCommentReply(Base):
return self._created_by_account_cache
return db.session.get(Account, self.created_by)
def cache_created_by_account(self, account: Account | None) -> None:
"""Cache creator account to avoid extra queries."""
self._created_by_account_cache = account
class WorkflowCommentMention(Base):
"""Workflow comment mention model.
@ -192,3 +204,7 @@ class WorkflowCommentMention(Base):
if hasattr(self, "_mentioned_user_account_cache"):
return self._mentioned_user_account_cache
return db.session.get(Account, self.mentioned_user_id)
def cache_mentioned_user_account(self, account: Account | None) -> None:
"""Cache mentioned account to avoid extra queries."""
self._mentioned_user_account_cache = account

View File

@ -1,6 +1,5 @@
import logging
from collections.abc import Sequence
from typing import Protocol, cast
from sqlalchemy import desc, select
from sqlalchemy.orm import Session, selectinload
@ -15,19 +14,6 @@ from models.account import Account
logger = logging.getLogger(__name__)
class _CommentAccountCache(Protocol):
_created_by_account_cache: Account | None
_resolved_by_account_cache: Account | None
class _ReplyAccountCache(Protocol):
_created_by_account_cache: Account | None
class _MentionAccountCache(Protocol):
_mentioned_user_account_cache: Account | None
class WorkflowCommentService:
"""Service for managing workflow comments."""
@ -79,17 +65,12 @@ class WorkflowCommentService:
# Cache accounts on objects
for comment in comments:
comment_cache = cast(_CommentAccountCache, comment)
comment_cache._created_by_account_cache = account_map.get(comment.created_by)
comment_cache._resolved_by_account_cache = (
account_map.get(comment.resolved_by) if comment.resolved_by else None
)
comment.cache_created_by_account(account_map.get(comment.created_by))
comment.cache_resolved_by_account(account_map.get(comment.resolved_by) if comment.resolved_by else None)
for reply in comment.replies:
reply_cache = cast(_ReplyAccountCache, reply)
reply_cache._created_by_account_cache = account_map.get(reply.created_by)
reply.cache_created_by_account(account_map.get(reply.created_by))
for mention in comment.mentions:
mention_cache = cast(_MentionAccountCache, mention)
mention_cache._mentioned_user_account_cache = account_map.get(mention.mentioned_user_id)
mention.cache_mentioned_user_account(account_map.get(mention.mentioned_user_id))
@staticmethod
def get_comment(tenant_id: str, app_id: str, comment_id: str, session: Session | None = None) -> WorkflowComment: