Skip to content

Commit f3d12be

Browse files
committed
refactor: adjust caching
1 parent b7feea9 commit f3d12be

File tree

7 files changed

+29
-28
lines changed

7 files changed

+29
-28
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ aliri_traits = "0.1.1"
1616
async-trait = "0.1.80"
1717
bincode = { version = "1.3.3", optional = true }
1818
bb8 = { version = "0.8.5", optional = true }
19-
cached = { version = "0.52.0", features = ["async", "proc_macro"], optional = true }
19+
cached = { version = "0.52.0", features = ["async", "proc_macro"] }
2020
chrono = { version = "0.4.38", features = ["serde"] }
2121
derivative = "2.2.0"
2222
envy = "0.4.2"
@@ -73,9 +73,9 @@ reqwest = { version = "0.12.3", features = ["json"] }
7373
test-log = "0.2.15"
7474

7575
[features]
76-
default = ["all-databases", "otlp", "caching-skytable", "bincode"]
76+
default = ["all-databases", "otlp", "caching-skytable"]
7777

78-
caching-skytable = ["skytable", "cached", "bb8"]
78+
caching-skytable = ["skytable", "bb8", "bincode"]
7979
otlp = [
8080
"opentelemetry",
8181
"opentelemetry-otlp",

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ integration_test:
6767
cleanup:
6868
docker rm -f database;docker rm -f oidc-server-mock;docker rm -f feedback-fusion;docker rm -f skytable;docker network rm feedback-fusion; echo ""
6969

70-
bench: cleanup docker_network skytable oidc-server-mock postgres_database postgres_backend
70+
bench: cleanup docker_network oidc-server-mock postgres_database postgres_backend
7171
GRPC_ENDPOINT=http://localhost:8000 OIDC_CLIENT_ID=client OIDC_CLIENT_SECRET=secret OIDC_PROVIDER=http://localhost:5151 cargo bench
7272
${MAKE} cleanup
7373

src/cache.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,39 @@ use crate::{
2424
database::schema::feedback::{Field, Prompt},
2525
prelude::*,
2626
};
27+
#[cfg(feature = "caching-skytable")]
2728
use bb8::ManageConnection;
29+
#[cfg(feature = "caching-skytable")]
2830
use cached::IOCachedAsync;
31+
#[cfg(feature = "caching-skytable")]
2932
use chrono::Utc;
3033
use feedback_fusion_codegen::dynamic_cache;
34+
35+
#[cfg(feature = "caching-skytable")]
3136
use serde::{de::DeserializeOwned, Serialize};
37+
#[cfg(feature = "caching-skytable")]
3238
use skytable::{
3339
aio::TcpConnection,
3440
pool::{ConnectionMgrTcp, ConnectionMgrTls},
3541
query, ClientResult, Pipeline, Query, Response,
3642
};
43+
44+
#[cfg(feature = "caching-skytable")]
3745
use std::{
3846
fmt::{Debug, Display},
3947
marker::PhantomData,
4048
ops::DerefMut,
4149
time::Duration,
4250
};
51+
#[cfg(feature = "caching-skytable")]
4352
use thiserror::Error;
53+
#[cfg(feature = "caching-skytable")]
4454
use tokio::io::{AsyncRead, AsyncWrite};
55+
#[cfg(feature = "caching-skytable")]
4556
use tracing::{instrument, Instrument};
4657

4758
// may publish this as crate or submit as pr for cached
59+
#[cfg(feature = "caching-skytable")]
4860
pub struct SkytableCacheBuilder<'a, K, V> {
4961
username: &'a str,
5062
password: &'a str,
@@ -57,6 +69,7 @@ pub struct SkytableCacheBuilder<'a, K, V> {
5769
_phantom: PhantomData<(K, V)>,
5870
}
5971

72+
#[cfg(feature = "caching-skytable")]
6073
impl<'a, K, V> SkytableCacheBuilder<'a, K, V>
6174
where
6275
K: Display,
@@ -122,6 +135,7 @@ where
122135
}
123136
}
124137

138+
#[cfg(feature = "caching-skytable")]
125139
pub struct SkytableTlsCacheBuilder<'a, K, V> {
126140
username: &'a str,
127141
password: &'a str,
@@ -135,6 +149,7 @@ pub struct SkytableTlsCacheBuilder<'a, K, V> {
135149
_phantom: PhantomData<(K, V)>,
136150
}
137151

152+
#[cfg(feature = "caching-skytable")]
138153
impl<'a, K, V> SkytableTlsCacheBuilder<'a, K, V>
139154
where
140155
K: Display,
@@ -208,6 +223,7 @@ where
208223
}
209224
}
210225

226+
#[cfg(feature = "caching-skytable")]
211227
#[derive(Error, Debug)]
212228
pub enum SkytableCacheError {
213229
#[error(transparent)]
@@ -218,6 +234,7 @@ pub enum SkytableCacheError {
218234
PoolError(String),
219235
}
220236

237+
#[cfg(feature = "caching-skytable")]
221238
impl<E> From<bb8::RunError<E>> for SkytableCacheError
222239
where
223240
E: Debug,
@@ -227,18 +244,22 @@ where
227244
}
228245
}
229246

