uorm 0.9.4

Rust 下的轻量级 ORM 框架,借鉴了 Java MyBatis 的设计理念,强调 SQL 与业务逻辑分离。它结合 Rust 的类型系统与宏机制,支持编写原生 SQL 并自动映射结果,兼容 async/await,兼顾性能与可控性。
Documentation
use crate::tpl::ast::AstNode;
use crate::tpl::parser::parse_template;
use dashmap::DashMap;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::sync::{Arc, LazyLock};

#[derive(Clone)]
pub struct CachedTemplate {
    pub ast: Arc<Vec<AstNode>>,
    pub content_hash: u64,
}

/// Cache for parsed template ASTs.
pub(crate) static TEMPLATE_CACHE: LazyLock<DashMap<String, CachedTemplate>> =
    LazyLock::new(DashMap::new);

pub(crate) fn get_ast(template_name: &str, template_content: &str) -> Arc<Vec<AstNode>> {
    let mut hasher = DefaultHasher::new();
    template_content.hash(&mut hasher);
    let new_hash = hasher.finish();

    if let Some(cached) = TEMPLATE_CACHE.get(template_name)
        && cached.content_hash == new_hash
    {
        return cached.ast.clone();
    }

    let ast = Arc::new(parse_template(template_content));
    TEMPLATE_CACHE.insert(
        template_name.to_string(),
        CachedTemplate {
            ast: ast.clone(),
            content_hash: new_hash,
        },
    );
    ast
}