Skip to main content

Cache

Trait Cache 

Source
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§

Source

fn get(&self, key: &str) -> Option<IpInfo>

Returns the cached value for key, or None when absent or expired.

Source

fn set(&self, key: &str, value: IpInfo)

Stores value under key.

Source

fn invalidate(&self, key: &str)

Removes the entry for key, if present.

Source

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".

Implementations on Foreign Types§

Source§

impl<C: Cache + ?Sized> Cache for Arc<C>

Source§

fn get(&self, key: &str) -> Option<IpInfo>

Source§

fn set(&self, key: &str, value: IpInfo)

Source§

fn invalidate(&self, key: &str)

Source§

fn invalidate_all(&self)

Implementors§