reinhardt-utils 0.1.0-rc.22

Utility functions aggregator for Reinhardt
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! File-based cache implementation

use super::cache_trait::Cache;
use super::entry::CacheEntry;
use async_trait::async_trait;
use reinhardt_core::exception::{Error, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::Duration;
use tokio::fs;
use tokio::sync::RwLock;

/// File-based cache backend
///
/// Persists cache entries to the filesystem with TTL support.
/// Cache files are stored in a directory with hashed filenames for safety.
///
/// # Examples
///
/// ```
/// use reinhardt_utils::cache::{Cache, FileCache};
/// use std::path::PathBuf;
///
/// # async fn example() -> reinhardt_core::exception::Result<()> {
/// let cache = FileCache::new(PathBuf::from("/tmp/cache")).await?;
///
/// // Set a value
/// cache.set("key", &"value", None).await?;
///
/// // Get a value
/// let value: Option<String> = cache.get("key").await?;
/// assert_eq!(value, Some("value".to_string()));
///
/// // Delete a value
/// cache.delete("key").await?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct FileCache {
	cache_dir: PathBuf,
	default_ttl: Option<Duration>,
	index: std::sync::Arc<RwLock<HashMap<String, PathBuf>>>,
}

impl FileCache {
	/// Create a new file-based cache
	///
	/// Creates the cache directory if it doesn't exist.
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_utils::cache::FileCache;
	/// use std::path::PathBuf;
	///
	/// # async fn example() -> reinhardt_core::exception::Result<()> {
	/// let cache = FileCache::new(PathBuf::from("/tmp/my_cache")).await?;
	/// # Ok(())
	/// # }
	/// ```
	pub async fn new(cache_dir: PathBuf) -> Result<Self> {
		fs::create_dir_all(&cache_dir)
			.await
			.map_err(|e| Error::Internal(format!("Failed to create cache directory: {}", e)))?;

		Ok(Self {
			cache_dir,
			default_ttl: None,
			index: std::sync::Arc::new(RwLock::new(HashMap::new())),
		})
	}

	/// Set a default TTL for all cache entries
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_utils::cache::{Cache, FileCache};
	/// use std::path::PathBuf;
	/// use std::time::Duration;
	///
	/// # async fn example() -> reinhardt_core::exception::Result<()> {
	/// let cache = FileCache::new(PathBuf::from("/tmp/cache"))
	///     .await?
	///     .with_default_ttl(Duration::from_secs(300));
	///
	/// // Values will expire after 300 seconds by default
	/// cache.set("key", &"value", None).await?;
	/// # Ok(())
	/// # }
	/// ```
	pub fn with_default_ttl(mut self, ttl: Duration) -> Self {
		self.default_ttl = Some(ttl);
		self
	}

	/// Clean up expired entries from the filesystem
	///
	/// # Examples
	///
	/// ```
	/// use reinhardt_utils::cache::{Cache, FileCache};
	/// use std::path::PathBuf;
	/// use std::time::Duration;
	///
	/// # async fn example() -> reinhardt_core::exception::Result<()> {
	/// let cache = FileCache::new(PathBuf::from("/tmp/cache")).await?;
	///
	/// // Set a value with short TTL
	/// cache.set("key", &"value", Some(Duration::from_millis(10))).await?;
	///
	/// // Wait for expiration
	///
	/// // Clean up expired entries
	/// cache.cleanup_expired().await?;
	/// # Ok(())
	/// # }
	/// ```
	pub async fn cleanup_expired(&self) -> Result<()> {
		let mut index = self.index.write().await;
		let mut to_remove = Vec::new();

		for (key, path) in index.iter() {
			if let Ok(data) = fs::read(path).await
				&& let Ok(entry) = serde_json::from_slice::<CacheEntry>(&data)
				&& entry.is_expired()
			{
				to_remove.push((key.clone(), path.clone()));
			}
		}

		for (key, path) in to_remove {
			let _ = fs::remove_file(&path).await;
			index.remove(&key);
		}

		Ok(())
	}

	/// Get the file path for a cache key
	fn get_file_path(&self, key: &str) -> PathBuf {
		// Hash the key to create a safe filename using SHA-256
		use sha2::{Digest, Sha256};
		let hash = format!("{:x}", Sha256::digest(key.as_bytes()));
		self.cache_dir.join(hash)
	}

