implement a simple cache for autolink module
This commit is contained in:
parent
ca6f3391d4
commit
bcbc14ed17
@ -15,6 +15,7 @@ use stc::searchers::mtg::{MtgCard, MtgSearcher};
|
||||
use stc::searchers::mediawiki::MediawikiSearcher;
|
||||
|
||||
use regex::Regex;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
pub struct AutolinkModule {}
|
||||
|
||||
@ -89,14 +90,17 @@ impl EventLoop for AutolinkModule {
|
||||
searchers.add_searcher("gcl", Box::new(MediawikiSearcher::new(String::from("https://glitchcity.info/wiki/"))));
|
||||
searchers.add_searcher("wp", Box::new(MediawikiSearcher::new(String::from("https://en.wikipedia.org/wiki/"))));
|
||||
|
||||
let mut searcher_cache = BTreeMap::new();
|
||||
|
||||
loop {
|
||||
match receiver.recv() {
|
||||
Ok(envelope) => {
|
||||
if let Event::Message { ref message } = envelope.event {
|
||||
debug!("Received message from module {:?}... {:?}", envelope.from, message.content);
|
||||
for cap in link_regex.captures_iter(&message.content) {
|
||||
if let Some(item) = searchers.exact_search(&cap[1]) {
|
||||
print_any_link(&item, message);
|
||||
let term = cap[1].to_owned();
|
||||
if let &mut Some(ref mut item) = searcher_cache.entry(term.to_owned()).or_insert_with(|| searchers.exact_search(&term)) {
|
||||
print_any_link(item, message);
|
||||
if let Some(card) = item.downcast_ref::<MtgCard>() {
|
||||
print_mtg_card(card, message);
|
||||
} else if let Some(card) = item.downcast_ref::<YugiohCard>() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user