unistore-cache 0.1.0

In-memory cache capability for UniStore
Documentation
//! 【错误类型】- 缓存相关错误定义
//!
//! 职责:
//! - 定义缓存操作的错误类型

use thiserror::Error;

/// 缓存错误
#[derive(Debug, Error)]
pub enum CacheError {
    /// 键不存在
    #[error("键不存在: {key}")]
    KeyNotFound { key: String },

    /// 值已过期
    #[error("值已过期")]
    Expired,

    /// 缓存已满且无法淘汰
    #[error("缓存已满")]
    CacheFull,

    /// 无效的配置
    #[error("无效的配置: {0}")]
    InvalidConfig(String),
}

impl CacheError {
    /// 检查是否是键不存在错误
    pub fn is_not_found(&self) -> bool {
        matches!(self, Self::KeyNotFound { .. })
    }

    /// 检查是否是过期错误
    pub fn is_expired(&self) -> bool {
        matches!(self, Self::Expired)
    }
}