CacheFeed

Trait CacheFeed 

Source
pub trait CacheFeed<T: CacheEntity>: Send {
    // Required methods
    fn entity_id(&mut self) -> T::Key;
    fn feed(&mut self, entity: Option<T>);

    // Provided methods
    fn validate(&self) -> Result<()> { ... }
    fn on_loaded(&mut self, _entity: &T) -> Result<()> { ... }
    fn on_miss(&mut self, _key: &str) -> Result<()> { ... }
    fn on_hit(&mut self, _key: &str) -> Result<()> { ... }
}
Expand description

Generic trait for consuming cached data from operations.

Replaces specific feeder traits with a single generic abstraction.

§Example

use cache_kit::{CacheFeed, CacheEntity};
use serde::{Deserialize, Serialize};

#[derive(Clone, Serialize, Deserialize)]
struct Employment {
    id: String,
    name: String,
}

impl CacheEntity for Employment {
    type Key = String;
    fn cache_key(&self) -> Self::Key { self.id.clone() }
    fn cache_prefix() -> &'static str { "employment" }
}

struct EmploymentFeeder {
    id: String,
    employment: Option<Employment>,
}

impl CacheFeed<Employment> for EmploymentFeeder {
    fn entity_id(&mut self) -> String {
        self.id.clone()
    }

    fn feed(&mut self, entity: Option<Employment>) {
        self.employment = entity;
    }
}

Required Methods§

Source

fn entity_id(&mut self) -> T::Key

Return the entity ID to fetch cache for.

Called first by expander to determine which cache entry to fetch.

Source

fn feed(&mut self, entity: Option<T>)

Feed the loaded entity into this feeder.

Called by expander after successfully loading from cache. The feeder stores the entity internally for later use.

Provided Methods§

Source

fn validate(&self) -> Result<()>

Optional: Validate the feeder before processing.

Called before attempting cache fetch. Use to validate state. Example: Check that entity_id is not empty

Source

fn on_loaded(&mut self, _entity: &T) -> Result<()>

Optional: Called after entity is loaded but before returning.

Useful for post-processing, logging, or metrics.

Source

fn on_miss(&mut self, _key: &str) -> Result<()>

Optional: Called when cache miss occurs.

Useful for metrics or custom behavior.

Source

fn on_hit(&mut self, _key: &str) -> Result<()>

Optional: Called when cache hit occurs.

Useful for metrics or logging.

Implementors§