pub struct CacheManager { /* private fields */ }Expand description
Cache management utilities
Implementations§
Source§impl CacheManager
impl CacheManager
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Create a new cache manager with default settings
Examples found in repository?
examples/batch_operations_demo.rs (line 14)
10fn main() {
11 println!("=== Batch Operations Demonstration ===\n");
12
13 // Create a cache manager for demonstration
14 let cache_manager = CacheManager::new().expect("Failed to create cache manager");
15
16 println!("=== Setting up Batch Operations Manager =====");
17 let batch_ops = BatchOperations::new(cache_manager)
18 .with_parallel(false) // Use sequential for deterministic demo output
19 .with_retry_config(2, Duration::from_millis(500));
20
21 println!("Batch operations manager configured:");
22 println!(" - Parallel processing: disabled (for demo)");
23 println!(" - Max retries: 2");
24 println!(" - Retry delay: 500ms");
25
26 // Demonstrate cache setup with sample data
27 println!("\n=== Sample Data Setup ========================");
28 setup_sample_cachedata(&batch_ops);
29
30 // Demonstrate batch statistics
31 println!("\n=== Cache Statistics ==========================");
32 demonstrate_cache_statistics(&batch_ops);
33
34 // Demonstrate batch processing
35 println!("\n=== Batch Processing =========================");
36 demonstrate_batch_processing(&batch_ops);
37
38 // Demonstrate selective cleanup
39 println!("\n=== Selective Cache Cleanup ==================");
40 demonstrate_selective_cleanup(&batch_ops);
41
42 // Show final cache state
43 println!("\n=== Final Cache State =========================");
44 show_final_cache_state(&batch_ops);
45
46 // Performance considerations
47 println!("\n=== Performance Considerations ================");
48 demonstrate_performance_features();
49
50 println!("\n=== Batch Operations Demo Complete ===========");
51}Sourcepub fn with_config(
cachedir: PathBuf,
cache_size: usize,
ttl_seconds: u64,
) -> Self
pub fn with_config( cachedir: PathBuf, cache_size: usize, ttl_seconds: u64, ) -> Self
Create a new cache manager with custom settings
Sourcepub fn get(&self, key: &CacheKey) -> Result<Option<Dataset>>
pub fn get(&self, key: &CacheKey) -> Result<Option<Dataset>>
Get a dataset from cache using CacheKey
Sourcepub fn put(&self, key: &CacheKey, dataset: &Dataset) -> Result<()>
pub fn put(&self, key: &CacheKey, dataset: &Dataset) -> Result<()>
Put a dataset into cache using CacheKey
Sourcepub fn with_full_config(
cachedir: PathBuf,
cache_size: usize,
ttl_seconds: u64,
max_cache_size: u64,
offline_mode: bool,
) -> Self
pub fn with_full_config( cachedir: PathBuf, cache_size: usize, ttl_seconds: u64, max_cache_size: u64, offline_mode: bool, ) -> Self
Create a cache manager with comprehensive configuration
Examples found in repository?
examples/cache_management_demo.rs (lines 49-55)
10fn main() {
11 println!("=== Enhanced Cache Management Demonstration ===\n");
12
13 // Demonstrate platform-specific cache directory detection
14 println!("=== Platform-Specific Cache Directory =========");
15 match get_cachedir() {
16 Ok(cachedir) => {
17 println!("Default cache directory: {}", cachedir.display());
18 println!("Platform: {}", std::env::consts::OS);
19 }
20 Err(e) => {
21 println!("Error getting cache directory: {e}");
22 }
23 }
24 println!();
25
26 // Demonstrate environment variable override
27 println!("=== Environment Variable Configuration =========");
28 println!("Set SCIRS2_CACHE_DIR to override default cache location");
29 println!("Set SCIRS2_OFFLINE=true to enable offline mode");
30 if let Ok(cache_env) = std::env::var("SCIRS2_CACHE_DIR") {
31 println!("Custom cache directory: {cache_env}");
32 } else {
33 println!("Using default cache directory");
34 }
35
36 if let Ok(offline_env) = std::env::var("SCIRS2_OFFLINE") {
37 println!("Offline mode: {offline_env}");
38 } else {
39 println!("Offline mode: Not set (defaults to false)");
40 }
41 println!();
42
43 // Create a temporary cache for demonstration
44 let tempdir = tempfile::tempdir().unwrap();
45 let demo_cachedir = tempdir.path().join("demo_cache");
46
47 // Demonstrate cache with size limits
48 println!("=== Cache with Size Limits =====================");
49 let mut cache_manager = CacheManager::with_full_config(
50 demo_cachedir.clone(),
51 50, // 50 items in memory cache
52 3600, // 1 hour TTL
53 1024 * 1024, // 1MB disk cache limit
54 false, // Not in offline mode
55 );
56
57 println!("Created cache with 1MB size limit");
58 println!("Cache directory: {}", demo_cachedir.display());
59
60 // Add some test data to the cache
61 let cache = DatasetCache::with_full_config(demo_cachedir.clone(), 50, 3600, 1024 * 1024, false);
62
63 // Write several files of different sizes
64 let smalldata = vec![0u8; 1024]; // 1KB
65 let mediumdata = vec![1u8; 10240]; // 10KB
66 let largedata = vec![2u8; 102400]; // 100KB
67
68 cache.write_cached("small_file.dat", &smalldata).unwrap();
69 cache.write_cached("medium_file.dat", &mediumdata).unwrap();
70 cache.write_cached("large_file.dat", &largedata).unwrap();
71
72 println!("Added test files to cache");
73 println!();
74
75 // Demonstrate basic cache statistics
76 println!("=== Basic Cache Statistics ====================");
77 let basic_stats = cache_manager.get_stats();
78 println!("Files: {}", basic_stats.file_count);
79 println!("Total size: {}", basic_stats.formatted_size());
80 println!();
81
82 // Demonstrate detailed cache statistics
83 println!("=== Detailed Cache Statistics ==================");
84 match cache_manager.get_detailed_stats() {
85 Ok(detailed_stats) => {
86 println!("Cache Directory: {}", detailed_stats.cachedir.display());
87 println!(
88 "Total Size: {} ({} files)",
89 detailed_stats.formatted_size(),
90 detailed_stats.file_count
91 );
92 println!("Max Size: {}", detailed_stats.formatted_max_size());
93 println!("Usage: {:.1}%", detailed_stats.usage_percentage() * 100.0);
94 println!(
95 "Offline Mode: {}",
96 if detailed_stats.offline_mode {
97 "Enabled"
98 } else {
99 "Disabled"
100 }
101 );
102
103 if !detailed_stats.files.is_empty() {
104 println!("\nCached Files (sorted by size):");
105 for file in &detailed_stats.files {
106 println!(
107 " {} - {} (modified {})",
108 file.name,
109 file.formatted_size(),
110 file.formatted_modified()
111 );
112 }
113 }
114 }
115 Err(e) => {
116 println!("Error getting detailed stats: {e}");
117 }
118 }
119 println!();
120
121 // Demonstrate cache management operations
122 println!("=== Cache Management Operations ===============");
123 println!("Available operations:");
124 println!("1. List cached files");
125 let cached_files = cache_manager.list_cached_files().unwrap();
126 for file in &cached_files {
127 println!(" - {file}");
128 }
129
130 println!("2. Check if specific files are cached");
131 println!(
132 " small_file.dat: {}",
133 cache_manager.is_cached("small_file.dat")
134 );
135 println!(
136 " nonexistent.dat: {}",
137 cache_manager.is_cached("nonexistent.dat")
138 );
139
140 println!("3. Remove specific file");
141 cache_manager.remove("medium_file.dat").unwrap();
142 println!(" Removed medium_file.dat");
143 println!(
144 " Files remaining: {}",
145 cache_manager.list_cached_files().unwrap().len()
146 );
147 println!();
148
149 // Demonstrate offline mode
150 println!("=== Offline Mode Configuration ================");
151 println!("Current offline mode: {}", cache_manager.is_offline());
152 cache_manager.set_offline_mode(true);
153 println!("Enabled offline mode: {}", cache_manager.is_offline());
154 cache_manager.set_offline_mode(false);
155 println!("Disabled offline mode: {}", cache_manager.is_offline());
156 println!();
157
158 // Demonstrate cache size management
159 println!("=== Cache Size Management =====================");
160 println!(
161 "Current max cache size: {} bytes",
162 cache_manager.max_cache_size()
163 );
164 cache_manager.set_max_cache_size(512 * 1024); // 512KB
165 println!(
166 "Set max cache size to: {} bytes",
167 cache_manager.max_cache_size()
168 );
169
170 // Add a large file that would exceed the new limit
171 let very_largedata = vec![3u8; 400 * 1024]; // 400KB
172 cache
173 .write_cached("very_large_file.dat", &very_largedata)
174 .unwrap();
175
176 let final_stats = cache_manager.get_detailed_stats().unwrap();
177 println!(
178 "Final cache size: {} (should be within limit)",
179 final_stats.formatted_size()
180 );
181 println!(
182 "Final usage: {:.1}%",
183 final_stats.usage_percentage() * 100.0
184 );
185 println!();
186
187 // Demonstrate cache cleanup
188 println!("=== Cache Cleanup ==============================");
189 println!(
190 "Files before cleanup: {}",
191 cache_manager.list_cached_files().unwrap().len()
192 );
193 cache_manager.cleanup_old_files(100 * 1024).unwrap(); // Clean up to fit 100KB
194 let cleanup_stats = cache_manager.get_detailed_stats().unwrap();
195 println!("Files after cleanup: {}", cleanup_stats.file_count);
196 println!("Size after cleanup: {}", cleanup_stats.formatted_size());
197 println!();
198
199 // Demonstrate cache report
200 println!("=== Complete Cache Report ======================");
201 cache_manager.print_cache_report().unwrap();
202 println!();
203
204 // Clear all cache data
205 println!("=== Cache Clearing =============================");
206 cache_manager.clear_all().unwrap();
207 let empty_stats = cache_manager.get_stats();
208 println!("Files after clearing: {}", empty_stats.file_count);
209 println!("Size after clearing: {}", empty_stats.formatted_size());
210 println!();
211
212 // Demonstrate configuration examples
213 println!("=== Configuration Examples =====================");
214 println!("Example configurations for different use cases:");
215 println!();
216
217 println!("1. Development (small cache, frequent cleanup):");
218 println!(" CacheManager::with_full_config(cachedir, 20, 1800, 50*1024*1024, false)");
219 println!(" - 20 items in memory, 30 min TTL, 50MB disk limit");
220 println!();
221
222 println!("2. Production (large cache, longer retention):");
223 println!(" CacheManager::with_full_config(cachedir, 500, 86400, 1024*1024*1024, false)");
224 println!(" - 500 items in memory, 24 hour TTL, 1GB disk limit");
225 println!();
226
227 println!("3. Offline environment:");
228 println!(" CacheManager::with_full_config(cachedir, 100, 3600, 0, true)");
229 println!(" - Offline mode enabled, unlimited disk cache");
230 println!();
231
232 println!("4. Memory-constrained (minimal cache):");
233 println!(" CacheManager::with_full_config(cachedir, 10, 900, 10*1024*1024, false)");
234 println!(" - 10 items in memory, 15 min TTL, 10MB disk limit");
235 println!();
236
237 println!("=== Cache Management Demo Complete =============");
238}Sourcepub fn get_stats(&self) -> CacheStats
pub fn get_stats(&self) -> CacheStats
Get basic cache statistics
Examples found in repository?
examples/cache_management_demo.rs (line 77)
10fn main() {
11 println!("=== Enhanced Cache Management Demonstration ===\n");
12
13 // Demonstrate platform-specific cache directory detection
14 println!("=== Platform-Specific Cache Directory =========");
15 match get_cachedir() {
16 Ok(cachedir) => {
17 println!("Default cache directory: {}", cachedir.display());
18 println!("Platform: {}", std::env::consts::OS);
19 }
20 Err(e) => {
21 println!("Error getting cache directory: {e}");
22 }
23 }
24 println!();
25
26 // Demonstrate environment variable override
27 println!("=== Environment Variable Configuration =========");
28 println!("Set SCIRS2_CACHE_DIR to override default cache location");
29 println!("Set SCIRS2_OFFLINE=true to enable offline mode");
30 if let Ok(cache_env) = std::env::var("SCIRS2_CACHE_DIR") {
31 println!("Custom cache directory: {cache_env}");
32 } else {
33 println!("Using default cache directory");
34 }
35
36 if let Ok(offline_env) = std::env::var("SCIRS2_OFFLINE") {
37 println!("Offline mode: {offline_env}");
38 } else {
39 println!("Offline mode: Not set (defaults to false)");
40 }
41 println!();
42
43 // Create a temporary cache for demonstration
44 let tempdir = tempfile::tempdir().unwrap();
45 let demo_cachedir = tempdir.path().join("demo_cache");
46
47 // Demonstrate cache with size limits
48 println!("=== Cache with Size Limits =====================");
49 let mut cache_manager = CacheManager::with_full_config(
50 demo_cachedir.clone(),
51 50, // 50 items in memory cache
52 3600, // 1 hour TTL
53 1024 * 1024, // 1MB disk cache limit
54 false, // Not in offline mode
55 );
56
57 println!("Created cache with 1MB size limit");
58 println!("Cache directory: {}", demo_cachedir.display());
59
60 // Add some test data to the cache
61 let cache = DatasetCache::with_full_config(demo_cachedir.clone(), 50, 3600, 1024 * 1024, false);
62
63 // Write several files of different sizes
64 let smalldata = vec![0u8; 1024]; // 1KB
65 let mediumdata = vec![1u8; 10240]; // 10KB
66 let largedata = vec![2u8; 102400]; // 100KB
67
68 cache.write_cached("small_file.dat", &smalldata).unwrap();
69 cache.write_cached("medium_file.dat", &mediumdata).unwrap();
70 cache.write_cached("large_file.dat", &largedata).unwrap();
71
72 println!("Added test files to cache");
73 println!();
74
75 // Demonstrate basic cache statistics
76 println!("=== Basic Cache Statistics ====================");
77 let basic_stats = cache_manager.get_stats();
78 println!("Files: {}", basic_stats.file_count);
79 println!("Total size: {}", basic_stats.formatted_size());
80 println!();
81
82 // Demonstrate detailed cache statistics
83 println!("=== Detailed Cache Statistics ==================");
84 match cache_manager.get_detailed_stats() {
85 Ok(detailed_stats) => {
86 println!("Cache Directory: {}", detailed_stats.cachedir.display());
87 println!(
88 "Total Size: {} ({} files)",
89 detailed_stats.formatted_size(),
90 detailed_stats.file_count
91 );
92 println!("Max Size: {}", detailed_stats.formatted_max_size());
93 println!("Usage: {:.1}%", detailed_stats.usage_percentage() * 100.0);
94 println!(
95 "Offline Mode: {}",
96 if detailed_stats.offline_mode {
97 "Enabled"
98 } else {
99 "Disabled"
100 }
101 );
102
103 if !detailed_stats.files.is_empty() {
104 println!("\nCached Files (sorted by size):");
105 for file in &detailed_stats.files {
106 println!(
107 " {} - {} (modified {})",
108 file.name,
109 file.formatted_size(),
110 file.formatted_modified()
111 );
112 }
113 }
114 }
115 Err(e) => {
116 println!("Error getting detailed stats: {e}");
117 }
118 }
119 println!();
120
121 // Demonstrate cache management operations
122 println!("=== Cache Management Operations ===============");
123 println!("Available operations:");
124 println!("1. List cached files");
125 let cached_files = cache_manager.list_cached_files().unwrap();
126 for file in &cached_files {
127 println!(" - {file}");
128 }
129
130 println!("2. Check if specific files are cached");
131 println!(
132 " small_file.dat: {}",
133 cache_manager.is_cached("small_file.dat")
134 );
135 println!(
136 " nonexistent.dat: {}",
137 cache_manager.is_cached("nonexistent.dat")
138 );
139
140 println!("3. Remove specific file");
141 cache_manager.remove("medium_file.dat").unwrap();
142 println!(" Removed medium_file.dat");
143 println!(
144 " Files remaining: {}",
145 cache_manager.list_cached_files().unwrap().len()
146 );
147 println!();
148
149 // Demonstrate offline mode
150 println!("=== Offline Mode Configuration ================");
151 println!("Current offline mode: {}", cache_manager.is_offline());
152 cache_manager.set_offline_mode(true);
153 println!("Enabled offline mode: {}", cache_manager.is_offline());
154 cache_manager.set_offline_mode(false);
155 println!("Disabled offline mode: {}", cache_manager.is_offline());
156 println!();
157
158 // Demonstrate cache size management
159 println!("=== Cache Size Management =====================");
160 println!(
161 "Current max cache size: {} bytes",
162 cache_manager.max_cache_size()
163 );
164 cache_manager.set_max_cache_size(512 * 1024); // 512KB
165 println!(
166 "Set max cache size to: {} bytes",
167 cache_manager.max_cache_size()
168 );
169
170 // Add a large file that would exceed the new limit
171 let very_largedata = vec![3u8; 400 * 1024]; // 400KB
172 cache
173 .write_cached("very_large_file.dat", &very_largedata)
174 .unwrap();
175
176 let final_stats = cache_manager.get_detailed_stats().unwrap();
177 println!(
178 "Final cache size: {} (should be within limit)",
179 final_stats.formatted_size()
180 );
181 println!(
182 "Final usage: {:.1}%",
183 final_stats.usage_percentage() * 100.0
184 );
185 println!();
186
187 // Demonstrate cache cleanup
188 println!("=== Cache Cleanup ==============================");
189 println!(
190 "Files before cleanup: {}",
191 cache_manager.list_cached_files().unwrap().len()
192 );
193 cache_manager.cleanup_old_files(100 * 1024).unwrap(); // Clean up to fit 100KB
194 let cleanup_stats = cache_manager.get_detailed_stats().unwrap();
195 println!("Files after cleanup: {}", cleanup_stats.file_count);
196 println!("Size after cleanup: {}", cleanup_stats.formatted_size());
197 println!();
198
199 // Demonstrate cache report
200 println!("=== Complete Cache Report ======================");
201 cache_manager.print_cache_report().unwrap();
202 println!();
203
204 // Clear all cache data
205 println!("=== Cache Clearing =============================");
206 cache_manager.clear_all().unwrap();
207 let empty_stats = cache_manager.get_stats();
208 println!("Files after clearing: {}", empty_stats.file_count);
209 println!("Size after clearing: {}", empty_stats.formatted_size());
210 println!();
211
212 // Demonstrate configuration examples
213 println!("=== Configuration Examples =====================");
214 println!("Example configurations for different use cases:");
215 println!();
216
217 println!("1. Development (small cache, frequent cleanup):");
218 println!(" CacheManager::with_full_config(cachedir, 20, 1800, 50*1024*1024, false)");
219 println!(" - 20 items in memory, 30 min TTL, 50MB disk limit");
220 println!();
221
222 println!("2. Production (large cache, longer retention):");
223 println!(" CacheManager::with_full_config(cachedir, 500, 86400, 1024*1024*1024, false)");
224 println!(" - 500 items in memory, 24 hour TTL, 1GB disk limit");
225 println!();
226
227 println!("3. Offline environment:");
228 println!(" CacheManager::with_full_config(cachedir, 100, 3600, 0, true)");
229 println!(" - Offline mode enabled, unlimited disk cache");
230 println!();
231
232 println!("4. Memory-constrained (minimal cache):");
233 println!(" CacheManager::with_full_config(cachedir, 10, 900, 10*1024*1024, false)");
234 println!(" - 10 items in memory, 15 min TTL, 10MB disk limit");
235 println!();
236
237 println!("=== Cache Management Demo Complete =============");
238}Sourcepub fn get_detailed_stats(&self) -> Result<DetailedCacheStats>
pub fn get_detailed_stats(&self) -> Result<DetailedCacheStats>
Get detailed cache statistics
Examples found in repository?
examples/cache_management_demo.rs (line 84)
10fn main() {
11 println!("=== Enhanced Cache Management Demonstration ===\n");
12
13 // Demonstrate platform-specific cache directory detection
14 println!("=== Platform-Specific Cache Directory =========");
15 match get_cachedir() {
16 Ok(cachedir) => {
17 println!("Default cache directory: {}", cachedir.display());
18 println!("Platform: {}", std::env::consts::OS);
19 }
20 Err(e) => {
21 println!("Error getting cache directory: {e}");
22 }
23 }
24 println!();
25
26 // Demonstrate environment variable override
27 println!("=== Environment Variable Configuration =========");
28 println!("Set SCIRS2_CACHE_DIR to override default cache location");
29 println!("Set SCIRS2_OFFLINE=true to enable offline mode");
30 if let Ok(cache_env) = std::env::var("SCIRS2_CACHE_DIR") {
31 println!("Custom cache directory: {cache_env}");
32 } else {
33 println!("Using default cache directory");
34 }
35
36 if let Ok(offline_env) = std::env::var("SCIRS2_OFFLINE") {
37 println!("Offline mode: {offline_env}");
38 } else {
39 println!("Offline mode: Not set (defaults to false)");
40 }
41 println!();
42
43 // Create a temporary cache for demonstration
44 let tempdir = tempfile::tempdir().unwrap();
45 let demo_cachedir = tempdir.path().join("demo_cache");
46
47 // Demonstrate cache with size limits
48 println!("=== Cache with Size Limits =====================");
49 let mut cache_manager = CacheManager::with_full_config(
50 demo_cachedir.clone(),
51 50, // 50 items in memory cache
52 3600, // 1 hour TTL
53 1024 * 1024, // 1MB disk cache limit
54 false, // Not in offline mode
55 );
56
57 println!("Created cache with 1MB size limit");
58 println!("Cache directory: {}", demo_cachedir.display());
59
60 // Add some test data to the cache
61 let cache = DatasetCache::with_full_config(demo_cachedir.clone(), 50, 3600, 1024 * 1024, false);
62
63 // Write several files of different sizes
64 let smalldata = vec![0u8; 1024]; // 1KB
65 let mediumdata = vec![1u8; 10240]; // 10KB
66 let largedata = vec![2u8; 102400]; // 100KB
67
68 cache.write_cached("small_file.dat", &smalldata).unwrap();
69 cache.write_cached("medium_file.dat", &mediumdata).unwrap();
70 cache.write_cached("large_file.dat", &largedata).unwrap();
71
72 println!("Added test files to cache");
73 println!();
74
75 // Demonstrate basic cache statistics
76 println!("=== Basic Cache Statistics ====================");
77 let basic_stats = cache_manager.get_stats();
78 println!("Files: {}", basic_stats.file_count);
79 println!("Total size: {}", basic_stats.formatted_size());
80 println!();
81
82 // Demonstrate detailed cache statistics
83 println!("=== Detailed Cache Statistics ==================");
84 match cache_manager.get_detailed_stats() {
85 Ok(detailed_stats) => {
86 println!("Cache Directory: {}", detailed_stats.cachedir.display());
87 println!(
88 "Total Size: {} ({} files)",
89 detailed_stats.formatted_size(),
90 detailed_stats.file_count
91 );
92 println!("Max Size: {}", detailed_stats.formatted_max_size());
93 println!("Usage: {:.1}%", detailed_stats.usage_percentage() * 100.0);
94 println!(
95 "Offline Mode: {}",
96 if detailed_stats.offline_mode {
97 "Enabled"
98 } else {
99 "Disabled"
100 }
101 );
102
103 if !detailed_stats.files.is_empty() {
104 println!("\nCached Files (sorted by size):");
105 for file in &detailed_stats.files {
106 println!(
107 " {} - {} (modified {})",
108 file.name,
109 file.formatted_size(),
110 file.formatted_modified()
111 );
112 }
113 }
114 }
115 Err(e) => {
116 println!("Error getting detailed stats: {e}");
117 }
118 }
119 println!();
120
121 // Demonstrate cache management operations
122 println!("=== Cache Management Operations ===============");
123 println!("Available operations:");
124 println!("1. List cached files");
125 let cached_files = cache_manager.list_cached_files().unwrap();
126 for file in &cached_files {
127 println!(" - {file}");
128 }
129
130 println!("2. Check if specific files are cached");
131 println!(
132 " small_file.dat: {}",
133 cache_manager.is_cached("small_file.dat")
134 );
135 println!(
136 " nonexistent.dat: {}",
137 cache_manager.is_cached("nonexistent.dat")
138 );
139
140 println!("3. Remove specific file");
141 cache_manager.remove("medium_file.dat").unwrap();
142 println!(" Removed medium_file.dat");
143 println!(
144 " Files remaining: {}",
145 cache_manager.list_cached_files().unwrap().len()
146 );
147 println!();
148
149 // Demonstrate offline mode
150 println!("=== Offline Mode Configuration ================");
151 println!("Current offline mode: {}", cache_manager.is_offline());
152 cache_manager.set_offline_mode(true);
153 println!("Enabled offline mode: {}", cache_manager.is_offline());
154 cache_manager.set_offline_mode(false);
155 println!("Disabled offline mode: {}", cache_manager.is_offline());
156 println!();
157
158 // Demonstrate cache size management
159 println!("=== Cache Size Management =====================");
160 println!(
161 "Current max cache size: {} bytes",
162 cache_manager.max_cache_size()
163 );
164 cache_manager.set_max_cache_size(512 * 1024); // 512KB
165 println!(
166 "Set max cache size to: {} bytes",
167 cache_manager.max_cache_size()
168 );
169
170 // Add a large file that would exceed the new limit
171 let very_largedata = vec![3u8; 400 * 1024]; // 400KB
172 cache
173 .write_cached("very_large_file.dat", &very_largedata)
174 .unwrap();
175
176 let final_stats = cache_manager.get_detailed_stats().unwrap();
177 println!(
178 "Final cache size: {} (should be within limit)",
179 final_stats.formatted_size()
180 );
181 println!(
182 "Final usage: {:.1}%",
183 final_stats.usage_percentage() * 100.0
184 );
185 println!();
186
187 // Demonstrate cache cleanup
188 println!("=== Cache Cleanup ==============================");
189 println!(
190 "Files before cleanup: {}",
191 cache_manager.list_cached_files().unwrap().len()
192 );
193 cache_manager.cleanup_old_files(100 * 1024).unwrap(); // Clean up to fit 100KB
194 let cleanup_stats = cache_manager.get_detailed_stats().unwrap();
195 println!("Files after cleanup: {}", cleanup_stats.file_count);
196 println!("Size after cleanup: {}", cleanup_stats.formatted_size());
197 println!();
198
199 // Demonstrate cache report
200 println!("=== Complete Cache Report ======================");
201 cache_manager.print_cache_report().unwrap();
202 println!();
203
204 // Clear all cache data
205 println!("=== Cache Clearing =============================");
206 cache_manager.clear_all().unwrap();
207 let empty_stats = cache_manager.get_stats();
208 println!("Files after clearing: {}", empty_stats.file_count);
209 println!("Size after clearing: {}", empty_stats.formatted_size());
210 println!();
211
212 // Demonstrate configuration examples
213 println!("=== Configuration Examples =====================");
214 println!("Example configurations for different use cases:");
215 println!();
216
217 println!("1. Development (small cache, frequent cleanup):");
218 println!(" CacheManager::with_full_config(cachedir, 20, 1800, 50*1024*1024, false)");
219 println!(" - 20 items in memory, 30 min TTL, 50MB disk limit");
220 println!();
221
222 println!("2. Production (large cache, longer retention):");
223 println!(" CacheManager::with_full_config(cachedir, 500, 86400, 1024*1024*1024, false)");
224 println!(" - 500 items in memory, 24 hour TTL, 1GB disk limit");
225 println!();
226
227 println!("3. Offline environment:");
228 println!(" CacheManager::with_full_config(cachedir, 100, 3600, 0, true)");
229 println!(" - Offline mode enabled, unlimited disk cache");
230 println!();
231
232 println!("4. Memory-constrained (minimal cache):");
233 println!(" CacheManager::with_full_config(cachedir, 10, 900, 10*1024*1024, false)");
234 println!(" - 10 items in memory, 15 min TTL, 10MB disk limit");
235 println!();
236
237 println!("=== Cache Management Demo Complete =============");
238}Sourcepub fn set_offline_mode(&mut self, offline: bool)
pub fn set_offline_mode(&mut self, offline: bool)
Set offline mode
Examples found in repository?
examples/cache_management_demo.rs (line 152)
10fn main() {
11 println!("=== Enhanced Cache Management Demonstration ===\n");
12
13 // Demonstrate platform-specific cache directory detection
14 println!("=== Platform-Specific Cache Directory =========");
15 match get_cachedir() {
16 Ok(cachedir) => {
17 println!("Default cache directory: {}", cachedir.display());
18 println!("Platform: {}", std::env::consts::OS);
19 }
20 Err(e) => {
21 println!("Error getting cache directory: {e}");
22 }
23 }
24 println!();
25
26 // Demonstrate environment variable override
27 println!("=== Environment Variable Configuration =========");
28 println!("Set SCIRS2_CACHE_DIR to override default cache location");
29 println!("Set SCIRS2_OFFLINE=true to enable offline mode");
30 if let Ok(cache_env) = std::env::var("SCIRS2_CACHE_DIR") {
31 println!("Custom cache directory: {cache_env}");
32 } else {
33 println!("Using default cache directory");
34 }
35
36 if let Ok(offline_env) = std::env::var("SCIRS2_OFFLINE") {
37 println!("Offline mode: {offline_env}");
38 } else {
39 println!("Offline mode: Not set (defaults to false)");
40 }
41 println!();
42
43 // Create a temporary cache for demonstration
44 let tempdir = tempfile::tempdir().unwrap();
45 let demo_cachedir = tempdir.path().join("demo_cache");
46
47 // Demonstrate cache with size limits
48 println!("=== Cache with Size Limits =====================");
49 let mut cache_manager = CacheManager::with_full_config(
50 demo_cachedir.clone(),
51 50, // 50 items in memory cache
52 3600, // 1 hour TTL
53 1024 * 1024, // 1MB disk cache limit
54 false, // Not in offline mode
55 );
56
57 println!("Created cache with 1MB size limit");
58 println!("Cache directory: {}", demo_cachedir.display());
59
60 // Add some test data to the cache
61 let cache = DatasetCache::with_full_config(demo_cachedir.clone(), 50, 3600, 1024 * 1024, false);
62
63 // Write several files of different sizes
64 let smalldata = vec![0u8; 1024]; // 1KB
65 let mediumdata = vec![1u8; 10240]; // 10KB
66 let largedata = vec![2u8; 102400]; // 100KB
67
68 cache.write_cached("small_file.dat", &smalldata).unwrap();
69 cache.write_cached("medium_file.dat", &mediumdata).unwrap();
70 cache.write_cached("large_file.dat", &largedata).unwrap();
71
72 println!("Added test files to cache");
73 println!();
74
75 // Demonstrate basic cache statistics
76 println!("=== Basic Cache Statistics ====================");
77 let basic_stats = cache_manager.get_stats();
78 println!("Files: {}", basic_stats.file_count);
79 println!("Total size: {}", basic_stats.formatted_size());
80 println!();
81
82 // Demonstrate detailed cache statistics
83 println!("=== Detailed Cache Statistics ==================");
84 match cache_manager.get_detailed_stats() {
85 Ok(detailed_stats) => {
86 println!("Cache Directory: {}", detailed_stats.cachedir.display());
87 println!(
88 "Total Size: {} ({} files)",
89 detailed_stats.formatted_size(),
90 detailed_stats.file_count
91 );
92 println!("Max Size: {}", detailed_stats.formatted_max_size());
93 println!("Usage: {:.1}%", detailed_stats.usage_percentage() * 100.0);
94 println!(
95 "Offline Mode: {}",
96 if detailed_stats.offline_mode {
97 "Enabled"
98 } else {
99 "Disabled"
100 }
101 );
102
103 if !detailed_stats.files.is_empty() {
104 println!("\nCached Files (sorted by size):");
105 for file in &detailed_stats.files {
106 println!(
107 " {} - {} (modified {})",
108 file.name,
109 file.formatted_size(),
110 file.formatted_modified()
111 );
112 }
113 }
114 }
115 Err(e) => {
116 println!("Error getting detailed stats: {e}");
117 }
118 }
119 println!();
120
121 // Demonstrate cache management operations
122 println!("=== Cache Management Operations ===============");
123 println!("Available operations:");
124 println!("1. List cached files");
125 let cached_files = cache_manager.list_cached_files().unwrap();
126 for file in &cached_files {
127 println!(" - {file}");
128 }
129
130 println!("2. Check if specific files are cached");
131 println!(
132 " small_file.dat: {}",
133 cache_manager.is_cached("small_file.dat")
134 );
135 println!(
136 " nonexistent.dat: {}",
137 cache_manager.is_cached("nonexistent.dat")
138 );
139
140 println!("3. Remove specific file");
141 cache_manager.remove("medium_file.dat").unwrap();
142 println!(" Removed medium_file.dat");
143 println!(
144 " Files remaining: {}",
145 cache_manager.list_cached_files().unwrap().len()
146 );
147 println!();
148
149 // Demonstrate offline mode
150 println!("=== Offline Mode Configuration ================");
151 println!("Current offline mode: {}", cache_manager.is_offline());
152 cache_manager.set_offline_mode(true);
153 println!("Enabled offline mode: {}", cache_manager.is_offline());
154 cache_manager.set_offline_mode(false);
155 println!("Disabled offline mode: {}", cache_manager.is_offline());
156 println!();
157
158 // Demonstrate cache size management
159 println!("=== Cache Size Management =====================");
160 println!(
161 "Current max cache size: {} bytes",
162 cache_manager.max_cache_size()
163 );
164 cache_manager.set_max_cache_size(512 * 1024); // 512KB
165 println!(
166 "Set max cache size to: {} bytes",
167 cache_manager.max_cache_size()
168 );
169
170 // Add a large file that would exceed the new limit
171 let very_largedata = vec![3u8; 400 * 1024]; // 400KB
172 cache
173 .write_cached("very_large_file.dat", &very_largedata)
174 .unwrap();
175
176 let final_stats = cache_manager.get_detailed_stats().unwrap();
177 println!(
178 "Final cache size: {} (should be within limit)",
179 final_stats.formatted_size()
180 );
181 println!(
182 "Final usage: {:.1}%",
183 final_stats.usage_percentage() * 100.0
184 );
185 println!();
186
187 // Demonstrate cache cleanup
188 println!("=== Cache Cleanup ==============================");
189 println!(
190 "Files before cleanup: {}",
191 cache_manager.list_cached_files().unwrap().len()
192 );
193 cache_manager.cleanup_old_files(100 * 1024).unwrap(); // Clean up to fit 100KB
194 let cleanup_stats = cache_manager.get_detailed_stats().unwrap();
195 println!("Files after cleanup: {}", cleanup_stats.file_count);
196 println!("Size after cleanup: {}", cleanup_stats.formatted_size());
197 println!();
198
199 // Demonstrate cache report
200 println!("=== Complete Cache Report ======================");
201 cache_manager.print_cache_report().unwrap();
202 println!();
203
204 // Clear all cache data
205 println!("=== Cache Clearing =============================");
206 cache_manager.clear_all().unwrap();
207 let empty_stats = cache_manager.get_stats();
208 println!("Files after clearing: {}", empty_stats.file_count);
209 println!("Size after clearing: {}", empty_stats.formatted_size());
210 println!();
211
212 // Demonstrate configuration examples
213 println!("=== Configuration Examples =====================");
214 println!("Example configurations for different use cases:");
215 println!();
216
217 println!("1. Development (small cache, frequent cleanup):");
218 println!(" CacheManager::with_full_config(cachedir, 20, 1800, 50*1024*1024, false)");
219 println!(" - 20 items in memory, 30 min TTL, 50MB disk limit");
220 println!();
221
222 println!("2. Production (large cache, longer retention):");
223 println!(" CacheManager::with_full_config(cachedir, 500, 86400, 1024*1024*1024, false)");
224 println!(" - 500 items in memory, 24 hour TTL, 1GB disk limit");
225 println!();
226
227 println!("3. Offline environment:");
228 println!(" CacheManager::with_full_config(cachedir, 100, 3600, 0, true)");
229 println!(" - Offline mode enabled, unlimited disk cache");
230 println!();
231
232 println!("4. Memory-constrained (minimal cache):");
233 println!(" CacheManager::with_full_config(cachedir, 10, 900, 10*1024*1024, false)");
234 println!(" - 10 items in memory, 15 min TTL, 10MB disk limit");
235 println!();
236
237 println!("=== Cache Management Demo Complete =============");
238}Sourcepub fn is_offline(&self) -> bool
pub fn is_offline(&self) -> bool
Check if in offline mode
Examples found in repository?
examples/cache_management_demo.rs (line 151)
10fn main() {
11 println!("=== Enhanced Cache Management Demonstration ===\n");
12
13 // Demonstrate platform-specific cache directory detection
14 println!("=== Platform-Specific Cache Directory =========");
15 match get_cachedir() {
16 Ok(cachedir) => {
17 println!("Default cache directory: {}", cachedir.display());
18 println!("Platform: {}", std::env::consts::OS);
19 }
20 Err(e) => {
21 println!("Error getting cache directory: {e}");
22 }
23 }
24 println!();
25
26 // Demonstrate environment variable override
27 println!("=== Environment Variable Configuration =========");
28 println!("Set SCIRS2_CACHE_DIR to override default cache location");
29 println!("Set SCIRS2_OFFLINE=true to enable offline mode");
30 if let Ok(cache_env) = std::env::var("SCIRS2_CACHE_DIR") {
31 println!("Custom cache directory: {cache_env}");
32 } else {
33 println!("Using default cache directory");
34 }
35
36 if let Ok(offline_env) = std::env::var("SCIRS2_OFFLINE") {
37 println!("Offline mode: {offline_env}");
38 } else {
39 println!("Offline mode: Not set (defaults to false)");
40 }
41 println!();
42
43 // Create a temporary cache for demonstration
44 let tempdir = tempfile::tempdir().unwrap();
45 let demo_cachedir = tempdir.path().join("demo_cache");
46
47 // Demonstrate cache with size limits
48 println!("=== Cache with Size Limits =====================");
49 let mut cache_manager = CacheManager::with_full_config(
50 demo_cachedir.clone(),
51 50, // 50 items in memory cache
52 3600, // 1 hour TTL
53 1024 * 1024, // 1MB disk cache limit
54 false, // Not in offline mode
55 );
56
57 println!("Created cache with 1MB size limit");
58 println!("Cache directory: {}", demo_cachedir.display());
59
60 // Add some test data to the cache
61 let cache = DatasetCache::with_full_config(demo_cachedir.clone(), 50, 3600, 1024 * 1024, false);
62
63 // Write several files of different sizes
64 let smalldata = vec![0u8; 1024]; // 1KB
65 let mediumdata = vec![1u8; 10240]; // 10KB
66 let largedata = vec![2u8; 102400]; // 100KB
67
68 cache.write_cached("small_file.dat", &smalldata).unwrap();
69 cache.write_cached("medium_file.dat", &mediumdata).unwrap();
70 cache.write_cached("large_file.dat", &largedata).unwrap();
71
72 println!("Added test files to cache");
73 println!();
74
75 // Demonstrate basic cache statistics
76 println!("=== Basic Cache Statistics ====================");
77 let basic_stats = cache_manager.get_stats();
78 println!("Files: {}", basic_stats.file_count);
79 println!("Total size: {}", basic_stats.formatted_size());
80 println!();
81
82 // Demonstrate detailed cache statistics
83 println!("=== Detailed Cache Statistics ==================");
84 match cache_manager.get_detailed_stats() {
85 Ok(detailed_stats) => {
86 println!("Cache Directory: {}", detailed_stats.cachedir.display());
87 println!(
88 "Total Size: {} ({} files)",
89 detailed_stats.formatted_size(),
90 detailed_stats.file_count
91 );
92 println!("Max Size: {}", detailed_stats.formatted_max_size());
93 println!("Usage: {:.1}%", detailed_stats.usage_percentage() * 100.0);
94 println!(
95 "Offline Mode: {}",
96 if detailed_stats.offline_mode {
97 "Enabled"
98 } else {
99 "Disabled"
100 }
101 );
102
103 if !detailed_stats.files.is_empty() {
104 println!("\nCached Files (sorted by size):");
105 for file in &detailed_stats.files {
106 println!(
107 " {} - {} (modified {})",
108 file.name,
109 file.formatted_size(),
110 file.formatted_modified()
111 );
112 }
113 }
114 }
115 Err(e) => {
116 println!("Error getting detailed stats: {e}");
117 }
118 }
119 println!();
120
121 // Demonstrate cache management operations
122 println!("=== Cache Management Operations ===============");
123 println!("Available operations:");
124 println!("1. List cached files");
125 let cached_files = cache_manager.list_cached_files().unwrap();
126 for file in &cached_files {
127 println!(" - {file}");
128 }
129
130 println!("2. Check if specific files are cached");
131 println!(
132 " small_file.dat: {}",
133 cache_manager.is_cached("small_file.dat")
134 );
135 println!(
136 " nonexistent.dat: {}",
137 cache_manager.is_cached("nonexistent.dat")
138 );
139
140 println!("3. Remove specific file");
141 cache_manager.remove("medium_file.dat").unwrap();
142 println!(" Removed medium_file.dat");
143 println!(
144 " Files remaining: {}",
145 cache_manager.list_cached_files().unwrap().len()
146 );
147 println!();
148
149 // Demonstrate offline mode
150 println!("=== Offline Mode Configuration ================");
151 println!("Current offline mode: {}", cache_manager.is_offline());
152 cache_manager.set_offline_mode(true);
153 println!("Enabled offline mode: {}", cache_manager.is_offline());
154 cache_manager.set_offline_mode(false);
155 println!("Disabled offline mode: {}", cache_manager.is_offline());
156 println!();
157
158 // Demonstrate cache size management
159 println!("=== Cache Size Management =====================");
160 println!(
161 "Current max cache size: {} bytes",
162 cache_manager.max_cache_size()
163 );
164 cache_manager.set_max_cache_size(512 * 1024); // 512KB
165 println!(
166 "Set max cache size to: {} bytes",
167 cache_manager.max_cache_size()
168 );
169
170 // Add a large file that would exceed the new limit
171 let very_largedata = vec![3u8; 400 * 1024]; // 400KB
172 cache
173 .write_cached("very_large_file.dat", &very_largedata)
174 .unwrap();
175
176 let final_stats = cache_manager.get_detailed_stats().unwrap();
177 println!(
178 "Final cache size: {} (should be within limit)",
179 final_stats.formatted_size()
180 );
181 println!(
182 "Final usage: {:.1}%",
183 final_stats.usage_percentage() * 100.0
184 );
185 println!();
186
187 // Demonstrate cache cleanup
188 println!("=== Cache Cleanup ==============================");
189 println!(
190 "Files before cleanup: {}",
191 cache_manager.list_cached_files().unwrap().len()
192 );
193 cache_manager.cleanup_old_files(100 * 1024).unwrap(); // Clean up to fit 100KB
194 let cleanup_stats = cache_manager.get_detailed_stats().unwrap();
195 println!("Files after cleanup: {}", cleanup_stats.file_count);
196 println!("Size after cleanup: {}", cleanup_stats.formatted_size());
197 println!();
198
199 // Demonstrate cache report
200 println!("=== Complete Cache Report ======================");
201 cache_manager.print_cache_report().unwrap();
202 println!();
203
204 // Clear all cache data
205 println!("=== Cache Clearing =============================");
206 cache_manager.clear_all().unwrap();
207 let empty_stats = cache_manager.get_stats();
208 println!("Files after clearing: {}", empty_stats.file_count);
209 println!("Size after clearing: {}", empty_stats.formatted_size());
210 println!();
211
212 // Demonstrate configuration examples
213 println!("=== Configuration Examples =====================");
214 println!("Example configurations for different use cases:");
215 println!();
216
217 println!("1. Development (small cache, frequent cleanup):");
218 println!(" CacheManager::with_full_config(cachedir, 20, 1800, 50*1024*1024, false)");
219 println!(" - 20 items in memory, 30 min TTL, 50MB disk limit");
220 println!();
221
222 println!("2. Production (large cache, longer retention):");
223 println!(" CacheManager::with_full_config(cachedir, 500, 86400, 1024*1024*1024, false)");
224 println!(" - 500 items in memory, 24 hour TTL, 1GB disk limit");
225 println!();
226
227 println!("3. Offline environment:");
228 println!(" CacheManager::with_full_config(cachedir, 100, 3600, 0, true)");
229 println!(" - Offline mode enabled, unlimited disk cache");
230 println!();
231
232 println!("4. Memory-constrained (minimal cache):");
233 println!(" CacheManager::with_full_config(cachedir, 10, 900, 10*1024*1024, false)");
234 println!(" - 10 items in memory, 15 min TTL, 10MB disk limit");
235 println!();
236
237 println!("=== Cache Management Demo Complete =============");
238}Sourcepub fn set_max_cache_size(&mut self, max_size: u64)
pub fn set_max_cache_size(&mut self, max_size: u64)
Set maximum cache size in bytes (0 for unlimited)
Examples found in repository?
examples/cache_management_demo.rs (line 164)
10fn main() {
11 println!("=== Enhanced Cache Management Demonstration ===\n");
12
13 // Demonstrate platform-specific cache directory detection
14 println!("=== Platform-Specific Cache Directory =========");
15 match get_cachedir() {
16 Ok(cachedir) => {
17 println!("Default cache directory: {}", cachedir.display());
18 println!("Platform: {}", std::env::consts::OS);
19 }
20 Err(e) => {
21 println!("Error getting cache directory: {e}");
22 }
23 }
24 println!();
25
26 // Demonstrate environment variable override
27 println!("=== Environment Variable Configuration =========");
28 println!("Set SCIRS2_CACHE_DIR to override default cache location");
29 println!("Set SCIRS2_OFFLINE=true to enable offline mode");
30 if let Ok(cache_env) = std::env::var("SCIRS2_CACHE_DIR") {
31 println!("Custom cache directory: {cache_env}");
32 } else {
33 println!("Using default cache directory");
34 }
35
36 if let Ok(offline_env) = std::env::var("SCIRS2_OFFLINE") {
37 println!("Offline mode: {offline_env}");
38 } else {
39 println!("Offline mode: Not set (defaults to false)");
40 }
41 println!();
42
43 // Create a temporary cache for demonstration
44 let tempdir = tempfile::tempdir().unwrap();
45 let demo_cachedir = tempdir.path().join("demo_cache");
46
47 // Demonstrate cache with size limits
48 println!("=== Cache with Size Limits =====================");
49 let mut cache_manager = CacheManager::with_full_config(
50 demo_cachedir.clone(),
51 50, // 50 items in memory cache
52 3600, // 1 hour TTL
53 1024 * 1024, // 1MB disk cache limit
54 false, // Not in offline mode
55 );
56
57 println!("Created cache with 1MB size limit");
58 println!("Cache directory: {}", demo_cachedir.display());
59
60 // Add some test data to the cache
61 let cache = DatasetCache::with_full_config(demo_cachedir.clone(), 50, 3600, 1024 * 1024, false);
62
63 // Write several files of different sizes
64 let smalldata = vec![0u8; 1024]; // 1KB
65 let mediumdata = vec![1u8; 10240]; // 10KB
66 let largedata = vec![2u8; 102400]; // 100KB
67
68 cache.write_cached("small_file.dat", &smalldata).unwrap();
69 cache.write_cached("medium_file.dat", &mediumdata).unwrap();
70 cache.write_cached("large_file.dat", &largedata).unwrap();
71
72 println!("Added test files to cache");
73 println!();
74
75 // Demonstrate basic cache statistics
76 println!("=== Basic Cache Statistics ====================");
77 let basic_stats = cache_manager.get_stats();
78 println!("Files: {}", basic_stats.file_count);
79 println!("Total size: {}", basic_stats.formatted_size());
80 println!();
81
82 // Demonstrate detailed cache statistics
83 println!("=== Detailed Cache Statistics ==================");
84 match cache_manager.get_detailed_stats() {
85 Ok(detailed_stats) => {
86 println!("Cache Directory: {}", detailed_stats.cachedir.display());
87 println!(
88 "Total Size: {} ({} files)",
89 detailed_stats.formatted_size(),
90 detailed_stats.file_count
91 );
92 println!("Max Size: {}", detailed_stats.formatted_max_size());
93 println!("Usage: {:.1}%", detailed_stats.usage_percentage() * 100.0);
94 println!(
95 "Offline Mode: {}",
96 if detailed_stats.offline_mode {
97 "Enabled"
98 } else {
99 "Disabled"
100 }
101 );
102
103 if !detailed_stats.files.is_empty() {
104 println!("\nCached Files (sorted by size):");
105 for file in &detailed_stats.files {
106 println!(
107 " {} - {} (modified {})",
108 file.name,
109 file.formatted_size(),
110 file.formatted_modified()
111 );
112 }
113 }
114 }
115 Err(e) => {
116 println!("Error getting detailed stats: {e}");
117 }
118 }
119 println!();
120
121 // Demonstrate cache management operations
122 println!("=== Cache Management Operations ===============");
123 println!("Available operations:");
124 println!("1. List cached files");
125 let cached_files = cache_manager.list_cached_files().unwrap();
126 for file in &cached_files {
127 println!(" - {file}");
128 }
129
130 println!("2. Check if specific files are cached");
131 println!(
132 " small_file.dat: {}",
133 cache_manager.is_cached("small_file.dat")
134 );
135 println!(
136 " nonexistent.dat: {}",
137 cache_manager.is_cached("nonexistent.dat")
138 );
139
140 println!("3. Remove specific file");
141 cache_manager.remove("medium_file.dat").unwrap();
142 println!(" Removed medium_file.dat");
143 println!(
144 " Files remaining: {}",
145 cache_manager.list_cached_files().unwrap().len()
146 );
147 println!();
148
149 // Demonstrate offline mode
150 println!("=== Offline Mode Configuration ================");
151 println!("Current offline mode: {}", cache_manager.is_offline());
152 cache_manager.set_offline_mode(true);
153 println!("Enabled offline mode: {}", cache_manager.is_offline());
154 cache_manager.set_offline_mode(false);
155 println!("Disabled offline mode: {}", cache_manager.is_offline());
156 println!();
157
158 // Demonstrate cache size management
159 println!("=== Cache Size Management =====================");
160 println!(
161 "Current max cache size: {} bytes",
162 cache_manager.max_cache_size()
163 );
164 cache_manager.set_max_cache_size(512 * 1024); // 512KB
165 println!(
166 "Set max cache size to: {} bytes",
167 cache_manager.max_cache_size()
168 );
169
170 // Add a large file that would exceed the new limit
171 let very_largedata = vec![3u8; 400 * 1024]; // 400KB
172 cache
173 .write_cached("very_large_file.dat", &very_largedata)
174 .unwrap();
175
176 let final_stats = cache_manager.get_detailed_stats().unwrap();
177 println!(
178 "Final cache size: {} (should be within limit)",
179 final_stats.formatted_size()
180 );
181 println!(
182 "Final usage: {:.1}%",
183 final_stats.usage_percentage() * 100.0
184 );
185 println!();
186
187 // Demonstrate cache cleanup
188 println!("=== Cache Cleanup ==============================");
189 println!(
190 "Files before cleanup: {}",
191 cache_manager.list_cached_files().unwrap().len()
192 );
193 cache_manager.cleanup_old_files(100 * 1024).unwrap(); // Clean up to fit 100KB
194 let cleanup_stats = cache_manager.get_detailed_stats().unwrap();
195 println!("Files after cleanup: {}", cleanup_stats.file_count);
196 println!("Size after cleanup: {}", cleanup_stats.formatted_size());
197 println!();
198
199 // Demonstrate cache report
200 println!("=== Complete Cache Report ======================");
201 cache_manager.print_cache_report().unwrap();
202 println!();
203
204 // Clear all cache data
205 println!("=== Cache Clearing =============================");
206 cache_manager.clear_all().unwrap();
207 let empty_stats = cache_manager.get_stats();
208 println!("Files after clearing: {}", empty_stats.file_count);
209 println!("Size after clearing: {}", empty_stats.formatted_size());
210 println!();
211
212 // Demonstrate configuration examples
213 println!("=== Configuration Examples =====================");
214 println!("Example configurations for different use cases:");
215 println!();
216
217 println!("1. Development (small cache, frequent cleanup):");
218 println!(" CacheManager::with_full_config(cachedir, 20, 1800, 50*1024*1024, false)");
219 println!(" - 20 items in memory, 30 min TTL, 50MB disk limit");
220 println!();
221
222 println!("2. Production (large cache, longer retention):");
223 println!(" CacheManager::with_full_config(cachedir, 500, 86400, 1024*1024*1024, false)");
224 println!(" - 500 items in memory, 24 hour TTL, 1GB disk limit");
225 println!();
226
227 println!("3. Offline environment:");
228 println!(" CacheManager::with_full_config(cachedir, 100, 3600, 0, true)");
229 println!(" - Offline mode enabled, unlimited disk cache");
230 println!();
231
232 println!("4. Memory-constrained (minimal cache):");
233 println!(" CacheManager::with_full_config(cachedir, 10, 900, 10*1024*1024, false)");
234 println!(" - 10 items in memory, 15 min TTL, 10MB disk limit");
235 println!();
236
237 println!("=== Cache Management Demo Complete =============");
238}Sourcepub fn max_cache_size(&self) -> u64
pub fn max_cache_size(&self) -> u64
Get maximum cache size in bytes
Examples found in repository?
examples/cache_management_demo.rs (line 162)
10fn main() {
11 println!("=== Enhanced Cache Management Demonstration ===\n");
12
13 // Demonstrate platform-specific cache directory detection
14 println!("=== Platform-Specific Cache Directory =========");
15 match get_cachedir() {
16 Ok(cachedir) => {
17 println!("Default cache directory: {}", cachedir.display());
18 println!("Platform: {}", std::env::consts::OS);
19 }
20 Err(e) => {
21 println!("Error getting cache directory: {e}");
22 }
23 }
24 println!();
25
26 // Demonstrate environment variable override
27 println!("=== Environment Variable Configuration =========");
28 println!("Set SCIRS2_CACHE_DIR to override default cache location");
29 println!("Set SCIRS2_OFFLINE=true to enable offline mode");
30 if let Ok(cache_env) = std::env::var("SCIRS2_CACHE_DIR") {
31 println!("Custom cache directory: {cache_env}");
32 } else {
33 println!("Using default cache directory");
34 }
35
36 if let Ok(offline_env) = std::env::var("SCIRS2_OFFLINE") {
37 println!("Offline mode: {offline_env}");
38 } else {
39 println!("Offline mode: Not set (defaults to false)");
40 }
41 println!();
42
43 // Create a temporary cache for demonstration
44 let tempdir = tempfile::tempdir().unwrap();
45 let demo_cachedir = tempdir.path().join("demo_cache");
46
47 // Demonstrate cache with size limits
48 println!("=== Cache with Size Limits =====================");
49 let mut cache_manager = CacheManager::with_full_config(
50 demo_cachedir.clone(),
51 50, // 50 items in memory cache
52 3600, // 1 hour TTL
53 1024 * 1024, // 1MB disk cache limit
54 false, // Not in offline mode
55 );
56
57 println!("Created cache with 1MB size limit");
58 println!("Cache directory: {}", demo_cachedir.display());
59
60 // Add some test data to the cache
61 let cache = DatasetCache::with_full_config(demo_cachedir.clone(), 50, 3600, 1024 * 1024, false);
62
63 // Write several files of different sizes
64 let smalldata = vec![0u8; 1024]; // 1KB
65 let mediumdata = vec![1u8; 10240]; // 10KB
66 let largedata = vec![2u8; 102400]; // 100KB
67
68 cache.write_cached("small_file.dat", &smalldata).unwrap();
69 cache.write_cached("medium_file.dat", &mediumdata).unwrap();
70 cache.write_cached("large_file.dat", &largedata).unwrap();
71
72 println!("Added test files to cache");
73 println!();
74
75 // Demonstrate basic cache statistics
76 println!("=== Basic Cache Statistics ====================");
77 let basic_stats = cache_manager.get_stats();
78 println!("Files: {}", basic_stats.file_count);
79 println!("Total size: {}", basic_stats.formatted_size());
80 println!();
81
82 // Demonstrate detailed cache statistics
83 println!("=== Detailed Cache Statistics ==================");
84 match cache_manager.get_detailed_stats() {
85 Ok(detailed_stats) => {
86 println!("Cache Directory: {}", detailed_stats.cachedir.display());
87 println!(
88 "Total Size: {} ({} files)",
89 detailed_stats.formatted_size(),
90 detailed_stats.file_count
91 );
92 println!("Max Size: {}", detailed_stats.formatted_max_size());
93 println!("Usage: {:.1}%", detailed_stats.usage_percentage() * 100.0);
94 println!(
95 "Offline Mode: {}",
96 if detailed_stats.offline_mode {
97 "Enabled"
98 } else {
99 "Disabled"
100 }
101 );
102
103 if !detailed_stats.files.is_empty() {
104 println!("\nCached Files (sorted by size):");
105 for file in &detailed_stats.files {
106 println!(
107 " {} - {} (modified {})",
108 file.name,
109 file.formatted_size(),
110 file.formatted_modified()
111 );
112 }
113 }
114 }
115 Err(e) => {
116 println!("Error getting detailed stats: {e}");
117 }
118 }
119 println!();
120
121 // Demonstrate cache management operations
122 println!("=== Cache Management Operations ===============");
123 println!("Available operations:");
124 println!("1. List cached files");
125 let cached_files = cache_manager.list_cached_files().unwrap();
126 for file in &cached_files {
127 println!(" - {file}");
128 }
129
130 println!("2. Check if specific files are cached");
131 println!(
132 " small_file.dat: {}",
133 cache_manager.is_cached("small_file.dat")
134 );
135 println!(
136 " nonexistent.dat: {}",
137 cache_manager.is_cached("nonexistent.dat")
138 );
139
140 println!("3. Remove specific file");
141 cache_manager.remove("medium_file.dat").unwrap();
142 println!(" Removed medium_file.dat");
143 println!(
144 " Files remaining: {}",
145 cache_manager.list_cached_files().unwrap().len()
146 );
147 println!();
148
149 // Demonstrate offline mode
150 println!("=== Offline Mode Configuration ================");
151 println!("Current offline mode: {}", cache_manager.is_offline());
152 cache_manager.set_offline_mode(true);
153 println!("Enabled offline mode: {}", cache_manager.is_offline());
154 cache_manager.set_offline_mode(false);
155 println!("Disabled offline mode: {}", cache_manager.is_offline());
156 println!();
157
158 // Demonstrate cache size management
159 println!("=== Cache Size Management =====================");
160 println!(
161 "Current max cache size: {} bytes",
162 cache_manager.max_cache_size()
163 );
164 cache_manager.set_max_cache_size(512 * 1024); // 512KB
165 println!(
166 "Set max cache size to: {} bytes",
167 cache_manager.max_cache_size()
168 );
169
170 // Add a large file that would exceed the new limit
171 let very_largedata = vec![3u8; 400 * 1024]; // 400KB
172 cache
173 .write_cached("very_large_file.dat", &very_largedata)
174 .unwrap();
175
176 let final_stats = cache_manager.get_detailed_stats().unwrap();
177 println!(
178 "Final cache size: {} (should be within limit)",
179 final_stats.formatted_size()
180 );
181 println!(
182 "Final usage: {:.1}%",
183 final_stats.usage_percentage() * 100.0
184 );
185 println!();
186
187 // Demonstrate cache cleanup
188 println!("=== Cache Cleanup ==============================");
189 println!(
190 "Files before cleanup: {}",
191 cache_manager.list_cached_files().unwrap().len()
192 );
193 cache_manager.cleanup_old_files(100 * 1024).unwrap(); // Clean up to fit 100KB
194 let cleanup_stats = cache_manager.get_detailed_stats().unwrap();
195 println!("Files after cleanup: {}", cleanup_stats.file_count);
196 println!("Size after cleanup: {}", cleanup_stats.formatted_size());
197 println!();
198
199 // Demonstrate cache report
200 println!("=== Complete Cache Report ======================");
201 cache_manager.print_cache_report().unwrap();
202 println!();
203
204 // Clear all cache data
205 println!("=== Cache Clearing =============================");
206 cache_manager.clear_all().unwrap();
207 let empty_stats = cache_manager.get_stats();
208 println!("Files after clearing: {}", empty_stats.file_count);
209 println!("Size after clearing: {}", empty_stats.formatted_size());
210 println!();
211
212 // Demonstrate configuration examples
213 println!("=== Configuration Examples =====================");
214 println!("Example configurations for different use cases:");
215 println!();
216
217 println!("1. Development (small cache, frequent cleanup):");
218 println!(" CacheManager::with_full_config(cachedir, 20, 1800, 50*1024*1024, false)");
219 println!(" - 20 items in memory, 30 min TTL, 50MB disk limit");
220 println!();
221
222 println!("2. Production (large cache, longer retention):");
223 println!(" CacheManager::with_full_config(cachedir, 500, 86400, 1024*1024*1024, false)");
224 println!(" - 500 items in memory, 24 hour TTL, 1GB disk limit");
225 println!();
226
227 println!("3. Offline environment:");
228 println!(" CacheManager::with_full_config(cachedir, 100, 3600, 0, true)");
229 println!(" - Offline mode enabled, unlimited disk cache");
230 println!();
231
232 println!("4. Memory-constrained (minimal cache):");
233 println!(" CacheManager::with_full_config(cachedir, 10, 900, 10*1024*1024, false)");
234 println!(" - 10 items in memory, 15 min TTL, 10MB disk limit");
235 println!();
236
237 println!("=== Cache Management Demo Complete =============");
238}Sourcepub fn clear_all(&self) -> Result<()>
pub fn clear_all(&self) -> Result<()>
Clear all cached data
Examples found in repository?
examples/cache_management_demo.rs (line 206)
10fn main() {
11 println!("=== Enhanced Cache Management Demonstration ===\n");
12
13 // Demonstrate platform-specific cache directory detection
14 println!("=== Platform-Specific Cache Directory =========");
15 match get_cachedir() {
16 Ok(cachedir) => {
17 println!("Default cache directory: {}", cachedir.display());
18 println!("Platform: {}", std::env::consts::OS);
19 }
20 Err(e) => {
21 println!("Error getting cache directory: {e}");
22 }
23 }
24 println!();
25
26 // Demonstrate environment variable override
27 println!("=== Environment Variable Configuration =========");
28 println!("Set SCIRS2_CACHE_DIR to override default cache location");
29 println!("Set SCIRS2_OFFLINE=true to enable offline mode");
30 if let Ok(cache_env) = std::env::var("SCIRS2_CACHE_DIR") {
31 println!("Custom cache directory: {cache_env}");
32 } else {
33 println!("Using default cache directory");
34 }
35
36 if let Ok(offline_env) = std::env::var("SCIRS2_OFFLINE") {
37 println!("Offline mode: {offline_env}");
38 } else {
39 println!("Offline mode: Not set (defaults to false)");
40 }
41 println!();
42
43 // Create a temporary cache for demonstration
44 let tempdir = tempfile::tempdir().unwrap();
45 let demo_cachedir = tempdir.path().join("demo_cache");
46
47 // Demonstrate cache with size limits
48 println!("=== Cache with Size Limits =====================");
49 let mut cache_manager = CacheManager::with_full_config(
50 demo_cachedir.clone(),
51 50, // 50 items in memory cache
52 3600, // 1 hour TTL
53 1024 * 1024, // 1MB disk cache limit
54 false, // Not in offline mode
55 );
56
57 println!("Created cache with 1MB size limit");
58 println!("Cache directory: {}", demo_cachedir.display());
59
60 // Add some test data to the cache
61 let cache = DatasetCache::with_full_config(demo_cachedir.clone(), 50, 3600, 1024 * 1024, false);
62
63 // Write several files of different sizes
64 let smalldata = vec![0u8; 1024]; // 1KB
65 let mediumdata = vec![1u8; 10240]; // 10KB
66 let largedata = vec![2u8; 102400]; // 100KB
67
68 cache.write_cached("small_file.dat", &smalldata).unwrap();
69 cache.write_cached("medium_file.dat", &mediumdata).unwrap();
70 cache.write_cached("large_file.dat", &largedata).unwrap();
71
72 println!("Added test files to cache");
73 println!();
74
75 // Demonstrate basic cache statistics
76 println!("=== Basic Cache Statistics ====================");
77 let basic_stats = cache_manager.get_stats();
78 println!("Files: {}", basic_stats.file_count);
79 println!("Total size: {}", basic_stats.formatted_size());
80 println!();
81
82 // Demonstrate detailed cache statistics
83 println!("=== Detailed Cache Statistics ==================");
84 match cache_manager.get_detailed_stats() {
85 Ok(detailed_stats) => {
86 println!("Cache Directory: {}", detailed_stats.cachedir.display());
87 println!(
88 "Total Size: {} ({} files)",
89 detailed_stats.formatted_size(),
90 detailed_stats.file_count
91 );
92 println!("Max Size: {}", detailed_stats.formatted_max_size());
93 println!("Usage: {:.1}%", detailed_stats.usage_percentage() * 100.0);
94 println!(
95 "Offline Mode: {}",
96 if detailed_stats.offline_mode {
97 "Enabled"
98 } else {
99 "Disabled"
100 }
101 );
102
103 if !detailed_stats.files.is_empty() {
104 println!("\nCached Files (sorted by size):");
105 for file in &detailed_stats.files {
106 println!(
107 " {} - {} (modified {})",
108 file.name,
109 file.formatted_size(),
110 file.formatted_modified()
111 );
112 }
113 }
114 }
115 Err(e) => {
116 println!("Error getting detailed stats: {e}");
117 }
118 }
119 println!();
120
121 // Demonstrate cache management operations
122 println!("=== Cache Management Operations ===============");
123 println!("Available operations:");
124 println!("1. List cached files");
125 let cached_files = cache_manager.list_cached_files().unwrap();
126 for file in &cached_files {
127 println!(" - {file}");
128 }
129
130 println!("2. Check if specific files are cached");
131 println!(
132 " small_file.dat: {}",
133 cache_manager.is_cached("small_file.dat")
134 );
135 println!(
136 " nonexistent.dat: {}",
137 cache_manager.is_cached("nonexistent.dat")
138 );
139
140 println!("3. Remove specific file");
141 cache_manager.remove("medium_file.dat").unwrap();
142 println!(" Removed medium_file.dat");
143 println!(
144 " Files remaining: {}",
145 cache_manager.list_cached_files().unwrap().len()
146 );
147 println!();
148
149 // Demonstrate offline mode
150 println!("=== Offline Mode Configuration ================");
151 println!("Current offline mode: {}", cache_manager.is_offline());
152 cache_manager.set_offline_mode(true);
153 println!("Enabled offline mode: {}", cache_manager.is_offline());
154 cache_manager.set_offline_mode(false);
155 println!("Disabled offline mode: {}", cache_manager.is_offline());
156 println!();
157
158 // Demonstrate cache size management
159 println!("=== Cache Size Management =====================");
160 println!(
161 "Current max cache size: {} bytes",
162 cache_manager.max_cache_size()
163 );
164 cache_manager.set_max_cache_size(512 * 1024); // 512KB
165 println!(
166 "Set max cache size to: {} bytes",
167 cache_manager.max_cache_size()
168 );
169
170 // Add a large file that would exceed the new limit
171 let very_largedata = vec![3u8; 400 * 1024]; // 400KB
172 cache
173 .write_cached("very_large_file.dat", &very_largedata)
174 .unwrap();
175
176 let final_stats = cache_manager.get_detailed_stats().unwrap();
177 println!(
178 "Final cache size: {} (should be within limit)",
179 final_stats.formatted_size()
180 );
181 println!(
182 "Final usage: {:.1}%",
183 final_stats.usage_percentage() * 100.0
184 );
185 println!();
186
187 // Demonstrate cache cleanup
188 println!("=== Cache Cleanup ==============================");
189 println!(
190 "Files before cleanup: {}",
191 cache_manager.list_cached_files().unwrap().len()
192 );
193 cache_manager.cleanup_old_files(100 * 1024).unwrap(); // Clean up to fit 100KB
194 let cleanup_stats = cache_manager.get_detailed_stats().unwrap();
195 println!("Files after cleanup: {}", cleanup_stats.file_count);
196 println!("Size after cleanup: {}", cleanup_stats.formatted_size());
197 println!();
198
199 // Demonstrate cache report
200 println!("=== Complete Cache Report ======================");
201 cache_manager.print_cache_report().unwrap();
202 println!();
203
204 // Clear all cache data
205 println!("=== Cache Clearing =============================");
206 cache_manager.clear_all().unwrap();
207 let empty_stats = cache_manager.get_stats();
208 println!("Files after clearing: {}", empty_stats.file_count);
209 println!("Size after clearing: {}", empty_stats.formatted_size());
210 println!();
211
212 // Demonstrate configuration examples
213 println!("=== Configuration Examples =====================");
214 println!("Example configurations for different use cases:");
215 println!();
216
217 println!("1. Development (small cache, frequent cleanup):");
218 println!(" CacheManager::with_full_config(cachedir, 20, 1800, 50*1024*1024, false)");
219 println!(" - 20 items in memory, 30 min TTL, 50MB disk limit");
220 println!();
221
222 println!("2. Production (large cache, longer retention):");
223 println!(" CacheManager::with_full_config(cachedir, 500, 86400, 1024*1024*1024, false)");
224 println!(" - 500 items in memory, 24 hour TTL, 1GB disk limit");
225 println!();
226
227 println!("3. Offline environment:");
228 println!(" CacheManager::with_full_config(cachedir, 100, 3600, 0, true)");
229 println!(" - Offline mode enabled, unlimited disk cache");
230 println!();
231
232 println!("4. Memory-constrained (minimal cache):");
233 println!(" CacheManager::with_full_config(cachedir, 10, 900, 10*1024*1024, false)");
234 println!(" - 10 items in memory, 15 min TTL, 10MB disk limit");
235 println!();
236
237 println!("=== Cache Management Demo Complete =============");
238}Sourcepub fn remove(&self, name: &str) -> Result<()>
pub fn remove(&self, name: &str) -> Result<()>
Remove specific cached file
Examples found in repository?
examples/cache_management_demo.rs (line 141)
10fn main() {
11 println!("=== Enhanced Cache Management Demonstration ===\n");
12
13 // Demonstrate platform-specific cache directory detection
14 println!("=== Platform-Specific Cache Directory =========");
15 match get_cachedir() {
16 Ok(cachedir) => {
17 println!("Default cache directory: {}", cachedir.display());
18 println!("Platform: {}", std::env::consts::OS);
19 }
20 Err(e) => {
21 println!("Error getting cache directory: {e}");
22 }
23 }
24 println!();
25
26 // Demonstrate environment variable override
27 println!("=== Environment Variable Configuration =========");
28 println!("Set SCIRS2_CACHE_DIR to override default cache location");
29 println!("Set SCIRS2_OFFLINE=true to enable offline mode");
30 if let Ok(cache_env) = std::env::var("SCIRS2_CACHE_DIR") {
31 println!("Custom cache directory: {cache_env}");
32 } else {
33 println!("Using default cache directory");
34 }
35
36 if let Ok(offline_env) = std::env::var("SCIRS2_OFFLINE") {
37 println!("Offline mode: {offline_env}");
38 } else {
39 println!("Offline mode: Not set (defaults to false)");
40 }
41 println!();
42
43 // Create a temporary cache for demonstration
44 let tempdir = tempfile::tempdir().unwrap();
45 let demo_cachedir = tempdir.path().join("demo_cache");
46
47 // Demonstrate cache with size limits
48 println!("=== Cache with Size Limits =====================");
49 let mut cache_manager = CacheManager::with_full_config(
50 demo_cachedir.clone(),
51 50, // 50 items in memory cache
52 3600, // 1 hour TTL
53 1024 * 1024, // 1MB disk cache limit
54 false, // Not in offline mode
55 );
56
57 println!("Created cache with 1MB size limit");
58 println!("Cache directory: {}", demo_cachedir.display());
59
60 // Add some test data to the cache
61 let cache = DatasetCache::with_full_config(demo_cachedir.clone(), 50, 3600, 1024 * 1024, false);
62
63 // Write several files of different sizes
64 let smalldata = vec![0u8; 1024]; // 1KB
65 let mediumdata = vec![1u8; 10240]; // 10KB
66 let largedata = vec![2u8; 102400]; // 100KB
67
68 cache.write_cached("small_file.dat", &smalldata).unwrap();
69 cache.write_cached("medium_file.dat", &mediumdata).unwrap();
70 cache.write_cached("large_file.dat", &largedata).unwrap();
71
72 println!("Added test files to cache");
73 println!();
74
75 // Demonstrate basic cache statistics
76 println!("=== Basic Cache Statistics ====================");
77 let basic_stats = cache_manager.get_stats();
78 println!("Files: {}", basic_stats.file_count);
79 println!("Total size: {}", basic_stats.formatted_size());
80 println!();
81
82 // Demonstrate detailed cache statistics
83 println!("=== Detailed Cache Statistics ==================");
84 match cache_manager.get_detailed_stats() {
85 Ok(detailed_stats) => {
86 println!("Cache Directory: {}", detailed_stats.cachedir.display());
87 println!(
88 "Total Size: {} ({} files)",
89 detailed_stats.formatted_size(),
90 detailed_stats.file_count
91 );
92 println!("Max Size: {}", detailed_stats.formatted_max_size());
93 println!("Usage: {:.1}%", detailed_stats.usage_percentage() * 100.0);
94 println!(
95 "Offline Mode: {}",
96 if detailed_stats.offline_mode {
97 "Enabled"
98 } else {
99 "Disabled"
100 }
101 );
102
103 if !detailed_stats.files.is_empty() {
104 println!("\nCached Files (sorted by size):");
105 for file in &detailed_stats.files {
106 println!(
107 " {} - {} (modified {})",
108 file.name,
109 file.formatted_size(),
110 file.formatted_modified()
111 );
112 }
113 }
114 }
115 Err(e) => {
116 println!("Error getting detailed stats: {e}");
117 }
118 }
119 println!();
120
121 // Demonstrate cache management operations
122 println!("=== Cache Management Operations ===============");
123 println!("Available operations:");
124 println!("1. List cached files");
125 let cached_files = cache_manager.list_cached_files().unwrap();
126 for file in &cached_files {
127 println!(" - {file}");
128 }
129
130 println!("2. Check if specific files are cached");
131 println!(
132 " small_file.dat: {}",
133 cache_manager.is_cached("small_file.dat")
134 );
135 println!(
136 " nonexistent.dat: {}",
137 cache_manager.is_cached("nonexistent.dat")
138 );
139
140 println!("3. Remove specific file");
141 cache_manager.remove("medium_file.dat").unwrap();
142 println!(" Removed medium_file.dat");
143 println!(
144 " Files remaining: {}",
145 cache_manager.list_cached_files().unwrap().len()
146 );
147 println!();
148
149 // Demonstrate offline mode
150 println!("=== Offline Mode Configuration ================");
151 println!("Current offline mode: {}", cache_manager.is_offline());
152 cache_manager.set_offline_mode(true);
153 println!("Enabled offline mode: {}", cache_manager.is_offline());
154 cache_manager.set_offline_mode(false);
155 println!("Disabled offline mode: {}", cache_manager.is_offline());
156 println!();
157
158 // Demonstrate cache size management
159 println!("=== Cache Size Management =====================");
160 println!(
161 "Current max cache size: {} bytes",
162 cache_manager.max_cache_size()
163 );
164 cache_manager.set_max_cache_size(512 * 1024); // 512KB
165 println!(
166 "Set max cache size to: {} bytes",
167 cache_manager.max_cache_size()
168 );
169
170 // Add a large file that would exceed the new limit
171 let very_largedata = vec![3u8; 400 * 1024]; // 400KB
172 cache
173 .write_cached("very_large_file.dat", &very_largedata)
174 .unwrap();
175
176 let final_stats = cache_manager.get_detailed_stats().unwrap();
177 println!(
178 "Final cache size: {} (should be within limit)",
179 final_stats.formatted_size()
180 );
181 println!(
182 "Final usage: {:.1}%",
183 final_stats.usage_percentage() * 100.0
184 );
185 println!();
186
187 // Demonstrate cache cleanup
188 println!("=== Cache Cleanup ==============================");
189 println!(
190 "Files before cleanup: {}",
191 cache_manager.list_cached_files().unwrap().len()
192 );
193 cache_manager.cleanup_old_files(100 * 1024).unwrap(); // Clean up to fit 100KB
194 let cleanup_stats = cache_manager.get_detailed_stats().unwrap();
195 println!("Files after cleanup: {}", cleanup_stats.file_count);
196 println!("Size after cleanup: {}", cleanup_stats.formatted_size());
197 println!();
198
199 // Demonstrate cache report
200 println!("=== Complete Cache Report ======================");
201 cache_manager.print_cache_report().unwrap();
202 println!();
203
204 // Clear all cache data
205 println!("=== Cache Clearing =============================");
206 cache_manager.clear_all().unwrap();
207 let empty_stats = cache_manager.get_stats();
208 println!("Files after clearing: {}", empty_stats.file_count);
209 println!("Size after clearing: {}", empty_stats.formatted_size());
210 println!();
211
212 // Demonstrate configuration examples
213 println!("=== Configuration Examples =====================");
214 println!("Example configurations for different use cases:");
215 println!();
216
217 println!("1. Development (small cache, frequent cleanup):");
218 println!(" CacheManager::with_full_config(cachedir, 20, 1800, 50*1024*1024, false)");
219 println!(" - 20 items in memory, 30 min TTL, 50MB disk limit");
220 println!();
221
222 println!("2. Production (large cache, longer retention):");
223 println!(" CacheManager::with_full_config(cachedir, 500, 86400, 1024*1024*1024, false)");
224 println!(" - 500 items in memory, 24 hour TTL, 1GB disk limit");
225 println!();
226
227 println!("3. Offline environment:");
228 println!(" CacheManager::with_full_config(cachedir, 100, 3600, 0, true)");
229 println!(" - Offline mode enabled, unlimited disk cache");
230 println!();
231
232 println!("4. Memory-constrained (minimal cache):");
233 println!(" CacheManager::with_full_config(cachedir, 10, 900, 10*1024*1024, false)");
234 println!(" - 10 items in memory, 15 min TTL, 10MB disk limit");
235 println!();
236
237 println!("=== Cache Management Demo Complete =============");
238}Sourcepub fn cleanup_old_files(&self, target_size: u64) -> Result<()>
pub fn cleanup_old_files(&self, target_size: u64) -> Result<()>
Remove old files to free up space
Examples found in repository?
examples/cache_management_demo.rs (line 193)
10fn main() {
11 println!("=== Enhanced Cache Management Demonstration ===\n");
12
13 // Demonstrate platform-specific cache directory detection
14 println!("=== Platform-Specific Cache Directory =========");
15 match get_cachedir() {
16 Ok(cachedir) => {
17 println!("Default cache directory: {}", cachedir.display());
18 println!("Platform: {}", std::env::consts::OS);
19 }
20 Err(e) => {
21 println!("Error getting cache directory: {e}");
22 }
23 }
24 println!();
25
26 // Demonstrate environment variable override
27 println!("=== Environment Variable Configuration =========");
28 println!("Set SCIRS2_CACHE_DIR to override default cache location");
29 println!("Set SCIRS2_OFFLINE=true to enable offline mode");
30 if let Ok(cache_env) = std::env::var("SCIRS2_CACHE_DIR") {
31 println!("Custom cache directory: {cache_env}");
32 } else {
33 println!("Using default cache directory");
34 }
35
36 if let Ok(offline_env) = std::env::var("SCIRS2_OFFLINE") {
37 println!("Offline mode: {offline_env}");
38 } else {
39 println!("Offline mode: Not set (defaults to false)");
40 }
41 println!();
42
43 // Create a temporary cache for demonstration
44 let tempdir = tempfile::tempdir().unwrap();
45 let demo_cachedir = tempdir.path().join("demo_cache");
46
47 // Demonstrate cache with size limits
48 println!("=== Cache with Size Limits =====================");
49 let mut cache_manager = CacheManager::with_full_config(
50 demo_cachedir.clone(),
51 50, // 50 items in memory cache
52 3600, // 1 hour TTL
53 1024 * 1024, // 1MB disk cache limit
54 false, // Not in offline mode
55 );
56
57 println!("Created cache with 1MB size limit");
58 println!("Cache directory: {}", demo_cachedir.display());
59
60 // Add some test data to the cache
61 let cache = DatasetCache::with_full_config(demo_cachedir.clone(), 50, 3600, 1024 * 1024, false);
62
63 // Write several files of different sizes
64 let smalldata = vec![0u8; 1024]; // 1KB
65 let mediumdata = vec![1u8; 10240]; // 10KB
66 let largedata = vec![2u8; 102400]; // 100KB
67
68 cache.write_cached("small_file.dat", &smalldata).unwrap();
69 cache.write_cached("medium_file.dat", &mediumdata).unwrap();
70 cache.write_cached("large_file.dat", &largedata).unwrap();
71
72 println!("Added test files to cache");
73 println!();
74
75 // Demonstrate basic cache statistics
76 println!("=== Basic Cache Statistics ====================");
77 let basic_stats = cache_manager.get_stats();
78 println!("Files: {}", basic_stats.file_count);
79 println!("Total size: {}", basic_stats.formatted_size());
80 println!();
81
82 // Demonstrate detailed cache statistics
83 println!("=== Detailed Cache Statistics ==================");
84 match cache_manager.get_detailed_stats() {
85 Ok(detailed_stats) => {
86 println!("Cache Directory: {}", detailed_stats.cachedir.display());
87 println!(
88 "Total Size: {} ({} files)",
89 detailed_stats.formatted_size(),
90 detailed_stats.file_count
91 );
92 println!("Max Size: {}", detailed_stats.formatted_max_size());
93 println!("Usage: {:.1}%", detailed_stats.usage_percentage() * 100.0);
94 println!(
95 "Offline Mode: {}",
96 if detailed_stats.offline_mode {
97 "Enabled"
98 } else {
99 "Disabled"
100 }
101 );
102
103 if !detailed_stats.files.is_empty() {
104 println!("\nCached Files (sorted by size):");
105 for file in &detailed_stats.files {
106 println!(
107 " {} - {} (modified {})",
108 file.name,
109 file.formatted_size(),
110 file.formatted_modified()
111 );
112 }
113 }
114 }
115 Err(e) => {
116 println!("Error getting detailed stats: {e}");
117 }
118 }
119 println!();
120
121 // Demonstrate cache management operations
122 println!("=== Cache Management Operations ===============");
123 println!("Available operations:");
124 println!("1. List cached files");
125 let cached_files = cache_manager.list_cached_files().unwrap();
126 for file in &cached_files {
127 println!(" - {file}");
128 }
129
130 println!("2. Check if specific files are cached");
131 println!(
132 " small_file.dat: {}",
133 cache_manager.is_cached("small_file.dat")
134 );
135 println!(
136 " nonexistent.dat: {}",
137 cache_manager.is_cached("nonexistent.dat")
138 );
139
140 println!("3. Remove specific file");
141 cache_manager.remove("medium_file.dat").unwrap();
142 println!(" Removed medium_file.dat");
143 println!(
144 " Files remaining: {}",
145 cache_manager.list_cached_files().unwrap().len()
146 );
147 println!();
148
149 // Demonstrate offline mode
150 println!("=== Offline Mode Configuration ================");
151 println!("Current offline mode: {}", cache_manager.is_offline());
152 cache_manager.set_offline_mode(true);
153 println!("Enabled offline mode: {}", cache_manager.is_offline());
154 cache_manager.set_offline_mode(false);
155 println!("Disabled offline mode: {}", cache_manager.is_offline());
156 println!();
157
158 // Demonstrate cache size management
159 println!("=== Cache Size Management =====================");
160 println!(
161 "Current max cache size: {} bytes",
162 cache_manager.max_cache_size()
163 );
164 cache_manager.set_max_cache_size(512 * 1024); // 512KB
165 println!(
166 "Set max cache size to: {} bytes",
167 cache_manager.max_cache_size()
168 );
169
170 // Add a large file that would exceed the new limit
171 let very_largedata = vec![3u8; 400 * 1024]; // 400KB
172 cache
173 .write_cached("very_large_file.dat", &very_largedata)
174 .unwrap();
175
176 let final_stats = cache_manager.get_detailed_stats().unwrap();
177 println!(
178 "Final cache size: {} (should be within limit)",
179 final_stats.formatted_size()
180 );
181 println!(
182 "Final usage: {:.1}%",
183 final_stats.usage_percentage() * 100.0
184 );
185 println!();
186
187 // Demonstrate cache cleanup
188 println!("=== Cache Cleanup ==============================");
189 println!(
190 "Files before cleanup: {}",
191 cache_manager.list_cached_files().unwrap().len()
192 );
193 cache_manager.cleanup_old_files(100 * 1024).unwrap(); // Clean up to fit 100KB
194 let cleanup_stats = cache_manager.get_detailed_stats().unwrap();
195 println!("Files after cleanup: {}", cleanup_stats.file_count);
196 println!("Size after cleanup: {}", cleanup_stats.formatted_size());
197 println!();
198
199 // Demonstrate cache report
200 println!("=== Complete Cache Report ======================");
201 cache_manager.print_cache_report().unwrap();
202 println!();
203
204 // Clear all cache data
205 println!("=== Cache Clearing =============================");
206 cache_manager.clear_all().unwrap();
207 let empty_stats = cache_manager.get_stats();
208 println!("Files after clearing: {}", empty_stats.file_count);
209 println!("Size after clearing: {}", empty_stats.formatted_size());
210 println!();
211
212 // Demonstrate configuration examples
213 println!("=== Configuration Examples =====================");
214 println!("Example configurations for different use cases:");
215 println!();
216
217 println!("1. Development (small cache, frequent cleanup):");
218 println!(" CacheManager::with_full_config(cachedir, 20, 1800, 50*1024*1024, false)");
219 println!(" - 20 items in memory, 30 min TTL, 50MB disk limit");
220 println!();
221
222 println!("2. Production (large cache, longer retention):");
223 println!(" CacheManager::with_full_config(cachedir, 500, 86400, 1024*1024*1024, false)");
224 println!(" - 500 items in memory, 24 hour TTL, 1GB disk limit");
225 println!();
226
227 println!("3. Offline environment:");
228 println!(" CacheManager::with_full_config(cachedir, 100, 3600, 0, true)");
229 println!(" - Offline mode enabled, unlimited disk cache");
230 println!();
231
232 println!("4. Memory-constrained (minimal cache):");
233 println!(" CacheManager::with_full_config(cachedir, 10, 900, 10*1024*1024, false)");
234 println!(" - 10 items in memory, 15 min TTL, 10MB disk limit");
235 println!();
236
237 println!("=== Cache Management Demo Complete =============");
238}Sourcepub fn list_cached_files(&self) -> Result<Vec<String>>
pub fn list_cached_files(&self) -> Result<Vec<String>>
List all cached files
Examples found in repository?
examples/cache_management_demo.rs (line 125)
10fn main() {
11 println!("=== Enhanced Cache Management Demonstration ===\n");
12
13 // Demonstrate platform-specific cache directory detection
14 println!("=== Platform-Specific Cache Directory =========");
15 match get_cachedir() {
16 Ok(cachedir) => {
17 println!("Default cache directory: {}", cachedir.display());
18 println!("Platform: {}", std::env::consts::OS);
19 }
20 Err(e) => {
21 println!("Error getting cache directory: {e}");
22 }
23 }
24 println!();
25
26 // Demonstrate environment variable override
27 println!("=== Environment Variable Configuration =========");
28 println!("Set SCIRS2_CACHE_DIR to override default cache location");
29 println!("Set SCIRS2_OFFLINE=true to enable offline mode");
30 if let Ok(cache_env) = std::env::var("SCIRS2_CACHE_DIR") {
31 println!("Custom cache directory: {cache_env}");
32 } else {
33 println!("Using default cache directory");
34 }
35
36 if let Ok(offline_env) = std::env::var("SCIRS2_OFFLINE") {
37 println!("Offline mode: {offline_env}");
38 } else {
39 println!("Offline mode: Not set (defaults to false)");
40 }
41 println!();
42
43 // Create a temporary cache for demonstration
44 let tempdir = tempfile::tempdir().unwrap();
45 let demo_cachedir = tempdir.path().join("demo_cache");
46
47 // Demonstrate cache with size limits
48 println!("=== Cache with Size Limits =====================");
49 let mut cache_manager = CacheManager::with_full_config(
50 demo_cachedir.clone(),
51 50, // 50 items in memory cache
52 3600, // 1 hour TTL
53 1024 * 1024, // 1MB disk cache limit
54 false, // Not in offline mode
55 );
56
57 println!("Created cache with 1MB size limit");
58 println!("Cache directory: {}", demo_cachedir.display());
59
60 // Add some test data to the cache
61 let cache = DatasetCache::with_full_config(demo_cachedir.clone(), 50, 3600, 1024 * 1024, false);
62
63 // Write several files of different sizes
64 let smalldata = vec![0u8; 1024]; // 1KB
65 let mediumdata = vec![1u8; 10240]; // 10KB
66 let largedata = vec![2u8; 102400]; // 100KB
67
68 cache.write_cached("small_file.dat", &smalldata).unwrap();
69 cache.write_cached("medium_file.dat", &mediumdata).unwrap();
70 cache.write_cached("large_file.dat", &largedata).unwrap();
71
72 println!("Added test files to cache");
73 println!();
74
75 // Demonstrate basic cache statistics
76 println!("=== Basic Cache Statistics ====================");
77 let basic_stats = cache_manager.get_stats();
78 println!("Files: {}", basic_stats.file_count);
79 println!("Total size: {}", basic_stats.formatted_size());
80 println!();
81
82 // Demonstrate detailed cache statistics
83 println!("=== Detailed Cache Statistics ==================");
84 match cache_manager.get_detailed_stats() {
85 Ok(detailed_stats) => {
86 println!("Cache Directory: {}", detailed_stats.cachedir.display());
87 println!(
88 "Total Size: {} ({} files)",
89 detailed_stats.formatted_size(),
90 detailed_stats.file_count
91 );
92 println!("Max Size: {}", detailed_stats.formatted_max_size());
93 println!("Usage: {:.1}%", detailed_stats.usage_percentage() * 100.0);
94 println!(
95 "Offline Mode: {}",
96 if detailed_stats.offline_mode {
97 "Enabled"
98 } else {
99 "Disabled"
100 }
101 );
102
103 if !detailed_stats.files.is_empty() {
104 println!("\nCached Files (sorted by size):");
105 for file in &detailed_stats.files {
106 println!(
107 " {} - {} (modified {})",
108 file.name,
109 file.formatted_size(),
110 file.formatted_modified()
111 );
112 }
113 }
114 }
115 Err(e) => {
116 println!("Error getting detailed stats: {e}");
117 }
118 }
119 println!();
120
121 // Demonstrate cache management operations
122 println!("=== Cache Management Operations ===============");
123 println!("Available operations:");
124 println!("1. List cached files");
125 let cached_files = cache_manager.list_cached_files().unwrap();
126 for file in &cached_files {
127 println!(" - {file}");
128 }
129
130 println!("2. Check if specific files are cached");
131 println!(
132 " small_file.dat: {}",
133 cache_manager.is_cached("small_file.dat")
134 );
135 println!(
136 " nonexistent.dat: {}",
137 cache_manager.is_cached("nonexistent.dat")
138 );
139
140 println!("3. Remove specific file");
141 cache_manager.remove("medium_file.dat").unwrap();
142 println!(" Removed medium_file.dat");
143 println!(
144 " Files remaining: {}",
145 cache_manager.list_cached_files().unwrap().len()
146 );
147 println!();
148
149 // Demonstrate offline mode
150 println!("=== Offline Mode Configuration ================");
151 println!("Current offline mode: {}", cache_manager.is_offline());
152 cache_manager.set_offline_mode(true);
153 println!("Enabled offline mode: {}", cache_manager.is_offline());
154 cache_manager.set_offline_mode(false);
155 println!("Disabled offline mode: {}", cache_manager.is_offline());
156 println!();
157
158 // Demonstrate cache size management
159 println!("=== Cache Size Management =====================");
160 println!(
161 "Current max cache size: {} bytes",
162 cache_manager.max_cache_size()
163 );
164 cache_manager.set_max_cache_size(512 * 1024); // 512KB
165 println!(
166 "Set max cache size to: {} bytes",
167 cache_manager.max_cache_size()
168 );
169
170 // Add a large file that would exceed the new limit
171 let very_largedata = vec![3u8; 400 * 1024]; // 400KB
172 cache
173 .write_cached("very_large_file.dat", &very_largedata)
174 .unwrap();
175
176 let final_stats = cache_manager.get_detailed_stats().unwrap();
177 println!(
178 "Final cache size: {} (should be within limit)",
179 final_stats.formatted_size()
180 );
181 println!(
182 "Final usage: {:.1}%",
183 final_stats.usage_percentage() * 100.0
184 );
185 println!();
186
187 // Demonstrate cache cleanup
188 println!("=== Cache Cleanup ==============================");
189 println!(
190 "Files before cleanup: {}",
191 cache_manager.list_cached_files().unwrap().len()
192 );
193 cache_manager.cleanup_old_files(100 * 1024).unwrap(); // Clean up to fit 100KB
194 let cleanup_stats = cache_manager.get_detailed_stats().unwrap();
195 println!("Files after cleanup: {}", cleanup_stats.file_count);
196 println!("Size after cleanup: {}", cleanup_stats.formatted_size());
197 println!();
198
199 // Demonstrate cache report
200 println!("=== Complete Cache Report ======================");
201 cache_manager.print_cache_report().unwrap();
202 println!();
203
204 // Clear all cache data
205 println!("=== Cache Clearing =============================");
206 cache_manager.clear_all().unwrap();
207 let empty_stats = cache_manager.get_stats();
208 println!("Files after clearing: {}", empty_stats.file_count);
209 println!("Size after clearing: {}", empty_stats.formatted_size());
210 println!();
211
212 // Demonstrate configuration examples
213 println!("=== Configuration Examples =====================");
214 println!("Example configurations for different use cases:");
215 println!();
216
217 println!("1. Development (small cache, frequent cleanup):");
218 println!(" CacheManager::with_full_config(cachedir, 20, 1800, 50*1024*1024, false)");
219 println!(" - 20 items in memory, 30 min TTL, 50MB disk limit");
220 println!();
221
222 println!("2. Production (large cache, longer retention):");
223 println!(" CacheManager::with_full_config(cachedir, 500, 86400, 1024*1024*1024, false)");
224 println!(" - 500 items in memory, 24 hour TTL, 1GB disk limit");
225 println!();
226
227 println!("3. Offline environment:");
228 println!(" CacheManager::with_full_config(cachedir, 100, 3600, 0, true)");
229 println!(" - Offline mode enabled, unlimited disk cache");
230 println!();
231
232 println!("4. Memory-constrained (minimal cache):");
233 println!(" CacheManager::with_full_config(cachedir, 10, 900, 10*1024*1024, false)");
234 println!(" - 10 items in memory, 15 min TTL, 10MB disk limit");
235 println!();
236
237 println!("=== Cache Management Demo Complete =============");
238}Sourcepub fn is_cached(&self, name: &str) -> bool
pub fn is_cached(&self, name: &str) -> bool
Check if a file is cached
Examples found in repository?
examples/cache_management_demo.rs (line 133)
10fn main() {
11 println!("=== Enhanced Cache Management Demonstration ===\n");
12
13 // Demonstrate platform-specific cache directory detection
14 println!("=== Platform-Specific Cache Directory =========");
15 match get_cachedir() {
16 Ok(cachedir) => {
17 println!("Default cache directory: {}", cachedir.display());
18 println!("Platform: {}", std::env::consts::OS);
19 }
20 Err(e) => {
21 println!("Error getting cache directory: {e}");
22 }
23 }
24 println!();
25
26 // Demonstrate environment variable override
27 println!("=== Environment Variable Configuration =========");
28 println!("Set SCIRS2_CACHE_DIR to override default cache location");
29 println!("Set SCIRS2_OFFLINE=true to enable offline mode");
30 if let Ok(cache_env) = std::env::var("SCIRS2_CACHE_DIR") {
31 println!("Custom cache directory: {cache_env}");
32 } else {
33 println!("Using default cache directory");
34 }
35
36 if let Ok(offline_env) = std::env::var("SCIRS2_OFFLINE") {
37 println!("Offline mode: {offline_env}");
38 } else {
39 println!("Offline mode: Not set (defaults to false)");
40 }
41 println!();
42
43 // Create a temporary cache for demonstration
44 let tempdir = tempfile::tempdir().unwrap();
45 let demo_cachedir = tempdir.path().join("demo_cache");
46
47 // Demonstrate cache with size limits
48 println!("=== Cache with Size Limits =====================");
49 let mut cache_manager = CacheManager::with_full_config(
50 demo_cachedir.clone(),
51 50, // 50 items in memory cache
52 3600, // 1 hour TTL
53 1024 * 1024, // 1MB disk cache limit
54 false, // Not in offline mode
55 );
56
57 println!("Created cache with 1MB size limit");
58 println!("Cache directory: {}", demo_cachedir.display());
59
60 // Add some test data to the cache
61 let cache = DatasetCache::with_full_config(demo_cachedir.clone(), 50, 3600, 1024 * 1024, false);
62
63 // Write several files of different sizes
64 let smalldata = vec![0u8; 1024]; // 1KB
65 let mediumdata = vec![1u8; 10240]; // 10KB
66 let largedata = vec![2u8; 102400]; // 100KB
67
68 cache.write_cached("small_file.dat", &smalldata).unwrap();
69 cache.write_cached("medium_file.dat", &mediumdata).unwrap();
70 cache.write_cached("large_file.dat", &largedata).unwrap();
71
72 println!("Added test files to cache");
73 println!();
74
75 // Demonstrate basic cache statistics
76 println!("=== Basic Cache Statistics ====================");
77 let basic_stats = cache_manager.get_stats();
78 println!("Files: {}", basic_stats.file_count);
79 println!("Total size: {}", basic_stats.formatted_size());
80 println!();
81
82 // Demonstrate detailed cache statistics
83 println!("=== Detailed Cache Statistics ==================");
84 match cache_manager.get_detailed_stats() {
85 Ok(detailed_stats) => {
86 println!("Cache Directory: {}", detailed_stats.cachedir.display());
87 println!(
88 "Total Size: {} ({} files)",
89 detailed_stats.formatted_size(),
90 detailed_stats.file_count
91 );
92 println!("Max Size: {}", detailed_stats.formatted_max_size());
93 println!("Usage: {:.1}%", detailed_stats.usage_percentage() * 100.0);
94 println!(
95 "Offline Mode: {}",
96 if detailed_stats.offline_mode {
97 "Enabled"
98 } else {
99 "Disabled"
100 }
101 );
102
103 if !detailed_stats.files.is_empty() {
104 println!("\nCached Files (sorted by size):");
105 for file in &detailed_stats.files {
106 println!(
107 " {} - {} (modified {})",
108 file.name,
109 file.formatted_size(),
110 file.formatted_modified()
111 );
112 }
113 }
114 }
115 Err(e) => {
116 println!("Error getting detailed stats: {e}");
117 }
118 }
119 println!();
120
121 // Demonstrate cache management operations
122 println!("=== Cache Management Operations ===============");
123 println!("Available operations:");
124 println!("1. List cached files");
125 let cached_files = cache_manager.list_cached_files().unwrap();
126 for file in &cached_files {
127 println!(" - {file}");
128 }
129
130 println!("2. Check if specific files are cached");
131 println!(
132 " small_file.dat: {}",
133 cache_manager.is_cached("small_file.dat")
134 );
135 println!(
136 " nonexistent.dat: {}",
137 cache_manager.is_cached("nonexistent.dat")
138 );
139
140 println!("3. Remove specific file");
141 cache_manager.remove("medium_file.dat").unwrap();
142 println!(" Removed medium_file.dat");
143 println!(
144 " Files remaining: {}",
145 cache_manager.list_cached_files().unwrap().len()
146 );
147 println!();
148
149 // Demonstrate offline mode
150 println!("=== Offline Mode Configuration ================");
151 println!("Current offline mode: {}", cache_manager.is_offline());
152 cache_manager.set_offline_mode(true);
153 println!("Enabled offline mode: {}", cache_manager.is_offline());
154 cache_manager.set_offline_mode(false);
155 println!("Disabled offline mode: {}", cache_manager.is_offline());
156 println!();
157
158 // Demonstrate cache size management
159 println!("=== Cache Size Management =====================");
160 println!(
161 "Current max cache size: {} bytes",
162 cache_manager.max_cache_size()
163 );
164 cache_manager.set_max_cache_size(512 * 1024); // 512KB
165 println!(
166 "Set max cache size to: {} bytes",
167 cache_manager.max_cache_size()
168 );
169
170 // Add a large file that would exceed the new limit
171 let very_largedata = vec![3u8; 400 * 1024]; // 400KB
172 cache
173 .write_cached("very_large_file.dat", &very_largedata)
174 .unwrap();
175
176 let final_stats = cache_manager.get_detailed_stats().unwrap();
177 println!(
178 "Final cache size: {} (should be within limit)",
179 final_stats.formatted_size()
180 );
181 println!(
182 "Final usage: {:.1}%",
183 final_stats.usage_percentage() * 100.0
184 );
185 println!();
186
187 // Demonstrate cache cleanup
188 println!("=== Cache Cleanup ==============================");
189 println!(
190 "Files before cleanup: {}",
191 cache_manager.list_cached_files().unwrap().len()
192 );
193 cache_manager.cleanup_old_files(100 * 1024).unwrap(); // Clean up to fit 100KB
194 let cleanup_stats = cache_manager.get_detailed_stats().unwrap();
195 println!("Files after cleanup: {}", cleanup_stats.file_count);
196 println!("Size after cleanup: {}", cleanup_stats.formatted_size());
197 println!();
198
199 // Demonstrate cache report
200 println!("=== Complete Cache Report ======================");
201 cache_manager.print_cache_report().unwrap();
202 println!();
203
204 // Clear all cache data
205 println!("=== Cache Clearing =============================");
206 cache_manager.clear_all().unwrap();
207 let empty_stats = cache_manager.get_stats();
208 println!("Files after clearing: {}", empty_stats.file_count);
209 println!("Size after clearing: {}", empty_stats.formatted_size());
210 println!();
211
212 // Demonstrate configuration examples
213 println!("=== Configuration Examples =====================");
214 println!("Example configurations for different use cases:");
215 println!();
216
217 println!("1. Development (small cache, frequent cleanup):");
218 println!(" CacheManager::with_full_config(cachedir, 20, 1800, 50*1024*1024, false)");
219 println!(" - 20 items in memory, 30 min TTL, 50MB disk limit");
220 println!();
221
222 println!("2. Production (large cache, longer retention):");
223 println!(" CacheManager::with_full_config(cachedir, 500, 86400, 1024*1024*1024, false)");
224 println!(" - 500 items in memory, 24 hour TTL, 1GB disk limit");
225 println!();
226
227 println!("3. Offline environment:");
228 println!(" CacheManager::with_full_config(cachedir, 100, 3600, 0, true)");
229 println!(" - Offline mode enabled, unlimited disk cache");
230 println!();
231
232 println!("4. Memory-constrained (minimal cache):");
233 println!(" CacheManager::with_full_config(cachedir, 10, 900, 10*1024*1024, false)");
234 println!(" - 10 items in memory, 15 min TTL, 10MB disk limit");
235 println!();
236
237 println!("=== Cache Management Demo Complete =============");
238}Sourcepub fn print_cache_report(&self) -> Result<()>
pub fn print_cache_report(&self) -> Result<()>
Print detailed cache report
Examples found in repository?
examples/cache_management_demo.rs (line 201)
10fn main() {
11 println!("=== Enhanced Cache Management Demonstration ===\n");
12
13 // Demonstrate platform-specific cache directory detection
14 println!("=== Platform-Specific Cache Directory =========");
15 match get_cachedir() {
16 Ok(cachedir) => {
17 println!("Default cache directory: {}", cachedir.display());
18 println!("Platform: {}", std::env::consts::OS);
19 }
20 Err(e) => {
21 println!("Error getting cache directory: {e}");
22 }
23 }
24 println!();
25
26 // Demonstrate environment variable override
27 println!("=== Environment Variable Configuration =========");
28 println!("Set SCIRS2_CACHE_DIR to override default cache location");
29 println!("Set SCIRS2_OFFLINE=true to enable offline mode");
30 if let Ok(cache_env) = std::env::var("SCIRS2_CACHE_DIR") {
31 println!("Custom cache directory: {cache_env}");
32 } else {
33 println!("Using default cache directory");
34 }
35
36 if let Ok(offline_env) = std::env::var("SCIRS2_OFFLINE") {
37 println!("Offline mode: {offline_env}");
38 } else {
39 println!("Offline mode: Not set (defaults to false)");
40 }
41 println!();
42
43 // Create a temporary cache for demonstration
44 let tempdir = tempfile::tempdir().unwrap();
45 let demo_cachedir = tempdir.path().join("demo_cache");
46
47 // Demonstrate cache with size limits
48 println!("=== Cache with Size Limits =====================");
49 let mut cache_manager = CacheManager::with_full_config(
50 demo_cachedir.clone(),
51 50, // 50 items in memory cache
52 3600, // 1 hour TTL
53 1024 * 1024, // 1MB disk cache limit
54 false, // Not in offline mode
55 );
56
57 println!("Created cache with 1MB size limit");
58 println!("Cache directory: {}", demo_cachedir.display());
59
60 // Add some test data to the cache
61 let cache = DatasetCache::with_full_config(demo_cachedir.clone(), 50, 3600, 1024 * 1024, false);
62
63 // Write several files of different sizes
64 let smalldata = vec![0u8; 1024]; // 1KB
65 let mediumdata = vec![1u8; 10240]; // 10KB
66 let largedata = vec![2u8; 102400]; // 100KB
67
68 cache.write_cached("small_file.dat", &smalldata).unwrap();
69 cache.write_cached("medium_file.dat", &mediumdata).unwrap();
70 cache.write_cached("large_file.dat", &largedata).unwrap();
71
72 println!("Added test files to cache");
73 println!();
74
75 // Demonstrate basic cache statistics
76 println!("=== Basic Cache Statistics ====================");
77 let basic_stats = cache_manager.get_stats();
78 println!("Files: {}", basic_stats.file_count);
79 println!("Total size: {}", basic_stats.formatted_size());
80 println!();
81
82 // Demonstrate detailed cache statistics
83 println!("=== Detailed Cache Statistics ==================");
84 match cache_manager.get_detailed_stats() {
85 Ok(detailed_stats) => {
86 println!("Cache Directory: {}", detailed_stats.cachedir.display());
87 println!(
88 "Total Size: {} ({} files)",
89 detailed_stats.formatted_size(),
90 detailed_stats.file_count
91 );
92 println!("Max Size: {}", detailed_stats.formatted_max_size());
93 println!("Usage: {:.1}%", detailed_stats.usage_percentage() * 100.0);
94 println!(
95 "Offline Mode: {}",
96 if detailed_stats.offline_mode {
97 "Enabled"
98 } else {
99 "Disabled"
100 }
101 );
102
103 if !detailed_stats.files.is_empty() {
104 println!("\nCached Files (sorted by size):");
105 for file in &detailed_stats.files {
106 println!(
107 " {} - {} (modified {})",
108 file.name,
109 file.formatted_size(),
110 file.formatted_modified()
111 );
112 }
113 }
114 }
115 Err(e) => {
116 println!("Error getting detailed stats: {e}");
117 }
118 }
119 println!();
120
121 // Demonstrate cache management operations
122 println!("=== Cache Management Operations ===============");
123 println!("Available operations:");
124 println!("1. List cached files");
125 let cached_files = cache_manager.list_cached_files().unwrap();
126 for file in &cached_files {
127 println!(" - {file}");
128 }
129
130 println!("2. Check if specific files are cached");
131 println!(
132 " small_file.dat: {}",
133 cache_manager.is_cached("small_file.dat")
134 );
135 println!(
136 " nonexistent.dat: {}",
137 cache_manager.is_cached("nonexistent.dat")
138 );
139
140 println!("3. Remove specific file");
141 cache_manager.remove("medium_file.dat").unwrap();
142 println!(" Removed medium_file.dat");
143 println!(
144 " Files remaining: {}",
145 cache_manager.list_cached_files().unwrap().len()
146 );
147 println!();
148
149 // Demonstrate offline mode
150 println!("=== Offline Mode Configuration ================");
151 println!("Current offline mode: {}", cache_manager.is_offline());
152 cache_manager.set_offline_mode(true);
153 println!("Enabled offline mode: {}", cache_manager.is_offline());
154 cache_manager.set_offline_mode(false);
155 println!("Disabled offline mode: {}", cache_manager.is_offline());
156 println!();
157
158 // Demonstrate cache size management
159 println!("=== Cache Size Management =====================");
160 println!(
161 "Current max cache size: {} bytes",
162 cache_manager.max_cache_size()
163 );
164 cache_manager.set_max_cache_size(512 * 1024); // 512KB
165 println!(
166 "Set max cache size to: {} bytes",
167 cache_manager.max_cache_size()
168 );
169
170 // Add a large file that would exceed the new limit
171 let very_largedata = vec![3u8; 400 * 1024]; // 400KB
172 cache
173 .write_cached("very_large_file.dat", &very_largedata)
174 .unwrap();
175
176 let final_stats = cache_manager.get_detailed_stats().unwrap();
177 println!(
178 "Final cache size: {} (should be within limit)",
179 final_stats.formatted_size()
180 );
181 println!(
182 "Final usage: {:.1}%",
183 final_stats.usage_percentage() * 100.0
184 );
185 println!();
186
187 // Demonstrate cache cleanup
188 println!("=== Cache Cleanup ==============================");
189 println!(
190 "Files before cleanup: {}",
191 cache_manager.list_cached_files().unwrap().len()
192 );
193 cache_manager.cleanup_old_files(100 * 1024).unwrap(); // Clean up to fit 100KB
194 let cleanup_stats = cache_manager.get_detailed_stats().unwrap();
195 println!("Files after cleanup: {}", cleanup_stats.file_count);
196 println!("Size after cleanup: {}", cleanup_stats.formatted_size());
197 println!();
198
199 // Demonstrate cache report
200 println!("=== Complete Cache Report ======================");
201 cache_manager.print_cache_report().unwrap();
202 println!();
203
204 // Clear all cache data
205 println!("=== Cache Clearing =============================");
206 cache_manager.clear_all().unwrap();
207 let empty_stats = cache_manager.get_stats();
208 println!("Files after clearing: {}", empty_stats.file_count);
209 println!("Size after clearing: {}", empty_stats.formatted_size());
210 println!();
211
212 // Demonstrate configuration examples
213 println!("=== Configuration Examples =====================");
214 println!("Example configurations for different use cases:");
215 println!();
216
217 println!("1. Development (small cache, frequent cleanup):");
218 println!(" CacheManager::with_full_config(cachedir, 20, 1800, 50*1024*1024, false)");
219 println!(" - 20 items in memory, 30 min TTL, 50MB disk limit");
220 println!();
221
222 println!("2. Production (large cache, longer retention):");
223 println!(" CacheManager::with_full_config(cachedir, 500, 86400, 1024*1024*1024, false)");
224 println!(" - 500 items in memory, 24 hour TTL, 1GB disk limit");
225 println!();
226
227 println!("3. Offline environment:");
228 println!(" CacheManager::with_full_config(cachedir, 100, 3600, 0, true)");
229 println!(" - Offline mode enabled, unlimited disk cache");
230 println!();
231
232 println!("4. Memory-constrained (minimal cache):");
233 println!(" CacheManager::with_full_config(cachedir, 10, 900, 10*1024*1024, false)");
234 println!(" - 10 items in memory, 15 min TTL, 10MB disk limit");
235 println!();
236
237 println!("=== Cache Management Demo Complete =============");
238}Auto Trait Implementations§
impl !Freeze for CacheManager
impl !RefUnwindSafe for CacheManager
impl Send for CacheManager
impl !Sync for CacheManager
impl Unpin for CacheManager
impl UnwindSafe for CacheManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.