forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: [torrust#1195] extract InMemoryKeyRepository
- Loading branch information
1 parent
f4c7b97
commit 12a62ce
Showing
3 changed files
with
52 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use crate::core::authentication::key::{Key, PeerKey}; | ||
|
||
/// In-memory implementation of the authentication key repository. | ||
#[derive(Debug, Default)] | ||
pub struct InMemoryKeyRepository { | ||
/// Tracker users' keys. Only for private trackers. | ||
keys: tokio::sync::RwLock<std::collections::HashMap<Key, PeerKey>>, | ||
} | ||
|
||
impl InMemoryKeyRepository { | ||
/// It adds a new authentication key. | ||
pub async fn insert(&self, auth_key: &PeerKey) { | ||
self.keys.write().await.insert(auth_key.key.clone(), auth_key.clone()); | ||
} | ||
|
||
/// It removes an authentication key. | ||
pub async fn remove(&self, key: &Key) { | ||
self.keys.write().await.remove(key); | ||
} | ||
|
||
pub async fn get(&self, key: &Key) -> Option<PeerKey> { | ||
self.keys.read().await.get(key).cloned() | ||
} | ||
|
||
/// It clears all the authentication keys. | ||
pub async fn clear(&self) { | ||
let mut keys = self.keys.write().await; | ||
keys.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod in_memory; | ||
pub mod persisted; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters