Skip to main content

LLMCache

Struct LLMCache 

Source
pub struct LLMCache { /* private fields */ }
Expand description

LLM 调用缓存

缓存 LLM 调用的输入输出,避免相同请求重复调用 API。

§示例

use langchainrust::core::cache::LLMCache;

let cache = LLMCache::new();
cache.put("key", llm_result).await;

if let Some(cached) = cache.get("key").await {
    println!("缓存命中: {}", cached.result.content);
}

Implementations§

Source§

impl LLMCache

Source

pub fn new() -> Self

Source

pub fn with_config(config: CacheConfig) -> Self

Source

pub fn build_key(messages: &[Message], model: &str) -> Result<String, String>

从消息列表生成缓存键

将消息列表序列化为 JSON 字符串作为键。 包含 model 名称以确保不同模型的调用不互相影响。 如果序列化失败,返回错误而非回退到空字符串(M34)。

Source

pub async fn get(&self, key: &str) -> Option<CachedLLMResult>

获取缓存结果

如果发现过期条目,会立即删除(H36)。

Source

pub async fn put(&self, key: String, result: LLMResult)

存入缓存结果

Source

pub async fn clear(&self)

清除缓存

Source

pub async fn len(&self) -> usize

获取缓存大小

Source

pub async fn is_empty(&self) -> bool

缓存是否为空

Source

pub async fn evict_expired(&self) -> usize

移除过期条目

Trait Implementations§

Source§

impl Default for LLMCache

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more