add: redis cache support

This commit is contained in:
trisua 2025-03-23 21:19:16 -04:00
parent 1d9a96ae69
commit 38dbf10130
13 changed files with 541 additions and 17 deletions

View file

@ -39,6 +39,31 @@ macro_rules! auto_method {
}
};
($name:ident()@$select_fn:ident -> $query:literal --name=$name_:literal --returns=$returns_:tt --cache-key-tmpl=$cache_key_tmpl:literal) => {
pub async fn $name(&self, id: usize) -> Result<$returns_> {
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_row!(&conn, $query, &[&id], |x| { Ok(Self::$select_fn(x)) });
if res.is_err() {
return Err(Error::GeneralNotFound($name_.to_string()));
}
let x = res.unwrap();
self.2
.set(
format!($cache_key_tmpl, id),
serde_json::to_string(&x).unwrap(),
)
.await;
Ok(x)
}
};
($name:ident($selector_t:ty)@$select_fn:ident -> $query:literal --name=$name_:literal --returns=$returns_:tt) => {
pub async fn $name(&self, selector: $selector_t) -> Result<$returns_> {
let conn = match self.connect().await {
@ -56,6 +81,31 @@ macro_rules! auto_method {
}
};
($name:ident($selector_t:ty)@$select_fn:ident -> $query:literal --name=$name_:literal --returns=$returns_:tt --cache-key-tmpl=$cache_key_tmpl:literal) => {
pub async fn $name(&self, selector: $selector_t) -> Result<$returns_> {
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_row!(&conn, $query, &[&selector], |x| { Ok(Self::$select_fn(x)) });
if res.is_err() {
return Err(Error::GeneralNotFound($name_.to_string()));
}
let x = res.unwrap();
self.2
.set(
format!($cache_key_tmpl, selector),
serde_json::to_string(&x).unwrap(),
)
.await;
Ok(x)
}
};
($name:ident()@$select_fn:ident:$permission:ident -> $query:literal) => {
pub async fn $name(&self, id: usize, user: User) -> Result<()> {
let page = self.$select_fn(id).await?;
@ -81,6 +131,33 @@ macro_rules! auto_method {
}
};
($name:ident()@$select_fn:ident:$permission:ident -> $query:literal --cache-key-tmpl=$cache_key_tmpl:literal) => {
pub async fn $name(&self, id: usize, user: User) -> Result<()> {
let page = self.$select_fn(id).await?;
if user.id != page.owner {
if !user.permissions.check(FinePermission::$permission) {
return Err(Error::NotAllowed);
}
}
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(&conn, $query, &[&id.to_string()]);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
self.2.remove(format!($cache_key_tmpl, id)).await;
Ok(())
}
};
($name:ident($x:ty)@$select_fn:ident:$permission:ident -> $query:literal) => {
pub async fn $name(&self, id: usize, user: User, x: $x) -> Result<()> {
let y = self.$select_fn(id).await?;
@ -106,6 +183,33 @@ macro_rules! auto_method {
}
};
($name:ident($x:ty)@$select_fn:ident:$permission:ident -> $query:literal --cache-key-tmpl=$cache_key_tmpl:literal) => {
pub async fn $name(&self, id: usize, user: User, x: $x) -> Result<()> {
let y = self.$select_fn(id).await?;
if user.id != y.owner {
if !user.permissions.check(FinePermission::$permission) {
return Err(Error::NotAllowed);
}
}
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(&conn, $query, &[&x, &id.to_string()]);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
self.2.remove(format!($cache_key_tmpl, id)).await;
Ok(())
}
};
($name:ident($x:ty)@$select_fn:ident:$permission:ident -> $query:literal --serde) => {
pub async fn $name(&self, id: usize, user: User, x: $x) -> Result<()> {
let y = self.$select_fn(id).await?;
@ -135,6 +239,37 @@ macro_rules! auto_method {
}
};
($name:ident($x:ty)@$select_fn:ident:$permission:ident -> $query:literal --serde --cache-key-tmpl=$cache_key_tmpl:literal) => {
pub async fn $name(&self, id: usize, user: User, x: $x) -> Result<()> {
let y = self.$select_fn(id).await?;
if user.id != y.owner {
if !user.permissions.check(FinePermission::$permission) {
return Err(Error::NotAllowed);
}
}
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(
&conn,
$query,
&[&serde_json::to_string(&x).unwrap(), &id.to_string()]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
self.2.remove(format!($cache_key_tmpl, id)).await;
Ok(())
}
};
($name:ident($x:ty) -> $query:literal) => {
pub async fn $name(&self, id: usize, x: $x) -> Result<()> {
let conn = match self.connect().await {
@ -152,6 +287,25 @@ macro_rules! auto_method {
}
};
($name:ident($x:ty) -> $query:literal --cache-key-tmpl=$cache_key_tmpl:literal) => {
pub async fn $name(&self, id: usize, x: $x) -> Result<()> {
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(&conn, $query, &[&x, &id.to_string()]);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
self.2.remove(format!($cache_key_tmpl, id)).await;
Ok(())
}
};
($name:ident($x:ty) -> $query:literal --serde) => {
pub async fn $name(&self, id: usize, x: $x) -> Result<()> {
let conn = match self.connect().await {
@ -172,4 +326,27 @@ macro_rules! auto_method {
Ok(())
}
};
($name:ident($x:ty) -> $query:literal --serde --cache-key-tmpl=$cache_key_tmpl:literal) => {
pub async fn $name(&self, id: usize, x: $x) -> Result<()> {
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(
&conn,
$query,
&[&serde_json::to_string(&x).unwrap(), &id.to_string()]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
self.2.remove(format!($cache_key_tmpl, id)).await;
Ok(())
}
};
}