	/// Load the cache index from filesystem
	// Reserved for future cache persistence recovery
	#[allow(dead_code)]
	async fn load_index(&self) -> Result<()> {
		let mut index = self.index.write().await;
		index.clear();

		let mut entries = fs::read_dir(&self.cache_dir)
			.await
			.map_err(|e| Error::Internal(format!("Failed to read cache directory: {}", e)))?;

		while let Some(entry) = entries
			.next_entry()
			.await
			.map_err(|e| Error::Internal(format!("Failed to read directory entry: {}", e)))?
		{
			let path = entry.path();
			if path.is_file()
				&& let Ok(data) = fs::read(&path).await
				&& let Ok(cache_entry) = serde_json::from_slice::<StoredEntry>(&data)
				&& !cache_entry.entry.is_expired()
			{
				index.insert(cache_entry.key.clone(), path);
			}
		}

		Ok(())
	}
}

/// Entry stored in file system with key information
#[derive(Debug, Serialize, Deserialize)]
struct StoredEntry {
	key: String,
	entry: CacheEntry,
}

#[async_trait]
impl Cache for FileCache {
	async fn get<T>(&self, key: &str) -> Result<Option<T>>
	where
		T: for<'de> Deserialize<'de> + Send,
	{
		let path = self.get_file_path(key);

		if !path.exists() {
			return Ok(None);
		}

		let data = fs::read(&path)
			.await
			.map_err(|e| Error::Internal(format!("Failed to read cache file: {}", e)))?;

		let stored: StoredEntry =
			serde_json::from_slice(&data).map_err(|e| Error::Serialization(e.to_string()))?;

		if stored.entry.is_expired() {
			// Clean up expired file
			let _ = fs::remove_file(&path).await;
			let mut index = self.index.write().await;
			index.remove(key);
			return Ok(None);
		}

		let value = serde_json::from_slice(&stored.entry.value)
			.map_err(|e| Error::Serialization(e.to_string()))?;

		Ok(Some(value))
	}

	async fn set<T>(&self, key: &str, value: &T, ttl: Option<Duration>) -> Result<()>
	where
		T: Serialize + Send + Sync,
	{
		let serialized =
			serde_json::to_vec(value).map_err(|e| Error::Serialization(e.to_string()))?;

		let ttl = ttl.or(self.default_ttl);
		let entry = CacheEntry::new(serialized, ttl);

		let stored = StoredEntry {
			key: key.to_string(),
			entry,
		};

		let path = self.get_file_path(key);
		let data = serde_json::to_vec(&stored).map_err(|e| Error::Serialization(e.to_string()))?;

		fs::write(&path, data)
			.await
			.map_err(|e| Error::Internal(format!("Failed to write cache file: {}", e)))?;

		let mut index = self.index.write().await;
		index.insert(key.to_string(), path);

		Ok(())
	}

	async fn delete(&self, key: &str) -> Result<()> {
		let path = self.get_file_path(key);

		if path.exists() {
			fs::remove_file(&path)
				.await
				.map_err(|e| Error::Internal(format!("Failed to delete cache file: {}", e)))?;
		}

		let mut index = self.index.write().await;
		index.remove(key);

		Ok(())
	}

	async fn has_key(&self, key: &str) -> Result<bool> {
		let path = self.get_file_path(key);

		if !path.exists() {
			return Ok(false);
		}

		let data = fs::read(&path)
			.await
			.map_err(|e| Error::Internal(format!("Failed to read cache file: {}", e)))?;

		let stored: StoredEntry =
			serde_json::from_slice(&data).map_err(|e| Error::Serialization(e.to_string()))?;

		Ok(!stored.entry.is_expired())
	}

