Skip to content

Commit 08f1345

Browse files
committed
added compression
1 parent e9719ba commit 08f1345

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ tower-http = { version = "0.5.2", features = ["compression-full"] }
5151
juniper_graphql_ws = "0.4.0"
5252
pretty_env_logger = "0.5.0"
5353
log = { version = "0.4.21" }
54+
async-compression = { version = "0.4.9", features = ["gzip"] }
5455

5556
[dev-dependencies]
5657
tracing = "0.1.40"

src/cache/menu_cache.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
use chrono::{DateTime, Utc};
88
use firestore::FirestoreDb;
99
use futures::{stream::FuturesUnordered, StreamExt};
10+
use tokio::io::AsyncReadExt;
1011

1112
const CACHES_COLLECTION: &str = "caches";
1213

@@ -19,19 +20,23 @@ pub struct MenuCache<'a> {
1920
#[derive(serde::Serialize, serde::Deserialize, Default)]
2021
struct InDbMenuCache {
2122
cached_at: DateTime<Utc>,
22-
data: String,
23+
data: Vec<u8>,
2324
}
2425

25-
impl<'a> From<InDbMenuCache> for MenuCache<'a> {
26-
fn from(cache: InDbMenuCache) -> Self {
26+
impl<'a> MenuCache<'a> {
27+
async fn from_async(cache: InDbMenuCache) -> Self {
2728
if cache.data.is_empty() {
2829
return MenuCache {
2930
cached_at: cache.cached_at,
3031
locations: Locations::default(),
3132
};
3233
}
34+
let mut uncompressed =
35+
async_compression::tokio::bufread::GzipDecoder::new(cache.data.as_slice());
36+
let mut dst = String::with_capacity(cache.data.len() * 8);
37+
uncompressed.read_to_string(&mut dst).await;
3338
let locations: Locations =
34-
serde_json::from_str(&cache.data).expect("Data parse should always be valid");
39+
serde_json::from_str(&dst).expect("Data parse should always be valid");
3540
MenuCache {
3641
cached_at: cache.cached_at,
3742
locations,
@@ -42,9 +47,14 @@ impl<'a> From<InDbMenuCache> for MenuCache<'a> {
4247
impl<'a> TryFrom<MenuCache<'a>> for InDbMenuCache {
4348
type Error = crate::error::Error;
4449
fn try_from(cache: MenuCache<'a>) -> Result<Self, Error> {
50+
let mut json = serde_json::to_string(&cache.locations)?;
51+
let mut compressed = Vec::with_capacity(json.len() / 4);
52+
let mut compress =
53+
async_compression::tokio::bufread::GzipEncoder::new(std::io::Cursor::new(json));
54+
compress.read_buf(&mut compressed);
4555
Ok(Self {
4656
cached_at: cache.cached_at,
47-
data: serde_json::to_string(&cache.locations)?,
57+
data: compressed,
4858
})
4959
}
5060
}
@@ -82,18 +92,23 @@ impl<'a> MenuCache<'a> {
8292
.one("menu")
8393
.await?
8494
.unwrap_or_default(); // default is an empty cache
85-
Ok(cache.into())
95+
Ok(MenuCache::from_async(cache).await)
8696
}
8797

88-
fn to_db_representation(&self) -> InDbMenuCache {
98+
async fn to_db_representation(&self) -> InDbMenuCache {
99+
let json = serde_json::to_string(self.locations()).unwrap();
100+
let mut compressed = Vec::with_capacity(json.len() / 4);
101+
let mut compress =
102+
async_compression::tokio::bufread::GzipEncoder::new(std::io::Cursor::new(json));
103+
compress.read_buf(&mut compressed).await;
89104
InDbMenuCache {
90-
data: serde_json::to_string(&self.locations).unwrap(),
91105
cached_at: self.cached_at,
106+
data: compressed,
92107
}
93108
}
94109

95110
async fn save_to_db(&self) -> Result<(), firestore::errors::FirestoreError> {
96-
let cache: InDbMenuCache = self.to_db_representation();
111+
let cache: InDbMenuCache = self.to_db_representation().await;
97112
let db = FirestoreDb::new("ucsc-menu").await?;
98113
db.fluent()
99114
.update()

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![deny(unused_crate_dependencies)]
44
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
55

6-
use axum_server as _;
6+
use axum_server as _; // to use rustls over openssl bc alpine linux
77

88
mod cache;
99
mod error;

0 commit comments

Comments
 (0)