-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to spawn new thread per connection
- Loading branch information
1 parent
a641826
commit 5e488e5
Showing
3 changed files
with
35 additions
and
31 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
/target | ||
.vscode |
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
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,38 +1,37 @@ | ||
use std::collections::HashMap; | ||
use std::sync::{Arc, RwLock}; | ||
use std::collections::HashMap; | ||
use std::sync::{Arc, RwLock}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Cache { | ||
data: Arc<RwLock<HashMap<String, String>>>, | ||
} | ||
#[derive(Debug, Clone)] | ||
pub struct Cache { | ||
data: Arc<RwLock<HashMap<String, String>>>, | ||
} | ||
|
||
impl Cache { | ||
// Initialize a new database | ||
pub fn new() -> Self { | ||
Self { | ||
data: Arc::new(RwLock::new(HashMap::new())), | ||
impl Cache { | ||
pub fn new() -> Self { | ||
Self { | ||
data: Arc::new(RwLock::new(HashMap::new())), | ||
} | ||
} | ||
} | ||
|
||
pub fn set(&self, key: String, value: String) { | ||
let mut data = self.data.write().unwrap(); | ||
data.insert(key, value); | ||
} | ||
pub fn set(&self, key: String, value: String) { | ||
let mut data = self.data.write().unwrap(); | ||
data.insert(key, value); | ||
} | ||
|
||
pub fn get(&self, key: &str) -> Option<String> { | ||
let data = self.data.read().unwrap(); | ||
data.get(key).cloned() | ||
pub fn get(&self, key: &str) -> Option<String> { | ||
let data = self.data.read().unwrap(); | ||
data.get(key).cloned() | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn should_initialize_and_get_set() { | ||
let cache = Cache::new(); | ||
cache.set(String::from("name"), String::from("The Wizard of Oz")); | ||
assert_eq!("The Wizard of Oz", cache.get("name").unwrap()); | ||
#[test] | ||
fn should_initialize_and_get_set() { | ||
let cache = Cache::new(); | ||
cache.set(String::from("name"), String::from("The Wizard of Oz")); | ||
assert_eq!("The Wizard of Oz", cache.get("name").unwrap()); | ||
} | ||
} | ||
} |