Matcher

Trait Matcher 

Source
pub trait Matcher<T> {
    // Required method
    fn matches(&self, key: &T) -> bool;
}
Expand description

Trait for implementing custom matching logic against cache keys.

This trait allows you to define complex search patterns for finding cached entries beyond simple exact key matching. Implementations can match based on patterns, ranges, regular expressions, or any custom logic.

§Examples

use simple_cacher::*;
use std::time::Duration;

// Custom matcher for email domains
struct DomainMatcher {
    domain: String,
}

impl Matcher<String> for DomainMatcher {
    fn matches(&self, email: &String) -> bool {
        email.ends_with(&format!("@{}", self.domain))
    }
}

let mut cache = SimpleCacher::new(Duration::from_secs(300));
cache.insert("alice@company.com".to_string(), "Alice".to_string());
cache.insert("bob@company.com".to_string(), "Bob".to_string());
cache.insert("charlie@gmail.com".to_string(), "Charlie".to_string());

let company_matcher = DomainMatcher { domain: "company.com".to_string() };
let company_users = cache.get_all_by_matcher(&company_matcher);
assert_eq!(company_users.len(), 2);

Required Methods§

Source

fn matches(&self, key: &T) -> bool

Returns true if the given key matches this matcher’s criteria.

§Arguments
  • key - The cache key to test against this matcher
§Returns

true if the key matches, false otherwise

Implementors§