romance_core/addon/
cache.rs1use crate::addon::Addon;
2use anyhow::Result;
3use std::path::Path;
4
5pub struct CacheAddon;
6
7impl Addon for CacheAddon {
8 fn name(&self) -> &str {
9 "cache"
10 }
11
12 fn check_prerequisites(&self, project_root: &Path) -> Result<()> {
13 super::check_romance_project(project_root)
14 }
15
16 fn is_already_installed(&self, project_root: &Path) -> bool {
17 project_root.join("backend/src/cache.rs").exists()
18 }
19
20 fn install(&self, project_root: &Path) -> Result<()> {
21 install_cache(project_root)
22 }
23
24 fn uninstall(&self, project_root: &Path) -> Result<()> {
25 use colored::Colorize;
26
27 println!("{}", "Uninstalling caching layer...".bold());
28
29 if super::remove_file_if_exists(&project_root.join("backend/src/cache.rs"))? {
31 println!(" {} backend/src/cache.rs", "delete".red());
32 }
33
34 super::remove_mod_from_main(project_root, "cache")?;
36
37 super::remove_feature_flag(project_root, "cache")?;
39
40 crate::ai_context::regenerate(project_root).ok();
42
43 println!();
44 println!(
45 "{}",
46 "Caching layer uninstalled successfully.".green().bold()
47 );
48
49 Ok(())
50 }
51}
52
53fn install_cache(project_root: &Path) -> Result<()> {
54 use crate::template::TemplateEngine;
55 use crate::utils;
56 use colored::Colorize;
57 use tera::Context;
58
59 println!("{}", "Installing caching layer...".bold());
60
61 let engine = TemplateEngine::new()?;
62 let ctx = Context::new();
63
64 let content = engine.render("addon/cache/cache.rs.tera", &ctx)?;
66 utils::write_file(&project_root.join("backend/src/cache.rs"), &content)?;
67 println!(" {} backend/src/cache.rs", "create".green());
68
69 super::add_mod_to_main(project_root, "cache")?;
71
72 crate::generator::auth::insert_cargo_dependency(
74 &project_root.join("backend/Cargo.toml"),
75 &[(
76 "redis",
77 r#"{ version = "0.27", features = ["tokio-comp", "connection-manager"] }"#,
78 )],
79 )?;
80
81 super::append_env_var(
83 &project_root.join("backend/.env"),
84 "REDIS_URL=redis://127.0.0.1:6379",
85 )?;
86 super::append_env_var(
87 &project_root.join("backend/.env.example"),
88 "REDIS_URL=redis://127.0.0.1:6379",
89 )?;
90
91 super::update_feature_flag(project_root, "cache", true)?;
93
94 println!();
95 println!(
96 "{}",
97 "Caching layer installed successfully!".green().bold()
98 );
99 println!(" Configure Redis connection in backend/.env");
100 println!(" Use CacheService::new()? to create an instance.");
101 println!(" Example: cache.set(\"key\", &value, 300).await?");
102
103 Ok(())
104}