-
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.
docs(README): update comments and examples to use Chinese language fo…
…r better localization and understanding chore(README): remove unnecessary example code to streamline documentation and improve clarity docs(README): update performance optimization section and enhance future expansion ideas to provide clearer guidance for users feat(cache): implement a generic LRU cache with configurable size based on SsrkitConfig to improve performance and memory management feat(config): create SsrkitConfig struct to hold cache size configurations for islands and templates refactor(island): integrate Cache into IslandManager for efficient island rendering and caching chore(lib): update module exports to include new cache and config modules for better organization chore(render): adjust rendering logic to utilize the new cache system for improved performance and maintainability refactor(template.rs): replace LruCache with a generic Cache type for improved flexibility and maintainability feat(template.rs): add init_template_cache function to initialize the cache with a configurable size feat(template.rs): implement render_template function to simplify template rendering with caching support
- Loading branch information
Jerome
committed
Jul 30, 2024
1 parent
fcbd09b
commit 4370b02
Showing
7 changed files
with
260 additions
and
184 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
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,56 @@ | ||
use crate::config::SsrkitConfig; | ||
use lru::LruCache; | ||
use std::num::NonZeroUsize; | ||
use std::sync::{Mutex, OnceLock}; | ||
|
||
static CONFIG: OnceLock<SsrkitConfig> = OnceLock::new(); | ||
|
||
pub struct Cache<T> { | ||
cache: OnceLock<Mutex<LruCache<String, T>>>, | ||
cache_size_fn: Box<dyn Fn(&SsrkitConfig) -> NonZeroUsize + Send + Sync>, | ||
} | ||
|
||
impl<T: Clone> Cache<T> { | ||
pub fn new( | ||
cache_size_fn: impl Fn(&SsrkitConfig) -> NonZeroUsize + Send + Sync + 'static, | ||
) -> Self { | ||
Self { | ||
cache: OnceLock::new(), | ||
cache_size_fn: Box::new(cache_size_fn), | ||
} | ||
} | ||
|
||
fn get_or_create_cache(&self) -> &Mutex<LruCache<String, T>> { | ||
self.cache.get_or_init(|| { | ||
let config = CONFIG.get().cloned().unwrap_or_else(SsrkitConfig::default); | ||
Mutex::new(LruCache::new((self.cache_size_fn)(&config))) | ||
}) | ||
} | ||
|
||
pub fn insert(&self, key: &str, value: T) -> T { | ||
let mut cache_guard = self.get_or_create_cache().lock().unwrap(); | ||
cache_guard.put(key.to_string(), value.clone()); | ||
value | ||
} | ||
|
||
pub fn get(&self, key: &str) -> Option<T> { | ||
let mut cache_guard = self.get_or_create_cache().lock().unwrap(); | ||
cache_guard.get(key).cloned() | ||
} | ||
|
||
pub fn get_or_insert<F>(&self, key: &str, create_fn: F) -> T | ||
where | ||
F: FnOnce() -> T, | ||
{ | ||
if let Some(value) = self.get(key) { | ||
value | ||
} else { | ||
let new_value = create_fn(); | ||
self.insert(key, new_value) | ||
} | ||
} | ||
} | ||
|
||
pub fn init_cache(config: &SsrkitConfig) { | ||
let _ = CONFIG.set(config.clone()); | ||
} |
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,16 @@ | ||
use std::num::NonZeroUsize; | ||
|
||
#[derive(Clone)] | ||
pub struct SsrkitConfig { | ||
pub island_cache_size: NonZeroUsize, | ||
pub template_cache_size: NonZeroUsize, | ||
} | ||
|
||
impl Default for SsrkitConfig { | ||
fn default() -> Self { | ||
Self { | ||
island_cache_size: NonZeroUsize::new(100).unwrap(), | ||
template_cache_size: NonZeroUsize::new(100).unwrap(), | ||
} | ||
} | ||
} |
Oops, something went wrong.