247+
#[cfg(feature = "caching-skytable")]
230248
#[derive(Clone, Query, Response)]
231249
struct CachedSkytableValue {
232250
pub ckey: String,
233251
pub cvalue: Vec<u8>,
234252
pub ttl: i64,
235253
}
254+
255+
#[cfg(feature = "caching-skytable")]
236256
impl CachedSkytableValue {
237257
fn new(ckey: String, cvalue: Vec<u8>, ttl: i64) -> Self {
238258
Self { ckey, cvalue, ttl }
239259
}
240260
}
241261

262+
#[cfg(feature = "caching-skytable")]
242263
pub struct SkytableCache<'a, C: ManageConnection, K, V> {
243264
space: &'a str,
244265
model: &'a str,
@@ -248,6 +269,7 @@ pub struct SkytableCache<'a, C: ManageConnection, K, V> {
248269
_phantom: PhantomData<(K, V)>,
249270
}
250271

272+
#[cfg(feature = "caching-skytable")]
251273
impl<'a, C, I, S, K, V> SkytableCache<'a, C, K, V>
252274
where
253275
S: AsyncRead + AsyncWrite + Send + Sync + Unpin,
@@ -274,6 +296,7 @@ where
274296
}
275297
}
276298

299+
#[cfg(feature = "caching-skytable")]
277300
#[async_trait::async_trait]
278301
impl<'a, C, I, S, K, V> IOCachedAsync<K, V> for SkytableCache<'a, C, K, V>
279302
where

src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ use tonic_web::GrpcWebLayer;
4242
use tower_http::trace::TraceLayer;
4343
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
4444

45-
#[cfg(feature = "caching-skytable")]
4645
pub mod cache;
4746
pub mod config;
4847
pub mod database;

src/services/v1/field.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
use super::{FeedbackFusionV1Context, PublicFeedbackFusionV1Context};
2424
use crate::{
25-
database::schema::feedback::{Field, FieldOptions, FieldType},
26-
prelude::*,
25+
cache::fetch_prompt, database::schema::feedback::{Field, FieldOptions, FieldType}, prelude::*
2726
};
2827
use feedback_fusion_common::proto::{
2928
CreateFieldRequest, DeleteFieldRequest, Field as ProtoField, FieldPage, GetFieldsRequest,
@@ -62,12 +61,6 @@ pub async fn get_active_fields(
6261
let connection = context.connection();
6362

6463
// fetch the prompt
65-
#[cfg(not(feature = "caching-skytable"))]
66-
let prompt = database_request!(
67-
Prompt::select_by_id(connection, data.prompt.as_str()).await,
68-
"Fetch prompt by id"
69-
)?;
70-
#[cfg(feature = "caching-skytable")]
7164
let prompt = fetch_prompt(connection, data.prompt.as_str()).await?;
7265

7366
let prompt = prompt.ok_or(FeedbackFusionError::BadRequest(

src/services/v1/prompt.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ pub async fn create_prompt(
4848
Ok(Response::new(prompt.into()))
4949
}
5050

51-
5251
#[instrument(skip_all)]
5352
pub async fn get_prompt(
5453
context: &PublicFeedbackFusionV1Context,
@@ -57,12 +56,6 @@ pub async fn get_prompt(
5756
let data = request.into_inner();
5857
let connection = context.connection();
5958

60-
#[cfg(not(feature = "caching-skytable"))]
61-
let prompt: Option<Prompt> = database_request!(
62-
Prompt::select_by_id(connection, data.id.as_str()).await,
63-
"Select prompt by id"
64-
)?;
65-
#[cfg(feature = "caching-skytable")]
6659
let prompt = fetch_prompt(connection, data.id.as_str()).await?;
6760

6861
match prompt {

src/services/v1/response.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
use super::{FeedbackFusionV1Context, PublicFeedbackFusionV1Context};
2424
use crate::{
25-
database::schema::feedback::{FieldData, FieldResponse, PromptResponse},
26-
prelude::*,
25+
cache::fields_by_prompt, database::schema::feedback::{FieldData, FieldResponse, PromptResponse}, prelude::*
2726
};
2827
use feedback_fusion_common::proto::{
2928
CreateResponsesRequest, FieldResponse as ProtoFieldResponse, FieldResponseList,
@@ -47,13 +46,7 @@ pub async fn create_responses(
4746
});
4847

4948
// fetch the fields of the prompt
50-
#[cfg(feature = "caching-skytable")]
5149
let fields = fields_by_prompt(context.connection(), data.prompt.as_str()).await?;
52-
#[cfg(not(feature = "caching-skytable"))]
53-
let fields = database_request!(
54-
Field::select_by_column(&transaction, "prompt", data.prompt.as_str()).await,
55-
"Select fields by prompt"
56-
)?;
5750
// as we can assume a prompt has to have at least 1 field we can throw the 400 here
5851
if fields.is_empty() {
5952
return Err(FeedbackFusionError::BadRequest("invalid prompt".to_owned()));

0 commit comments

Comments
 (0)