Skip to content

Commit

Permalink
feat: cache for qrcode
Browse files Browse the repository at this point in the history
  • Loading branch information
AH-dark committed Mar 17, 2024
1 parent 7fceeb1 commit f7783f9
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 13 deletions.
164 changes: 164 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust-components/network-functions-handler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ teloxide = { workspace = true, features = ["macros"] }
serde = { workspace = true, features = ["derive"] }
anyhow = "1.0"
fast_qr = { version = "0.12", features = ["image"] }
moka = { version = "0.12", features = ["future"] }
31 changes: 28 additions & 3 deletions rust-components/network-functions-handler/src/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use fast_qr::convert::Builder;
use moka::future::Cache;
use teloxide::prelude::*;
use teloxide::types::InputFile;
use teloxide::utils::command::BotCommands;
Expand All @@ -19,13 +20,33 @@ macro_rules! send_error_message {
};
}

pub(crate) async fn qrcode_handler(bot: &Bot, message: &Message, text: &str) -> anyhow::Result<()> {
pub(crate) async fn qrcode_handler(
bot: Bot,
message: Message,
text: String,
cache: Cache<String, Vec<u8>>,
) -> anyhow::Result<()> {
if text.is_empty() {
send_error_message!(bot, message, "Text is empty");
return Err(anyhow::anyhow!("Text is empty"));
}

let qr_code = match fast_qr::QRBuilder::new(text).build() {
// Check if QRCode already exists in cache
if let Some(data) = cache.get(&format!("qrcode:{}", &text).to_string()).await {
log::debug!("QRCode cache hit");

bot.send_photo(
message.chat.id,
InputFile::memory(data.to_vec()).file_name("qrcode.png"),
)
.reply_to_message_id(message.id)
.send()
.await?;

return Ok(());
}

let qr_code = match fast_qr::QRBuilder::new(text.clone()).build() {
Ok(d) => d,
Err(err) => {
send_error_message!(bot, message, format!("Failed to generate QRCode: {}", err));
Expand All @@ -41,11 +62,15 @@ pub(crate) async fn qrcode_handler(bot: &Bot, message: &Message, text: &str) ->

bot.send_photo(
message.chat.id,
InputFile::memory(image).file_name("qrcode.png"),
InputFile::memory(image.clone()).file_name("qrcode.png"),
)
.reply_to_message_id(message.id)
.send()
.await?;

cache
.insert(format!("qrcode:{}", text).to_string(), image.to_vec())
.await;

Ok(())
}
15 changes: 5 additions & 10 deletions rust-components/network-functions-handler/src/run.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use moka::future::Cache;
use teloxide::prelude::*;

use pegasus_common::bot::channel::MqUpdateListener;
Expand All @@ -8,19 +9,13 @@ pub(crate) async fn run(bot: Bot, listener: MqUpdateListener) {
let handler = dptree::entry().branch(
Update::filter_message()
.filter_command::<Command>()
.endpoint(|bot: Bot, cmd: Command, msg: Message| async move {
match cmd {
Command::QRCode(text) => qrcode_handler(&bot, &msg, text.as_str())
.await
.map_err(|e| {
log::error!("Error handling qrcode command: {}", e);
e
}),
}
}),
.branch(dptree::case![Command::QRCode(string)].endpoint(qrcode_handler)),
);

let cache: Cache<String, Vec<u8>> = Cache::new(1000);

Dispatcher::builder(bot, handler)
.dependencies(dptree::deps![cache])
.build()
.dispatch_with_listener(
listener,
Expand Down

0 comments on commit f7783f9

Please sign in to comment.