pub struct DnsCache { /* private fields */ }Expand description
A concurrent DNS cache with background prefetching.
Caches DNS resolution results with configurable TTL and provides
background prefetching for upcoming URLs. Uses DashMap for
concurrent access without external locking.
§Examples
use crawlkit_engine::DnsCache;
use std::time::Duration;
let cache = DnsCache::new(Duration::from_secs(300), 10_000);
cache.insert("example.com", vec!["93.184.216.34:443".parse().unwrap()]);
assert!(cache.get_cached("example.com").is_some());Implementations§
Source§impl DnsCache
impl DnsCache
Sourcepub fn new(default_ttl: Duration, max_entries: usize) -> Self
pub fn new(default_ttl: Duration, max_entries: usize) -> Self
Creates a new DNS cache with the given TTL and capacity.
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Creates a DNS cache with sensible defaults (5 min TTL, 10k entries).
Sourcepub async fn resolve(&self, domain: &str) -> Result<Vec<SocketAddr>, DnsError>
pub async fn resolve(&self, domain: &str) -> Result<Vec<SocketAddr>, DnsError>
Resolves a domain, using the cache if available and not expired.
Sourcepub async fn resolve_url(&self, url: &Url) -> Result<Vec<SocketAddr>, DnsError>
pub async fn resolve_url(&self, url: &Url) -> Result<Vec<SocketAddr>, DnsError>
Resolves a domain from a URL.
Sourcepub fn insert(&self, domain: &str, addresses: Vec<SocketAddr>)
pub fn insert(&self, domain: &str, addresses: Vec<SocketAddr>)
Inserts a pre-resolved entry into the cache.
Sourcepub fn get_cached(&self, domain: &str) -> Option<Vec<SocketAddr>>
pub fn get_cached(&self, domain: &str) -> Option<Vec<SocketAddr>>
Returns cached addresses for a domain without doing DNS resolution.
Sourcepub fn enqueue_prefetch(&self, domains: Vec<String>)
pub fn enqueue_prefetch(&self, domains: Vec<String>)
Queues domains for background prefetching.
Sourcepub fn dequeue_prefetch(&self, batch_size: usize) -> Vec<String>
pub fn dequeue_prefetch(&self, batch_size: usize) -> Vec<String>
Dequeues domains for prefetching (called by the background task).
Sourcepub fn memory_usage(&self) -> usize
pub fn memory_usage(&self) -> usize
Returns the approximate memory usage in bytes.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for DnsCache
impl !RefUnwindSafe for DnsCache
impl !UnwindSafe for DnsCache
impl Send for DnsCache
impl Sync for DnsCache
impl Unpin for DnsCache
impl UnsafeUnpin for DnsCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more