Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgazelka committed Jan 18, 2025
1 parent ea00b94 commit 818ed09
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,32 @@ use smart_cache_macro::cached;

// First run of your program:
#[cached]
fn expensive_computation(x: String, y: i32) -> String {
fn expensive_computation(x: &str, y: i32) -> String {
println!("Computing..."); // We'll see when the function actually runs
std::thread::sleep(std::time::Duration::from_secs(3));
format!("example computation {}_{}", x, y)
}

fn main() {
// First call: takes 3 seconds, prints "Computing..."
let result1 = expensive_computation("hello".to_string(), 2);
let result1 = expensive_computation("hello", 2);
println!("{}", result1); // "example computation hello_2"

// Second call: instant, no "Computing..." message
let result2 = expensive_computation("hello".to_string(), 2);
let result2 = expensive_computation("hello", 2);
println!("{}", result2); // "example computation hello_2"
}

// If you restart your program, the cache persists:
fn main() {
// Still instant, no "Computing..." message, uses cached result from previous run
let result = expensive_computation("hello".to_string(), 2);
let result = expensive_computation("hello", 2);
println!("{}", result); // "example computation hello_2"
}

// If you modify the function, the cache invalidates automatically:
#[cached]
fn expensive_computation(x: String, y: i32) -> String {
fn expensive_computation(x: &str, y: i32) -> String {
println!("Computing...");
std::thread::sleep(std::time::Duration::from_secs(3));
format!("new computation {}_{}", x, y) // Changed the string
Expand All @@ -84,7 +84,7 @@ fn expensive_computation(x: String, y: i32) -> String {
fn main() {
// Cache was invalidated due to function change
// Takes 3 seconds and prints "Computing..." again
let result = expensive_computation("hello".to_string(), 2);
let result = expensive_computation("hello", 2);
println!("{}", result); // "new computation hello_2"
}
```
Expand Down

0 comments on commit 818ed09

Please sign in to comment.