mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 09:05:27 +00:00
add memory cache for gemini identities search
This commit is contained in:
parent
d9bf85884b
commit
f347c28f6f
4 changed files with 75 additions and 13 deletions
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…
Add table
Add a link
Reference in a new issue