feat: caching is really powerful
This commit is contained in:
parent
35896efa04
commit
b714571b85
1 changed files with 77 additions and 14 deletions
|
@ -1,6 +1,7 @@
|
|||
use ldap3::{LdapConn, Scope, SearchEntry};
|
||||
use skynet_ldap_backend::{db_init, get_config, AccountWolves, Accounts, Config};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use std::collections::HashMap;
|
||||
use wolves_oxidised::WolvesUser;
|
||||
|
||||
#[async_std::main]
|
||||
|
@ -8,9 +9,13 @@ async fn main() -> tide::Result<()> {
|
|||
let config = get_config();
|
||||
let db = db_init(&config).await.unwrap();
|
||||
|
||||
dbg!("&config");
|
||||
update_wolves(&config, &db).await;
|
||||
dbg!("complete - update_wolves");
|
||||
update_wolves_id(&db).await;
|
||||
dbg!("complete - update_wolves_id");
|
||||
update_ldap(&config, &db).await;
|
||||
dbg!("complete - update_ldap");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -46,6 +51,31 @@ async fn update_wolves_id(db: &Pool<Sqlite>) {
|
|||
}
|
||||
}
|
||||
|
||||
async fn get_account_hashmap(db: &Pool<Sqlite>) -> HashMap<String, Accounts> {
|
||||
// get all users, toss in hashmap then query for differences?
|
||||
let mut users_existing = HashMap::new();
|
||||
match sqlx::query_as::<_, Accounts>(
|
||||
r#"
|
||||
SELECT *
|
||||
FROM accounts
|
||||
"#,
|
||||
)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
{
|
||||
Ok(res) => {
|
||||
for account in &res {
|
||||
users_existing.insert(account.user.to_owned(), account.to_owned());
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
dbg!(e);
|
||||
}
|
||||
}
|
||||
|
||||
users_existing
|
||||
}
|
||||
|
||||
async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
|
||||
let mut ldap = match LdapConn::new(&config.ldap_host) {
|
||||
Ok(s) => s,
|
||||
|
@ -63,6 +93,8 @@ async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
|
|||
}
|
||||
}
|
||||
|
||||
let existing_accounts = get_account_hashmap(db).await;
|
||||
|
||||
// use this to pre load a large chunk of data
|
||||
if let Ok(x) = ldap.search(
|
||||
"ou=users,dc=skynet,dc=ie",
|
||||
|
@ -101,6 +133,44 @@ async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
|
|||
}
|
||||
|
||||
if !tmp_account.user.is_empty() {
|
||||
match existing_accounts.get(&tmp_account.user) {
|
||||
None => {
|
||||
update_accounts(db, &tmp_account).await;
|
||||
}
|
||||
Some(old) => {
|
||||
let mut differnt = false;
|
||||
|
||||
if old.mail != tmp_account.mail {
|
||||
differnt = true;
|
||||
}
|
||||
if old.secure != tmp_account.secure {
|
||||
differnt = true;
|
||||
}
|
||||
if old.uid != tmp_account.uid {
|
||||
differnt = true;
|
||||
}
|
||||
if old.student_id != tmp_account.student_id {
|
||||
differnt = true;
|
||||
}
|
||||
if old.id_wolves != tmp_account.id_wolves {
|
||||
differnt = true;
|
||||
}
|
||||
|
||||
if differnt {
|
||||
update_accounts(db, &tmp_account).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// done with ldap
|
||||
ldap.unbind().unwrap();
|
||||
}
|
||||
|
||||
async fn update_accounts(db: &Pool<Sqlite>, tmp_account: &Accounts) {
|
||||
sqlx::query_as::<_, Accounts>(
|
||||
"
|
||||
INSERT OR REPLACE INTO accounts (user, uid, mail, student_id, secure)
|
||||
|
@ -115,13 +185,6 @@ async fn update_ldap(config: &Config, db: &Pool<Sqlite>) {
|
|||
.fetch_optional(db)
|
||||
.await
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// done with ldap
|
||||
ldap.unbind().unwrap();
|
||||
}
|
||||
|
||||
async fn update_account(db: &Pool<Sqlite>, account: &WolvesUser) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue