pub struct JavaCache { /* private fields */ }Expand description
A simple TTL-based cache for Java search results.
Java installations rarely change during a session, so caching
avoids repeated full-disk scans. Call get_or_refresh to
retrieve cached results or run a fetcher when the TTL expires.
Implementations§
Source§impl JavaCache
impl JavaCache
Sourcepub fn new(ttl: Duration) -> Self
pub fn new(ttl: Duration) -> Self
Create a new cache with a given TTL.
After ttl elapses, the next call to get_or_refresh
will run the fetcher again.
Sourcepub fn ttl(self, ttl: Duration) -> Self
pub fn ttl(self, ttl: Duration) -> Self
Set a custom TTL (builder-style).
§Examples
use java_manager::JavaCache;
use std::time::Duration;
let cache = JavaCache::new(Duration::from_secs(60)).ttl(Duration::from_secs(30));Sourcepub fn get_or_refresh<F>(
&mut self,
fetcher: F,
) -> Result<&[JavaInfo], JavaError>
pub fn get_or_refresh<F>( &mut self, fetcher: F, ) -> Result<&[JavaInfo], JavaError>
Return cached results if they are still fresh, otherwise
run fetcher and cache the new results.
§Errors
Propagates any error returned by fetcher.
§Examples
use java_manager::{JavaCache, full_search};
use std::time::Duration;
let mut cache = JavaCache::new(Duration::from_secs(300));
let javas = cache.get_or_refresh(|| full_search())?;
println!("Found {} Java(s)", javas.len());Sourcepub fn force_refresh<F>(&mut self, fetcher: F) -> Result<&[JavaInfo], JavaError>
pub fn force_refresh<F>(&mut self, fetcher: F) -> Result<&[JavaInfo], JavaError>
Force a refresh, ignoring the TTL.
§Errors
Propagates any error returned by fetcher.
§Examples
use java_manager::{JavaCache, full_search};
use std::time::Duration;
let mut cache = JavaCache::new(Duration::from_secs(300));
let javas = cache.force_refresh(|| full_search())?;
println!("Found {} Java(s)", javas.len());Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear the cache.
After calling clear(), the next get_or_refresh
call will always run the fetcher regardless of the TTL.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for JavaCache
impl RefUnwindSafe for JavaCache
impl Send for JavaCache
impl Sync for JavaCache
impl Unpin for JavaCache
impl UnsafeUnpin for JavaCache
impl UnwindSafe for JavaCache
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