#[non_exhaustive]pub struct ResponseCache {
pub current_address: Option<ResponseRecord>,
pub lookup_address: BTreeMap<IpAddr, ResponseRecord>,
/* private fields */
}Expand description
Holds the current IP address lookup response
The cache can be saved to disk, loaded from disk, and deleted from disk. It also provides methods to clear the cache, update the cache with a new response, check if the cache has expired, and retrieve the IP address or the entire response from the cache.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.current_address: Option<ResponseRecord>The current IP address lookup response
lookup_address: BTreeMap<IpAddr, ResponseRecord>A tree of arbitrary IP address responses
Implementations§
Source§impl ResponseCache
impl ResponseCache
Sourcepub fn new(file_name: Option<String>) -> ResponseCache
pub fn new(file_name: Option<String>) -> ResponseCache
Creates a new ResponseCache instance.
The ResponseRecord is stored as the current_address in the ResponseCache.
§Arguments
file_name- AnOption<String>representing the name of the file where the cache will be stored. IfNone, no file will be used.
§Examples
let response = LookupResponse::new(
"1.1.1.1".parse::<std::net::IpAddr>().unwrap(),
LookupProvider::IpBase);
let mut cache = ResponseCache::new(None);
cache.update_current(&response, None);let cache = ResponseCache::new(Some("cache.txt".to_string()));Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the cache.
§Examples
let mut cache = ResponseCache::default();
cache.clear();
assert!(cache.current_response().is_none());Sourcepub fn update_current(&mut self, response: &LookupResponse, ttl: Option<u64>)
pub fn update_current(&mut self, response: &LookupResponse, ttl: Option<u64>)
Updates the cache entry for the current host with a new response.
§Arguments
response- ALookupResponseinstance representing the new address to be cached.ttl- AnOption<u64>representing the time-to-live (TTL) in seconds for the new cached response. IfNone, the cache never expires.
Sourcepub fn current_is_expired(&self) -> bool
pub fn current_is_expired(&self) -> bool
Checks if the current_address cache entry has expired.
Sourcepub fn current_ip(&self) -> Option<IpAddr>
pub fn current_ip(&self) -> Option<IpAddr>
Returns the IP address of the current host cache entry.
Sourcepub fn current_response(&self) -> Option<LookupResponse>
pub fn current_response(&self) -> Option<LookupResponse>
Returns the current_address cache entry.
Sourcepub fn update_target(
&mut self,
ip: IpAddr,
response: &LookupResponse,
ttl: Option<u64>,
)
pub fn update_target( &mut self, ip: IpAddr, response: &LookupResponse, ttl: Option<u64>, )
Updates the lookup cache with a new response.
Sourcepub fn target_is_expired(&self, ip: &IpAddr) -> bool
pub fn target_is_expired(&self, ip: &IpAddr) -> bool
Checks if the lookup cache entry for the given IP address has expired.
Sourcepub fn target_response(&self, ip: &IpAddr) -> Option<LookupResponse>
pub fn target_response(&self, ip: &IpAddr) -> Option<LookupResponse>
Returns lookup cached entry for the given IP address.
Sourcepub fn save(&self) -> Result<()>
pub fn save(&self) -> Result<()>
Writes the ResponseCache instance to a file on disk.
This method serializes the ResponseCache instance into a JSON string, encrypts the data if the “encryption” feature is enabled,
and then writes the encrypted (or plain text) data to a file. The file is located at the path specified by the file_name field of the ResponseCache instance.
§Examples
let cache = ResponseCache::new(Some("cache.txt".to_string()));
_ = cache.save();Sourcepub fn load(file_name: Option<String>) -> Result<ResponseCache>
pub fn load(file_name: Option<String>) -> Result<ResponseCache>
Loads the ResponseCache instance from a file on disk.
This method reads the file specified by file_name, decrypts the data if the “encryption” feature is enabled,
and then deserializes the data into a ResponseCache instance.
§Arguments
file_name- AnOption<String>representing the name of the file from which the cache will be loaded. IfNone, the default file namelookup.cachewill be used.
§Examples
let cache = ResponseCache::load(Some("cache.txt".to_string()));