-
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.
## Summary Add cache option for tauri plugin. ## Test Plan Should pass CI.
- Loading branch information
Showing
6 changed files
with
112 additions
and
17 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
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,61 @@ | ||
use std::hash::Hash; | ||
use webview_bundle::Bundle; | ||
|
||
pub trait Cache<K: Hash + Eq, V> { | ||
fn has(&self, key: &K) -> bool; | ||
fn get(&mut self, key: &K) -> Option<&V>; | ||
fn set(&mut self, key: K, value: V); | ||
} | ||
|
||
#[derive(Clone, Default)] | ||
pub struct NoopCache; | ||
|
||
impl Cache<String, Bundle> for NoopCache { | ||
fn has(&self, _key: &String) -> bool { | ||
false | ||
} | ||
|
||
fn get(&mut self, _key: &String) -> Option<&Bundle> { | ||
None | ||
} | ||
|
||
fn set(&mut self, _key: String, _value: Bundle) {} | ||
} | ||
|
||
#[cfg(feature = "cache-lru")] | ||
#[derive(Clone)] | ||
pub struct LruCache<K: Hash + Eq, V> { | ||
cache: lru::LruCache<K, V>, | ||
} | ||
|
||
#[cfg(feature = "cache-lru")] | ||
impl<K: Hash + Eq, V> LruCache<K, V> { | ||
pub fn new(size: usize) -> Self { | ||
Self { | ||
cache: lru::LruCache::<K, V>::new( | ||
std::num::NonZeroUsize::new(size).expect("size is not non zero"), | ||
), | ||
} | ||
} | ||
|
||
pub fn unbounded() -> Self { | ||
Self { | ||
cache: lru::LruCache::<K, V>::unbounded(), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "cache-lru")] | ||
impl Cache<String, Bundle> for LruCache<String, Bundle> { | ||
fn has(&self, key: &String) -> bool { | ||
self.cache.contains(key) | ||
} | ||
|
||
fn get(&mut self, key: &String) -> Option<&Bundle> { | ||
self.cache.get(key) | ||
} | ||
|
||
fn set(&mut self, key: String, value: Bundle) { | ||
self.cache.put(key, value); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,20 +1,32 @@ | ||
use crate::cache::Cache; | ||
use crate::loader::Loader; | ||
use webview_bundle::Bundle; | ||
|
||
pub struct Config<L: Loader + Send + Sync> { | ||
pub struct Config<L, C> | ||
where | ||
L: Loader + Send + Sync, | ||
C: Cache<String, Bundle> + Send + Sync, | ||
{ | ||
loader: L, | ||
cache: C, | ||
} | ||
|
||
#[buildstructor::buildstructor] | ||
impl<L> Config<L> | ||
impl<L, C> Config<L, C> | ||
where | ||
L: Loader + Send + Sync, | ||
C: Cache<String, Bundle> + Send + Sync, | ||
{ | ||
#[builder] | ||
pub fn new(loader: L) -> Self { | ||
Self { loader } | ||
pub fn new(loader: L, cache: C) -> Self { | ||
Self { loader, cache } | ||
} | ||
|
||
pub fn loader(&self) -> &L { | ||
&self.loader | ||
} | ||
|
||
pub fn cache(&self) -> &C { | ||
&self.cache | ||
} | ||
} |
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