Crates.io | airomem |
lib.rs | airomem |
version | 0.7.0 |
source | src |
created_at | 2024-03-04 04:39:25.597939 |
updated_at | 2024-10-09 12:30:07.865865 |
description | Simple persistence library inspired by Prevayler and @jarekratajski |
homepage | |
repository | https://github.com/makindotcc/airomem-rs |
max_upload_size | |
id | 1161185 |
size | 42,217 |
Release at crates.io
(Toy) persistence library for Rust inspired by prevayler for java and named after its wrapper airomem.
It is an implementation of the Prevalent System design pattern, in which business objects are kept live in memory and transactions are journaled for system recovery.
tokio::sync::RwLock
, reads are fast and concurrent safe.JournalFlushPolicy
for more info.
Recommended for data that may be lost (e.g. cache, http session storage).type UserId = usize;
type SessionsStore = JsonStore<Sessions, SessionsTx>;
#[derive(Serialize, Deserialize, Default)]
pub struct Sessions {
tokens: HashMap<String, UserId>,
operations: usize,
}
MergeTx!(pub SessionsTx<Sessions> = CreateSession | DeleteSession);
#[derive(Serialize, Deserialize)]
pub struct CreateSession {
token: String,
user_id: UserId,
}
impl Tx<Sessions> for CreateSession {
fn execute(self, data: &mut Sessions) {
data.operations += 1;
data.tokens.insert(self.token, self.user_id);
}
}
#[derive(Serialize, Deserialize)]
pub struct DeleteSession {
token: String,
}
impl Tx<Sessions, Option<UserId>> for DeleteSession {
fn execute(self, data: &mut Sessions) -> Option<UserId> {
data.operations += 1;
data.tokens.remove(&self.token)
}
}
#[tokio::test]
async fn test_mem_commit() {
let dir = tempdir().unwrap();
let mut store: SessionsStore =
Store::open(JsonSerializer, StoreOptions::default(), dir.into_path())
.await
.unwrap();
let example_token = "access_token".to_string();
let example_uid = 1;
store
.commit(CreateSession {
token: example_token.clone(),
user_id: example_uid,
})
.await
.unwrap();
let mut expected_tokens = HashMap::new();
expected_tokens.insert(example_token.clone(), example_uid);
assert_eq!(store.query().await.unwrap().tokens, expected_tokens);
let deleted_uid = store
.commit(DeleteSession {
token: example_token,
})
.await
.unwrap();
assert_eq!(deleted_uid, Some(example_uid));
}