pub struct PgCache { /* private fields */ }Expand description
PostgreSQL-backed implementation of the LlmCache trait.
Stores serialized ChatResponse values in a PostgreSQL table with optional
TTL expiration. Call initialize once after construction
to create the backing table (idempotent).
§Example
use sqlx::postgres::PgPoolOptions;
use synaptic_postgres::{PgCache, PgCacheConfig};
let pool = PgPoolOptions::new()
.max_connections(5)
.connect("postgres://user:pass@localhost/mydb")
.await?;
let config = PgCacheConfig::new("llm_cache").with_ttl(3600);
let cache = PgCache::new(pool, config);
cache.initialize().await?;Implementations§
Source§impl PgCache
impl PgCache
Sourcepub fn new(pool: PgPool, config: PgCacheConfig) -> Self
pub fn new(pool: PgPool, config: PgCacheConfig) -> Self
Create a new PgCache from an existing connection pool and config.
Sourcepub async fn initialize(&self) -> Result<(), SynapticError>
pub async fn initialize(&self) -> Result<(), SynapticError>
Ensure the backing table exists.
This is idempotent and safe to call on every application startup.
Sourcepub fn config(&self) -> &PgCacheConfig
pub fn config(&self) -> &PgCacheConfig
Return a reference to the configuration.
Trait Implementations§
Source§impl LlmCache for PgCache
impl LlmCache for PgCache
Source§fn get<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<ChatResponse>, SynapticError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn get<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<ChatResponse>, SynapticError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Look up a cached response by cache key.
Source§fn put<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
key: &'life1 str,
response: &'life2 ChatResponse,
) -> Pin<Box<dyn Future<Output = Result<(), SynapticError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn put<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
key: &'life1 str,
response: &'life2 ChatResponse,
) -> Pin<Box<dyn Future<Output = Result<(), SynapticError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Store a response in the cache.
Auto Trait Implementations§
impl Freeze for PgCache
impl !RefUnwindSafe for PgCache
impl Send for PgCache
impl Sync for PgCache
impl Unpin for PgCache
impl UnsafeUnpin for PgCache
impl !UnwindSafe for PgCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more