cache_disable/
cache_disable.rs

1use json_eval_rs::JSONEval;
2use serde_json::json;
3
4fn main() {
5    let schema = json!({
6        "type": "object",
7        "properties": {
8            "price": {
9                "type": "number"
10            },
11            "tax": {
12                "type": "number",
13                "value": {
14                    "$evaluation": {
15                        "*": [
16                            { "$ref": "#/properties/price" },
17                            0.1
18                        ]
19                    }
20                }
21            },
22            "total": {
23                "type": "number",
24                "value": {
25                    "$evaluation": {
26                        "+": [
27                            { "$ref": "#/properties/price" },
28                            { "$ref": "#/properties/tax" }
29                        ]
30                    }
31                }
32            }
33        }
34    });
35
36    let schema_str = serde_json::to_string(&schema).unwrap();
37    
38    println!("=== Example 1: With Caching (Default) ===");
39    {
40        let data = json!({ "price": 100 });
41        let data_str = serde_json::to_string(&data).unwrap();
42        
43        let mut eval = JSONEval::new(&schema_str, None, Some(&data_str)).unwrap();
44        
45        println!("Cache enabled: {}", eval.is_cache_enabled());
46        println!("Initial cache size: {}", eval.cache_len());
47        
48        eval.evaluate(&data_str, None).unwrap();
49        
50        println!("After evaluation cache size: {}", eval.cache_len());
51        let stats = eval.cache_stats();
52        println!("Cache stats: {}", stats);
53    }
54    
55    println!("\n=== Example 2: Without Caching (Web API Mode) ===");
56    {
57        let data = json!({ "price": 200 });
58        let data_str = serde_json::to_string(&data).unwrap();
59        
60        let mut eval = JSONEval::new(&schema_str, None, Some(&data_str)).unwrap();
61        
62        // Disable caching for single-use web API scenario
63        eval.disable_cache();
64        
65        println!("Cache enabled: {}", eval.is_cache_enabled());
66        println!("Initial cache size: {}", eval.cache_len());
67        
68        eval.evaluate(&data_str, None).unwrap();
69        
70        println!("After evaluation cache size: {}", eval.cache_len());
71        let stats = eval.cache_stats();
72        println!("Cache stats: {}", stats);
73        
74        println!("\n✅ No cache overhead - perfect for web APIs!");
75    }
76    
77    println!("\n=== Example 3: Re-enabling Cache ===");
78    {
79        let data = json!({ "price": 300 });
80        let data_str = serde_json::to_string(&data).unwrap();
81        
82        let mut eval = JSONEval::new(&schema_str, None, Some(&data_str)).unwrap();
83        
84        // Disable then re-enable
85        eval.disable_cache();
86        eval.enable_cache();
87        
88        println!("Cache enabled: {}", eval.is_cache_enabled());
89        eval.evaluate(&data_str, None).unwrap();
90        
91        println!("Cache size after evaluation: {}", eval.cache_len());
92        println!("\n✅ Cache can be toggled as needed!");
93    }
94}