Trait Cacheable

Source
pub trait Cacheable {
    // Required methods
    fn cache_key(&self) -> Result<String, CacheError>;
    fn cache_key_prefix(&self) -> String;

    // Provided methods
    fn cache_ttl(&self) -> u32 { ... }
    fn cache_stale_ttl(&self) -> u32 { ... }
    fn cache_version(&self) -> u32 { ... }
}
Expand description

Trait describes cache configuration per type that implements this trait.

Required Methods§

Source

fn cache_key(&self) -> Result<String, CacheError>

Method should return unique identifier for struct object.

In cache storage it may prepends with cache version and Upstream name.

§Examples
use hitbox::cache::Cacheable;
use hitbox::CacheError;

struct QueryNothing {
    id: Option<i32>,
}

impl Cacheable for QueryNothing {
    fn cache_key(&self) -> Result<String, CacheError> {
        let key = format!("{}::id::{}", self.cache_key_prefix(), self.id.map_or_else(
            || "None".to_owned(), |id| id.to_string())
        );
        Ok(key)
    }
    fn cache_key_prefix(&self) -> String { "database::QueryNothing".to_owned() }
}

let query = QueryNothing { id: Some(1) };
assert_eq!(query.cache_key().unwrap(), "database::QueryNothing::id::1");
let query = QueryNothing { id: None };
assert_eq!(query.cache_key().unwrap(), "database::QueryNothing::id::None");
Source

fn cache_key_prefix(&self) -> String

Method return cache key prefix based on message type.

Provided Methods§

Source

fn cache_ttl(&self) -> u32

Describe time-to-live (ttl) value for cache storage in seconds.

After that time value will be removed from cache storage.

Source

fn cache_stale_ttl(&self) -> u32

Describe expire\stale timeout value for cache storage in seconds.

After that time cached value marked as stale.

|__cache_is_valid__|__cache_is_stale__| -> time
                   ^                  ^
                stale_ttl       ttl (cache evicted)
Source

fn cache_version(&self) -> u32

Describe current cache version for this type.

Implementors§