	async fn clear(&self) -> Result<()> {
		let mut index = self.index.write().await;

		for path in index.values() {
			let _ = fs::remove_file(path).await;
		}

		index.clear();

		Ok(())
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use std::time::Duration;

	/// Polls a condition until it returns true or timeout is reached.
	async fn poll_until<F, Fut>(
		timeout: std::time::Duration,
		interval: std::time::Duration,
		mut condition: F,
	) -> std::result::Result<(), String>
	where
		F: FnMut() -> Fut,
		Fut: std::future::Future<Output = bool>,
	{
		let start = std::time::Instant::now();
		while start.elapsed() < timeout {
			if condition().await {
				return Ok(());
			}
			tokio::time::sleep(interval).await;
		}
		Err(format!("Timeout after {:?} waiting for condition", timeout))
	}

	fn get_test_dir(name: &str) -> PathBuf {
		PathBuf::from(format!("/tmp/reinhardt_file_cache_test_{}", name))
	}

	async fn create_test_cache(name: &str) -> FileCache {
		let temp_dir = get_test_dir(name);
		// Clean up before test
		let _ = tokio::fs::remove_dir_all(&temp_dir).await;
		FileCache::new(temp_dir).await.unwrap()
	}

	#[tokio::test]
	async fn test_file_cache_basic() {
		let cache = create_test_cache("basic").await;

		// Set and get
		cache.set("key1", &"value1", None).await.unwrap();
		let value: Option<String> = cache.get("key1").await.unwrap();
		assert_eq!(value, Some("value1".to_string()));

		// Has key
		assert!(cache.has_key("key1").await.unwrap());
		assert!(!cache.has_key("key2").await.unwrap());

		// Delete
		cache.delete("key1").await.unwrap();
		let value: Option<String> = cache.get("key1").await.unwrap();
		assert_eq!(value, None);
	}

	#[tokio::test]
	async fn test_file_cache_ttl() {
		let cache = create_test_cache("ttl").await;

		// Set with short TTL
		cache
			.set("key1", &"value1", Some(Duration::from_millis(100)))
			.await
			.unwrap();

		// Should exist immediately
		let value: Option<String> = cache.get("key1").await.unwrap();
		assert_eq!(value, Some("value1".to_string()));

		// Poll until key expires
		poll_until(
			Duration::from_millis(200),
			Duration::from_millis(10),
			|| async {
				let value: Option<String> = cache.get("key1").await.unwrap();
				value.is_none()
			},
		)
		.await
		.expect("Key should expire within 200ms");
	}

	#[tokio::test]
	async fn test_file_cache_cleanup_expired() {
		let cache = create_test_cache("cleanup").await;

		// Set some values with different TTLs
		cache
			.set("key1", &"value1", Some(Duration::from_millis(100)))
			.await
			.unwrap();
		cache.set("key2", &"value2", None).await.unwrap();

		// Poll until first key expires
		poll_until(
			Duration::from_millis(200),
			Duration::from_millis(10),
			|| async {
				let value: Option<String> = cache.get("key1").await.unwrap();
				value.is_none()
			},
		)
		.await
		.expect("Key1 should expire within 200ms");

		// Cleanup expired entries
		cache.cleanup_expired().await.unwrap();

		// key1 should be gone, key2 should remain
		assert!(!cache.has_key("key1").await.unwrap());
		assert!(cache.has_key("key2").await.unwrap());
	}

	#[tokio::test]
	async fn test_file_cache_persistence() {
		let temp_dir = get_test_dir("persistence");
		let _ = tokio::fs::remove_dir_all(&temp_dir).await;

		{
			let cache = FileCache::new(temp_dir.clone()).await.unwrap();
			cache.set("key1", &"value1", None).await.unwrap();
			cache.set("key2", &"value2", None).await.unwrap();
		}

		// Create new cache instance with same directory
		{
			let cache = FileCache::new(temp_dir.clone()).await.unwrap();
			cache.load_index().await.unwrap();

			// Values should still exist
			let value: Option<String> = cache.get("key1").await.unwrap();
			assert_eq!(value, Some("value1".to_string()));

			let value: Option<String> = cache.get("key2").await.unwrap();
			assert_eq!(value, Some("value2".to_string()));
		}
	}

	#[tokio::test]
	async fn test_file_cache_clear() {
		let cache = create_test_cache("clear").await;

		cache.set("key1", &"value1", None).await.unwrap();
		cache.set("key2", &"value2", None).await.unwrap();

		cache.clear().await.unwrap();

		assert!(!cache.has_key("key1").await.unwrap());
		assert!(!cache.has_key("key2").await.unwrap());
	}
}