pub trait Cache: Send + Sync {
// Required methods
fn get(&self, key: &str) -> Option<IpInfo>;
fn set(&self, key: &str, value: IpInfo);
fn invalidate(&self, key: &str);
fn invalidate_all(&self);
}Expand description
Abstracts the storage used by a Client to memoize IP
lookups. Implementations must be safe for concurrent use from multiple
threads and should never block for long, as they are called from async
contexts.
Only successful single and batch IP lookups are cached. Origin lookups are never cached, because the requester IP is only known from the response.
Keys are opaque strings derived from the looked-up IP address and the
request parameters. Cache is implemented for Arc<C>, so you can keep a
handle to a cache after handing it to a client:
use std::sync::Arc;
use ipregistry::{Cache, Client, InMemoryCache};
let cache = Arc::new(InMemoryCache::new());
let client = Client::builder("YOUR_API_KEY")
.cache(Arc::clone(&cache))
.build()
.unwrap();
// ... later:
cache.invalidate_all();Required Methods§
Sourcefn get(&self, key: &str) -> Option<IpInfo>
fn get(&self, key: &str) -> Option<IpInfo>
Returns the cached value for key, or None when absent or expired.
Sourcefn invalidate(&self, key: &str)
fn invalidate(&self, key: &str)
Removes the entry for key, if present.
Sourcefn invalidate_all(&self)
fn invalidate_all(&self)
Removes every entry.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".