1use aspect_core::{Aspect, AspectError, ProceedingJoinPoint};
4use std::any::Any;
5use std::time::Duration;
6
7#[derive(Clone)]
25pub struct CachingAspect {
26 max_size: usize,
27 ttl: Option<Duration>,
28}
29
30impl CachingAspect {
31 pub fn new() -> Self {
33 Self {
34 max_size: usize::MAX,
35 ttl: None,
36 }
37 }
38
39 pub fn with_max_size(mut self, max_size: usize) -> Self {
41 self.max_size = max_size;
42 self
43 }
44
45 pub fn with_ttl(mut self, ttl: Duration) -> Self {
47 self.ttl = Some(ttl);
48 self
49 }
50}
51
52impl Default for CachingAspect {
53 fn default() -> Self {
54 Self::new()
55 }
56}
57
58impl Aspect for CachingAspect {
59 fn around(&self, pjp: ProceedingJoinPoint) -> Result<Box<dyn Any>, AspectError> {
60 println!("[CACHE] Checking cache for {}", pjp.context().function_name);
63 pjp.proceed()
64 }
65}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70
71 #[test]
72 fn test_caching_aspect() {
73 let aspect = CachingAspect::new()
74 .with_max_size(100)
75 .with_ttl(Duration::from_secs(60));
76
77 assert_eq!(aspect.max_size, 100);
78 assert_eq!(aspect.ttl, Some(Duration::from_secs(60)));
79 }
80}