Skip to main content

GenerationCache

Struct GenerationCache 

Source
pub struct GenerationCache<G> { /* private fields */ }
Expand description

Hot-reload graph cache keyed on an external generation counter.

G is any graph type. generation is a monotonic u64 controlled by the caller — bump it whenever the underlying data source changes (index commit, file-watch event, etc.).

Only one graph is cached at a time. Filtered or derived views must be computed from the cached graph by the caller.

§Examples

use petgraph_live::cache::GenerationCache;

let cache: GenerationCache<Vec<u32>> = GenerationCache::new();
assert_eq!(cache.current_generation(), None);

Implementations§

Source§

impl<G> GenerationCache<G>

Source

pub fn new() -> Self

Create an empty cache with no cached graph.

Source

pub fn get_or_build<F, E>(&self, generation: u64, build: F) -> Result<Arc<G>, E>
where F: FnOnce() -> Result<G, E>,

Return cached graph if generation matches, else call build and cache result.

build is called only on a miss or stale entry. On error from build, the existing cache entry is left unchanged.

Concurrent callers that both observe a miss may each call build independently. The last writer wins the cache slot. build must be idempotent — callers that need to prevent redundant work should serialize get_or_build at a higher level.

§Examples
use petgraph_live::cache::GenerationCache;
use std::sync::Arc;

let cache: GenerationCache<Vec<u32>> = GenerationCache::new();
let g1: Arc<Vec<u32>> = cache.get_or_build(1, || Ok::<_, ()>(vec![1, 2, 3])).unwrap();
assert_eq!(*g1, vec![1, 2, 3]);

// Same generation — cached Arc returned, build closure not called.
let g2 = cache.get_or_build(1, || Ok::<_, ()>(vec![9, 9])).unwrap();
assert!(Arc::ptr_eq(&g1, &g2));
Source

pub fn invalidate(&self)

Force cache invalidation. Next get_or_build call always rebuilds.

Source

pub fn current_generation(&self) -> Option<u64>

Generation of the currently cached graph, or None if cache is empty.

Trait Implementations§

Source§

impl<G> Default for GenerationCache<G>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<G> !Freeze for GenerationCache<G>

§

impl<G> RefUnwindSafe for GenerationCache<G>

§

impl<G> Send for GenerationCache<G>
where G: Sync + Send,

§

impl<G> Sync for GenerationCache<G>
where G: Sync + Send,

§

impl<G> Unpin for GenerationCache<G>

§

impl<G> UnsafeUnpin for GenerationCache<G>

§

impl<G> UnwindSafe for GenerationCache<G>

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.