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
impl SourceCache
Sourcepub fn find_by_host(&self, host: &str, timeout: Duration) -> Result<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 fortimeout- Maximum time to wait for source discovery. Must not exceedcrate::MAX_TIMEOUT(~49.7 days).
§Returns
The discovered source, or an error if no matching source is found or the timeout expires.
§Errors
Error::NoSourcesFoundif no source matching the host is discoveredError::InvalidConfigurationiftimeoutexceedscrate::MAX_TIMEOUT- Other errors if NDI initialization or discovery fails
§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))?;Sourcepub fn invalidate(&self, host: &str)
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 discoverySourcepub fn clear(&self)
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();Sourcepub fn len(&self) -> usize
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);