Skip to content

Commit

Permalink
selecting custom caching strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Mindaugas Vinkelis committed Aug 10, 2024
1 parent bce4881 commit 97afa88
Show file tree
Hide file tree
Showing 9 changed files with 462 additions and 118 deletions.
6 changes: 0 additions & 6 deletions diesel/src/connection/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
//! Types related to database connections
pub(crate) mod instrumentation;
#[cfg(all(
not(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"),
any(feature = "sqlite", feature = "postgres", feature = "mysql")
))]
pub(crate) mod statement_cache;
#[cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes")]
pub mod statement_cache;
mod transaction_manager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
//! statements is [`SimpleConnection::batch_execute`](super::SimpleConnection::batch_execute).
//!
//! In order to avoid the cost of re-parsing and planning subsequent queries,
//! Diesel caches the prepared statement whenever possible. Queries will fall
//! into one of three buckets:
//! by default Diesel caches the prepared statement whenever possible, but
//! this an be customized by end user calling
//! [`ConnectionStatementCache::statement_caching`].
//! Queries will fall into one of three buckets:
//!
//! - Unsafe to cache
//! - Cached by SQL
Expand Down Expand Up @@ -94,25 +96,32 @@
use std::any::TypeId;
use std::borrow::Cow;
use std::collections::HashMap;
use std::hash::Hash;
use std::ops::{Deref, DerefMut};

use strategy::{ConnectionStatementCache, StatementCacheStrategy};

use crate::backend::Backend;
use crate::connection::InstrumentationEvent;
use crate::query_builder::*;
use crate::result::QueryResult;

use super::Instrumentation;

/// Various interfaces and implementations to control connection statement caching.
pub mod strategy;

/// A prepared statement cache
#[allow(missing_debug_implementations, unreachable_pub)]
#[cfg_attr(
docsrs,
doc(cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"))
)]
pub struct StatementCache<DB: Backend, Statement> {
pub(crate) cache: HashMap<StatementCacheKey<DB>, Statement>,
cache: Box<dyn StatementCacheStrategy<DB, Statement>>,
// increment every time a query is cached
// some backends might use it to create unique prepared statement names
cache_counter: u64,
}

/// A helper type that indicates if a certain query
Expand All @@ -134,41 +143,28 @@ pub enum PrepareForCache {
No,
}

#[allow(
clippy::len_without_is_empty,
clippy::new_without_default,
unreachable_pub
)]
#[allow(clippy::new_without_default, unreachable_pub)]
impl<DB, Statement> StatementCache<DB, Statement>
where
DB: Backend,
DB: Backend + 'static,
Statement: 'static,
DB::TypeMetadata: Clone,
DB::QueryBuilder: Default,
StatementCacheKey<DB>: Hash + Eq,
{
/// Create a new prepared statement cache
/// In order to do so, [`GlobalConnectionStatementCacheStrategy`] must be implemented for a connection.
#[allow(unreachable_pub)]
pub fn new() -> Self {
pub fn new<Conn>() -> Self
where
Conn: ConnectionStatementCache<Backend = DB, Statement = Statement>,
{
StatementCache {
cache: HashMap::new(),
cache: Conn::statement_caching().create_cache(),
cache_counter: 0,
}
}

/// Get the current length of the statement cache
#[allow(unreachable_pub)]
#[cfg(any(
feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes",
feature = "postgres",
all(feature = "sqlite", test)
))]
#[cfg_attr(
docsrs,
doc(cfg(feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"))
)]
pub fn len(&self) -> usize {
self.cache.len()
}

/// Prepare a query as prepared statement
///
/// This functions returns a prepared statement corresponding to the
Expand All @@ -191,52 +187,41 @@ where
) -> QueryResult<MaybeCached<'_, Statement>>
where
T: QueryFragment<DB> + QueryId,
F: FnMut(&str, PrepareForCache) -> QueryResult<Statement>,
F: FnMut(&str, Option<u64>) -> QueryResult<Statement>,
{
self.cached_statement_non_generic(
Self::cached_statement_non_generic(
self.cache.as_mut(),
T::query_id(),
source,
backend,
bind_types,
&mut prepare_fn,
instrumentation,
&mut |sql, is_cached| {
if let PrepareForCache::Yes = is_cached {
instrumentation.on_connection_event(InstrumentationEvent::CacheQuery { sql });
self.cache_counter += 1;
prepare_fn(sql, Some(self.cache_counter))
} else {
prepare_fn(sql, None)
}
},
)
}

/// Reduce the amount of monomorphized code by factoring this via dynamic dispatch
fn cached_statement_non_generic(
&mut self,
fn cached_statement_non_generic<'a>(
cache: &'a mut dyn StatementCacheStrategy<DB, Statement>,
maybe_type_id: Option<TypeId>,
source: &dyn QueryFragmentForCachedStatement<DB>,
backend: &DB,
bind_types: &[DB::TypeMetadata],
prepare_fn: &mut dyn FnMut(&str, PrepareForCache) -> QueryResult<Statement>,
instrumentation: &mut dyn Instrumentation,
) -> QueryResult<MaybeCached<'_, Statement>> {
use std::collections::hash_map::Entry::{Occupied, Vacant};

) -> QueryResult<MaybeCached<'a, Statement>> {
let cache_key = StatementCacheKey::for_source(maybe_type_id, source, bind_types, backend)?;

if !source.is_safe_to_cache_prepared(backend)? {
let sql = cache_key.sql(source, backend)?;
return prepare_fn(&sql, PrepareForCache::No).map(MaybeCached::CannotCache);
}

let cached_result = match self.cache.entry(cache_key) {
Occupied(entry) => entry.into_mut(),
Vacant(entry) => {
let statement = {
let sql = entry.key().sql(source, backend)?;
instrumentation
.on_connection_event(InstrumentationEvent::CacheQuery { sql: &sql });
prepare_fn(&sql, PrepareForCache::Yes)
};

entry.insert(statement?)
}
};

Ok(MaybeCached::Cached(cached_result))
cache.get(cache_key, backend, source, prepare_fn)
}
}

Expand Down
Loading

0 comments on commit 97afa88

Please sign in to comment.