mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-15 17:20:08 +00:00
add memory cache for gemini identities search
This commit is contained in:
parent
d9bf85884b
commit
f347c28f6f
@ -37,21 +37,18 @@ impl Identity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get `pem` record match `request`
|
/// Get `pem` record match `request` according to
|
||||||
///
|
/// [Gemini protocol specification](https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates)
|
||||||
/// https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates
|
/// * this function work with memory cache not database
|
||||||
pub fn gemini(&self, request: &str) -> Option<String> {
|
pub fn gemini(&self, request: &str) -> Option<String> {
|
||||||
|
// @TODO apply protocol rules to certificate selection
|
||||||
for profile_identity_gemini_id in self.gemini.auth.memory.starts_with(request) {
|
for profile_identity_gemini_id in self.gemini.auth.memory.starts_with(request) {
|
||||||
if let Ok(gemini_records) = self.gemini.database.records() {
|
if let Ok(pem) = self.gemini.memory.get(profile_identity_gemini_id) {
|
||||||
for gemini_record in gemini_records {
|
return Some(pem);
|
||||||
if gemini_record.id == profile_identity_gemini_id {
|
|
||||||
return Some(gemini_record.pem);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
} // @TODO apply protocol rules to selection
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tools
|
// Tools
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
mod auth;
|
mod auth;
|
||||||
mod database;
|
mod database;
|
||||||
|
mod memory;
|
||||||
|
|
||||||
use auth::Auth;
|
use auth::Auth;
|
||||||
use database::Database;
|
use database::Database;
|
||||||
|
use memory::Memory;
|
||||||
|
|
||||||
use sqlite::{Connection, Transaction};
|
use sqlite::{Connection, Transaction};
|
||||||
use std::{rc::Rc, sync::RwLock};
|
use std::{rc::Rc, sync::RwLock};
|
||||||
@ -12,7 +14,8 @@ use std::{rc::Rc, sync::RwLock};
|
|||||||
/// https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates
|
/// https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates
|
||||||
pub struct Gemini {
|
pub struct Gemini {
|
||||||
pub auth: Rc<Auth>,
|
pub auth: Rc<Auth>,
|
||||||
pub database: Rc<Database>,
|
// pub database: Rc<Database>,
|
||||||
|
pub memory: Rc<Memory>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Gemini {
|
impl Gemini {
|
||||||
@ -20,9 +23,27 @@ impl Gemini {
|
|||||||
|
|
||||||
/// Create new `Self`
|
/// Create new `Self`
|
||||||
pub fn new(connection: Rc<RwLock<Connection>>, profile_identity_id: Rc<i64>) -> Self {
|
pub fn new(connection: Rc<RwLock<Connection>>, profile_identity_id: Rc<i64>) -> Self {
|
||||||
|
// Init children components
|
||||||
|
let auth = Rc::new(Auth::new(connection.clone()));
|
||||||
|
let database = Rc::new(Database::new(connection, profile_identity_id));
|
||||||
|
let memory = Rc::new(Memory::new());
|
||||||
|
|
||||||
|
// Build initial index
|
||||||
|
match database.records() {
|
||||||
|
Ok(records) => {
|
||||||
|
for record in records {
|
||||||
|
if memory.add(record.id, record.pem).is_err() {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(reason) => todo!("{reason}"),
|
||||||
|
}
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
auth: Rc::new(Auth::new(connection.clone())),
|
auth,
|
||||||
database: Rc::new(Database::new(connection, profile_identity_id)),
|
// database,
|
||||||
|
memory,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
39
src/profile/identity/gemini/memory.rs
Normal file
39
src/profile/identity/gemini/memory.rs
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
mod error;
|
||||||
|
use error::Error;
|
||||||
|
|
||||||
|
use std::{cell::RefCell, collections::HashMap};
|
||||||
|
|
||||||
|
/// Reduce disk usage by cache index in memory
|
||||||
|
pub struct Memory {
|
||||||
|
index: RefCell<HashMap<i64, String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Memory {
|
||||||
|
// Constructors
|
||||||
|
|
||||||
|
/// Create new `Self`
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
index: RefCell::new(HashMap::new()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actions
|
||||||
|
|
||||||
|
/// Add new record with `id` as key and `pem` as value
|
||||||
|
/// * validate record with same key does not exist yet
|
||||||
|
pub fn add(&self, id: i64, pem: String) -> Result<(), Error> {
|
||||||
|
match self.index.borrow_mut().insert(id, pem) {
|
||||||
|
Some(_) => Err(Error::Overwrite), // @TODO prevent?
|
||||||
|
None => Ok(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get `pem` clone by `id` from memory index
|
||||||
|
pub fn get(&self, id: i64) -> Result<String, Error> {
|
||||||
|
match self.index.borrow().get(&id) {
|
||||||
|
Some(pem) => Ok(pem.clone()),
|
||||||
|
None => Err(Error::NotFound),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
5
src/profile/identity/gemini/memory/error.rs
Normal file
5
src/profile/identity/gemini/memory/error.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
NotFound,
|
||||||
|
Overwrite,
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user