Commit 3fb610d6 authored by Robert Czechowski's avatar Robert Czechowski

Cache db_status requests for up to a minute

parent 904d3ad1
Pipeline #1086 passed with stages
in 24 minutes and 54 seconds
......@@ -1610,8 +1610,25 @@ impl MedalConnection for Connection {
}
fn get_debug_information(&self) -> String {
let duration = Duration::minutes(60);
let now = time::get_time();
let cache_key = "dbstatus";
let query = "SELECT value, date
FROM string_cache
WHERE key = $1";
let db_has_value = if let Some((cached_value, cache_date))//: Option<>
= self.query_map_one(query, &[&cache_key], |row| -> (String, time::Timespec) {(row.get(0), row.get(1))}).unwrap() {
// Cache invalidates once per minute
if cache_date.sec / 60 >= now.sec / 60 {
return cached_value;
}
true
} else {
false
};
let duration = Duration::minutes(60);
let then = now - duration;
let query = "SELECT count(*)
......@@ -1658,7 +1675,7 @@ impl MedalConnection for Connection {
let n_participations_by_id: Vec<(i32, i64)> =
self.query_map_many(query, &[], |row| (row.get(0), row.get(1))).unwrap();
format!(
let result = format!(
"{{
\"timestamp\": {},
\"active_sessions\": {},
......@@ -1687,7 +1704,19 @@ impl MedalConnection for Connection {
.map(|(x, y)| -> String { format!("\"{}\": {}", x, y) })
.collect::<Vec<String>>()
.join(",\n ")
)
);
let query = if db_has_value {
"UPDATE string_cache
SET value = $2, date = $3
WHERE key = $1"
} else {
"INSERT INTO string_cache (key, value, date)
VALUES ($1, $2, $3)"
};
self.execute(query, &[&cache_key, &result, &now]).unwrap();
result
}
fn reset_all_contest_visibilities(&self) { self.execute("UPDATE contest SET public = $1", &[&false]).unwrap(); }
......
......@@ -1729,8 +1729,25 @@ impl MedalConnection for Connection {
}
fn get_debug_information(&self) -> String {
let duration = Duration::minutes(60);
let now = time::get_time();
let cache_key = "dbstatus";
let query = "SELECT value, date
FROM string_cache
WHERE key = $1";
let db_has_value = if let Some((cached_value, cache_date))//: Option<>
= self.query_map_one(query, &[&cache_key], |row| -> (String, time::Timespec) {(row.get(0), row.get(1))}).unwrap() {
// Cache invalidates once per minute
if cache_date.sec / 60 >= now.sec / 60 {
return cached_value;
}
true
} else {
false
};
let duration = Duration::minutes(60);
let then = now - duration;
let query = "SELECT count(*)
......@@ -1777,7 +1794,7 @@ impl MedalConnection for Connection {
let n_participations_by_id: Vec<(i32, i64)> =
self.query_map_many(query, &[], |row| (row.get(0), row.get(1))).unwrap();
format!(
let result = format!(
"{{
\"timestamp\": {},
\"active_sessions\": {},
......@@ -1806,7 +1823,19 @@ impl MedalConnection for Connection {
.map(|(x, y)| -> String { format!("\"{}\": {}", x, y) })
.collect::<Vec<String>>()
.join(",\n ")
)
);
let query = if db_has_value {
"UPDATE string_cache
SET value = $2, date = $3
WHERE key = $1"
} else {
"INSERT INTO string_cache (key, value, date)
VALUES ($1, $2, $3)"
};
self.execute(query, &[&cache_key, &result, &now]).unwrap();
result
}
fn reset_all_contest_visibilities(&self) { self.execute("UPDATE contest SET public = $1", &[&false]).unwrap(); }
......
......@@ -1729,8 +1729,25 @@ impl MedalConnection for Connection {
}
fn get_debug_information(&self) -> String {
let duration = Duration::minutes(60);
let now = time::get_time();
let cache_key = "dbstatus";
let query = "SELECT value, date
FROM string_cache
WHERE key = ?1";
let db_has_value = if let Some((cached_value, cache_date))//: Option<>
= self.query_map_one(query, &[&cache_key], |row| -> (String, time::Timespec) {(row.get(0), row.get(1))}).unwrap() {
// Cache invalidates once per minute
if cache_date.sec / 60 >= now.sec / 60 {
return cached_value;
}
true
} else {
false
};
let duration = Duration::minutes(60);
let then = now - duration;
let query = "SELECT count(*)
......@@ -1777,7 +1794,7 @@ impl MedalConnection for Connection {
let n_participations_by_id: Vec<(i32, i64)> =
self.query_map_many(query, &[], |row| (row.get(0), row.get(1))).unwrap();
format!(
let result = format!(
"{{
\"timestamp\": {},
\"active_sessions\": {},
......@@ -1806,7 +1823,19 @@ impl MedalConnection for Connection {
.map(|(x, y)| -> String { format!("\"{}\": {}", x, y) })
.collect::<Vec<String>>()
.join(",\n ")
)
);
let query = if db_has_value {
"UPDATE string_cache
SET value = ?2, date = ?3
WHERE key = ?1"
} else {
"INSERT INTO string_cache (key, value, date)
VALUES (?1, ?2, ?3)"
};
self.execute(query, &[&cache_key, &result, &now]).unwrap();
result
}
fn reset_all_contest_visibilities(&self) { self.execute("UPDATE contest SET public = ?1", &[&false]).unwrap(); }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment