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. Discovery initializes the runtime on a cache miss. Cached sources own their strings and need no live runtime. Entries remain cached until explicitly invalidated or cleared. Equivalent hostname case/final-dot and numeric IP spellings share one entry. Hostnames, IP addresses, and DNS aliases otherwise remain distinct identities.

§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() -> Self

Create a new source cache.

§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 the machine component of an SDK source 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. Discovery started before this call may return to its caller, but cannot repopulate the cache afterward. Any accepted equivalent spelling removes the same host identity. Malformed input removes no entry but still invalidates pending discovery.

§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. Pending discovery cannot repopulate the cache after this call returns.

§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 distinct cached host identities.

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.