sabuhish / fastapi-mail

Fastapi mail system sending mails(individual, bulk) attachments(individual, bulk)
https://sabuhish.github.io/fastapi-mail/
MIT License
673 stars 82 forks source link

how to cache template #202

Open maxiaojunwolf opened 10 months ago

maxiaojunwolf commented 10 months ago

fastapi 0.98 fastapi-mail 1.31

class ConnectionConfig(Settings): MAIL_USERNAME: str MAIL_PASSWORD: str MAIL_PORT: int MAIL_SERVER: str MAIL_STARTTLS: bool MAIL_SSL_TLS: bool MAIL_DEBUG: conint(gt=-1, lt=2) = 0 # type: ignore MAIL_FROM: EmailStr MAIL_FROM_NAME: Optional[str] = None TEMPLATE_FOLDER: Optional[DirectoryPath] = None SUPPRESS_SEND: conint(gt=-1, lt=2) = 0 # type: ignore USE_CREDENTIALS: bool = True VALIDATE_CERTS: bool = True TIMEOUT: int = DEFAULT_TIMEOUT

def template_engine(self) -> Environment:
    """
    Return template environment
    """
    folder = self.TEMPLATE_FOLDER
    if not folder:
        raise ValueError(
            "Class initialization did not include a ``TEMPLATE_FOLDER`` ``PathLike`` object."
        )
    **# in here ,a new template_env object created everytime, so the template_env.cache**
    template_env = Environment(loader=FileSystemLoader(folder))
    return template_env

# I hope the template can be cached,Avoiding failure of multiple processes opening the same template file