-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache_manager.rs
More file actions
74 lines (60 loc) · 1.96 KB
/
cache_manager.rs
File metadata and controls
74 lines (60 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::collections::HashMap;
use wasm_bindgen::prelude::*;
use std::sync::Mutex;
use lazy_static::lazy_static;
pub struct CacheGroup {
elevation_grid_cache: HashMap<String, Vec<Vec<f64>>>,
vector_tile_cache: HashMap<String, Vec<u8>>,
}
impl CacheGroup {
pub fn new() -> Self {
Self {
elevation_grid_cache: HashMap::new(),
vector_tile_cache: HashMap::new(),
}
}
pub fn store_elevation_grid(&mut self, key: String, grid: Vec<Vec<f64>>) {
self.elevation_grid_cache.insert(key, grid);
}
pub fn get_elevation_grid(&self, key: &str) -> Option<&Vec<Vec<f64>>> {
self.elevation_grid_cache.get(key)
}
pub fn store_vector_tile(&mut self, key: String, tile: Vec<u8>) {
self.vector_tile_cache.insert(key, tile);
}
pub fn get_vector_tile(&self, key: &str) -> Option<&Vec<u8>> {
self.vector_tile_cache.get(key)
}
}
pub struct CacheManager {
groups: HashMap<String, CacheGroup>,
}
impl CacheManager {
pub fn new() -> Self {
Self { groups: HashMap::new() }
}
pub fn register_group(&mut self, group_id: &str) {
self.groups.entry(group_id.to_string()).or_insert_with(CacheGroup::new);
}
pub fn get_group_mut(&mut self, group_id: &str) -> Option<&mut CacheGroup> {
self.groups.get_mut(group_id)
}
pub fn free_group(&mut self, group_id: &str) {
self.groups.remove(group_id);
}
}
lazy_static! {
static ref GLOBAL_CACHE_MANAGER: Mutex<CacheManager> = Mutex::new(CacheManager::new());
}
#[wasm_bindgen]
pub fn register_group_js(group_id: &str) -> Result<(), JsValue> {
let mut mgr = GLOBAL_CACHE_MANAGER.lock().map_err(|e| JsValue::from_str(&e.to_string()))?;
mgr.register_group(group_id);
Ok(())
}
#[wasm_bindgen]
pub fn free_group_js(group_id: &str) -> Result<(), JsValue> {
let mut mgr = GLOBAL_CACHE_MANAGER.lock().map_err(|e| JsValue::from_str(&e.to_string()))?;
mgr.free_group(group_id);
Ok(())
}