ResponseCache

Struct ResponseCache 

Source
#[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
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional 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

Source

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 - An Option<String> representing the name of the file where the cache will be stored. If None, 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()));
Source

pub fn clear(&mut self)

Clears the cache.

§Examples
let mut cache = ResponseCache::default();
cache.clear();
assert!(cache.current_response().is_none());
Source

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 - A LookupResponse instance representing the new address to be cached.
  • ttl - An Option<u64> representing the time-to-live (TTL) in seconds for the new cached response. If None, the cache never expires.
Source

pub fn current_is_expired(&self) -> bool

Checks if the current_address cache entry has expired.

Source

pub fn current_ip(&self) -> Option<IpAddr>

Returns the IP address of the current host cache entry.

Source

pub fn current_response(&self) -> Option<LookupResponse>

Returns the current_address cache entry.

Source

pub fn update_target( &mut self, ip: IpAddr, response: &LookupResponse, ttl: Option<u64>, )

Updates the lookup cache with a new response.

Source

pub fn target_is_expired(&self, ip: &IpAddr) -> bool

Checks if the lookup cache entry for the given IP address has expired.

Source

pub fn target_response(&self, ip: &IpAddr) -> Option<LookupResponse>

Returns lookup cached entry for the given IP address.

Source

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();
Source

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 - An Option<String> representing the name of the file from which the cache will be loaded. If None, the default file name lookup.cache will be used.
§Examples
let cache = ResponseCache::load(Some("cache.txt".to_string()));
Source

pub fn delete(self) -> Result<()>

Deletes the ResponseCache instance from disk.

Trait Implementations§

Source§

impl Debug for ResponseCache

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ResponseCache

Source§

fn default() -> ResponseCache

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for ResponseCache

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for ResponseCache

Source§

fn eq(&self, other: &ResponseCache) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for ResponseCache

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for ResponseCache

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,