This file contains hidden or 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
| #[derive(Debug, PartialEq, Clone)] | |
| pub struct Entry<T> { | |
| val: T, | |
| count: usize, | |
| } | |
| impl<T> Entry<T> { | |
| fn inc(&mut self) { | |
| self.count += 1; | |
| } |
This file contains hidden or 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
| pub fn set(&mut self, key: &'a str, value: T) where T: Debug { | |
| if let Some(entry) = self.by_key.get_mut(&key) { | |
| entry.val = value; | |
| self.update_freq_node(key); | |
| return | |
| } | |
| if self.len() >= self.capacity { | |
| // evict func | |
| self.evict(); |
This file contains hidden or 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
| fn update_freq_node(&mut self, key: &'a str) { | |
| let entry = self.by_key.get_mut(key).unwrap(); | |
| // get the freq node | |
| let freq_node = self.freq_node.get_mut(&entry.count).unwrap(); | |
| // once it have been access | |
| // we need to remove the key in this current node | |
| freq_node.remove(key); |
This file contains hidden or 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
| // return a ref of the most-recently-used entry | |
| pub fn get(&mut self, key: &'a str) -> Option<&T> where T: Debug{ | |
| self.update_freq_node(key); | |
| self.by_key.get(key).map(|e| &e.val) | |
| } | |
| // evict the least freq used | |
| fn evict(&mut self) { | |
| let least_freq_node = self.freq_node.get_mut(&self.least_freq_node).unwrap(); | |
| let least_freq_entry= least_freq_node.pop_front().unwrap(); |
This file contains hidden or 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
| // current cache length | |
| pub fn len(&self) -> usize { | |
| self.by_key.len() | |
| } | |
| // check if current cache is empty | |
| pub fn is_empty(&self) -> bool { | |
| self.by_key.is_empty() | |
| } |
This file contains hidden or 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
| let mut tu = TinyUrl::new(); | |
| let url = "http://hotmail.com"; | |
| let key = tu.set(url); | |
| let url_2 = "http://google.com"; | |
| let t key_2 = tu.set(url); | |
| let url_3 = "http://linkedin.com"; | |
| let key_3 = tu.set(url); |
This file contains hidden or 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
| pub struct TokenBucket { | |
| rate: i64, | |
| max_tokens: i64, | |
| current_tokens: i64, | |
| last_refill_timestamp: DateTime<Utc>, | |
| } |
This file contains hidden or 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
| /// TokeBucket implementation | |
| impl TokenBucket { | |
| pub fn new(rate: i64, max_token: i64) -> Self { | |
| TokenBucket { | |
| rate: rate, | |
| max_tokens: max_token, | |
| current_tokens: max_token, | |
| last_refill_timestamp: Utc::now(), | |
| } | |
| } |
This file contains hidden or 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
| fn update(&mut self) { | |
| let current = Utc::now(); | |
| let diff = current.time() - self.last_refill_timestamp.time(); | |
| let tokens_added = diff.num_seconds() * self.rate / 1000000000; | |
| self.current_tokens = cmp::min(self.current_tokens + tokens_added, self.max_tokens); | |
| self.last_refill_timestamp = current; | |
| } |
This file contains hidden or 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
| pub trait Actions { | |
| fn forward(&self, people: i64); | |
| fn queue(&self, people: i64); | |
| } | |
| impl Actions for TokenBucket { | |
| fn forward(&self, people: i64) { | |
| println!("-> forward : {:?} people", people); | |
| } | |
| fn queue(&self, people: i64){ |