mirror of
https://github.com/pradt2/always-online-stun.git
synced 2024-05-03 09:24:56 +08:00
Sort geoip cache
This commit is contained in:
parent
bd5071f892
commit
f24c712728
10
src/geoip.rs
10
src/geoip.rs
@ -1,4 +1,4 @@
|
|||||||
use std::collections::{HashMap};
|
use std::collections::{BTreeMap, HashMap};
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::{ErrorKind};
|
use std::io::{ErrorKind};
|
||||||
use std::io::ErrorKind::Other;
|
use std::io::ErrorKind::Other;
|
||||||
@ -67,14 +67,14 @@ impl IpGeolocationIoClient {
|
|||||||
pub(crate) struct CachedIpGeolocationIpClient {
|
pub(crate) struct CachedIpGeolocationIpClient {
|
||||||
path: String,
|
path: String,
|
||||||
client: IpGeolocationIoClient,
|
client: IpGeolocationIoClient,
|
||||||
map: HashMap<String, GeoIpData>,
|
map: BTreeMap<String, GeoIpData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CachedIpGeolocationIpClient {
|
impl CachedIpGeolocationIpClient {
|
||||||
pub(crate) async fn new(filename: String) -> io::Result<CachedIpGeolocationIpClient> {
|
pub(crate) async fn new(filename: String) -> io::Result<CachedIpGeolocationIpClient> {
|
||||||
let cache_str = tokio::fs::read_to_string(filename.as_str()).await?;
|
let cache_str = tokio::fs::read_to_string(filename.as_str()).await?;
|
||||||
|
|
||||||
let map: HashMap<String, GeoIpData> = serde_json::de::from_str(cache_str.as_str())
|
let map: BTreeMap<String, GeoIpData> = serde_json::de::from_str(cache_str.as_str())
|
||||||
.map_err(|err| io::Error::new(Other, err))?;
|
.map_err(|err| io::Error::new(Other, err))?;
|
||||||
|
|
||||||
let client = CachedIpGeolocationIpClient {
|
let client = CachedIpGeolocationIpClient {
|
||||||
@ -99,7 +99,7 @@ impl CachedIpGeolocationIpClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn save(&self) -> io::Result<()> {
|
pub(crate) async fn save(&self) -> io::Result<()> {
|
||||||
let str = serde_json::ser::to_string(&self.map)
|
let str = serde_json::ser::to_string_pretty(&self.map)
|
||||||
.map_err(|err| io::Error::new(Other, err))?;
|
.map_err(|err| io::Error::new(Other, err))?;
|
||||||
tokio::fs::write(self.path.as_str(), str).await
|
tokio::fs::write(self.path.as_str(), str).await
|
||||||
}
|
}
|
||||||
@ -112,4 +112,4 @@ impl CachedIpGeolocationIpClient {
|
|||||||
self.map.insert(String::from(hostname_or_ip), geo_ip_data.clone());
|
self.map.insert(String::from(hostname_or_ip), geo_ip_data.clone());
|
||||||
Ok(geo_ip_data)
|
Ok(geo_ip_data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user