Skip to main content

SourceCache

Struct SourceCache 

Source
pub struct SourceCache { /* private fields */ }
Expand description

Thread-safe cache for NDI source discovery.

SourceCache eliminates the need for applications to manually cache NDI instances and discovered sources. It handles expensive NDI initialization and source discovery operations internally with built-in caching.

§Thread Safety

SourceCache is thread-safe and can be shared across threads using Arc<SourceCache>. Interior mutability is handled internally with proper synchronization.

§Examples

use grafton_ndi::SourceCache;
use std::time::Duration;

// Create a cache instance
let cache = SourceCache::new()?;

// Find a source by hostname or IP with automatic caching
let source = cache.find_by_host("192.168.0.107", Duration::from_secs(5))?;
println!("Found source: {}", source);

// Subsequent lookups use the cache
let same_source = cache.find_by_host("192.168.0.107", Duration::from_secs(5))?;

Implementations§

Source§

impl SourceCache

Source

pub fn new() -> Result<Self>

Create a new source cache.

§Errors

Returns an error if the NDI runtime cannot be initialized.

§Examples
use grafton_ndi::SourceCache;

let cache = SourceCache::new()?;
Source

pub fn find_by_host(&self, host: &str, timeout: Duration) -> Result<Source>

Find a source by IP address or hostname with built-in caching.

This method handles NDI initialization and source discovery internally. If a source matching the host has been previously found, it returns the cached result. Otherwise, it performs NDI discovery and caches the result.

§Arguments
  • host - The hostname or IP address to search for
  • timeout - Maximum time to wait for source discovery. Must not exceed crate::MAX_TIMEOUT (~49.7 days).
§Returns

The discovered source, or an error if no matching source is found or the timeout expires.

§Errors
§Examples
use grafton_ndi::SourceCache;
use std::time::Duration;

let cache = SourceCache::new()?;

// Find by IP address
let source = cache.find_by_host("192.168.0.107", Duration::from_secs(5))?;

// Find by partial IP
let source = cache.find_by_host("192.168.0", Duration::from_secs(5))?;

// Find by name
let source = cache.find_by_host("CAMERA1", Duration::from_secs(5))?;
Source

pub fn invalidate(&self, host: &str)

Invalidate the cache entry for a specific host.

This is useful when a source goes offline or when you want to force a fresh discovery on the next lookup.

§Arguments
  • host - The hostname or IP address to remove from the cache
§Examples
use grafton_ndi::SourceCache;
use std::time::Duration;

let cache = SourceCache::new()?;
let source = cache.find_by_host("192.168.0.107", Duration::from_secs(5))?;

// Later, if the source goes offline
cache.invalidate("192.168.0.107");

// Next lookup will perform fresh discovery
Source

pub fn clear(&self)

Clear all cached sources.

This removes all entries from the cache, forcing fresh discovery for all subsequent lookups.

§Examples
use grafton_ndi::SourceCache;
use std::time::Duration;

let cache = SourceCache::new()?;
cache.find_by_host("192.168.0.107", Duration::from_secs(5))?;
cache.find_by_host("192.168.0.108", Duration::from_secs(5))?;

// Clear all cached sources
cache.clear();
Source

pub fn len(&self) -> usize

Get the number of cached sources.

This can be useful for monitoring cache usage and debugging.

§Examples
use grafton_ndi::SourceCache;
use std::time::Duration;

let cache = SourceCache::new()?;
assert_eq!(cache.len(), 0);

cache.find_by_host("192.168.0.107", Duration::from_secs(5))?;
assert_eq!(cache.len(), 1);
Source

pub fn is_empty(&self) -> bool

Check if the cache is empty.

§Examples
use grafton_ndi::SourceCache;
use std::time::Duration;

let cache = SourceCache::new()?;
assert!(cache.is_empty());

cache.find_by_host("192.168.0.107", Duration::from_secs(5))?;
assert!(!cache.is_empty());

Trait Implementations§

Source§

impl Default for SourceCache

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.