Skip to main content

JavaCache

Struct JavaCache 

Source
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

Source

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.

Source

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));
Source

pub fn get_or_refresh<F>( &mut self, fetcher: F, ) -> Result<&[JavaInfo], JavaError>
where F: Fn() -> Result<Vec<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());
Source

pub fn force_refresh<F>(&mut self, fetcher: F) -> Result<&[JavaInfo], JavaError>
where F: Fn() -> Result<Vec<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());
Source

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§

Source§

impl Debug for JavaCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for JavaCache

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.