This repository has been archived by the owner on Oct 25, 2019. It is now read-only.
-
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.
- Loading branch information
Romain Gautier
committed
Mar 28, 2017
1 parent
82637a6
commit 4bc245c
Showing
5 changed files
with
362 additions
and
22 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 |
---|---|---|
|
@@ -4,4 +4,10 @@ version = "0.1.0" | |
authors = ["Romain Gautier <[email protected]>"] | ||
|
||
[dependencies] | ||
discord = "^0.8.0" | ||
discord = "^0.8.0" | ||
futures = "^0.1.9" | ||
|
||
hyper = "^0.9.18" | ||
serde = "^0.9" | ||
serde_json = "^0.9" | ||
serde_derive = "^0.9" |
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,2 +1,8 @@ | ||
# rom-discord-bot | ||
Discord bot for Rise of Midgard | ||
Discord bot for Rise of Midgard. | ||
|
||
TODO: | ||
- pretty print location | ||
- cache api calls | ||
- assign a thread pool to prevent ddos on thread creation (very unlikely) | ||
- get feedbacks |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
Attempt to cache future. Replace with outdatable | ||
*/ | ||
|
||
use std::time::{Duration, Instant}; | ||
|
||
pub trait Cache { | ||
type Item; | ||
|
||
fn cached_or<F>(&mut self, f: F) -> Self::Item | ||
where F: Fn() -> Self::Item; | ||
|
||
fn set_cache(&mut self, item: Self::Item) -> Self::Item; | ||
} | ||
|
||
pub struct TtlCache<T> { | ||
ttl: u64, | ||
last: Instant, | ||
|
||
cached: Option<T>, | ||
} | ||
|
||
impl <T> TtlCache<T> { | ||
fn new(ttl: u64) -> TtlCache<T> { | ||
TtlCache { ttl: ttl, last: Instant::now(), cached: None } | ||
} | ||
} | ||
|
||
impl <T> Cache for TtlCache<T> { | ||
type Item = T; | ||
|
||
fn cached_or<F>(&mut self, f: F) -> Self::Item where F: Fn() -> Self::Item { | ||
|
||
//Refresh the cache | ||
if self.cached == None || self.last.elapsed().as_secs() > ttl { | ||
self.cached = f(); | ||
self.last = Instant::now(); | ||
} | ||
|
||
self.cached | ||
} | ||
|
||
fn set_cache(&mut self, item: Self::Item) -> Self::Item { | ||
self.cached = item; | ||
self.last = Instant::now(); | ||
|
||
self.cached | ||
} | ||
} |
Oops, something went wrong.