-
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.
Merge pull request #7 from gierens/page-cache
Add Page Cache This adds library functions page_get_minimal and page_get_updated_at and the page.PageCache to the FUSE code. Fs uses those to cache pages based on their updated_at field and only retrieve the actually needed data from the API. Resolves #2
- Loading branch information
Showing
6 changed files
with
295 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
query PageGetMinimal($id: Int!) { | ||
pages { | ||
single (id: $id) { | ||
id | ||
path | ||
content | ||
createdAt | ||
updatedAt | ||
editor | ||
locale | ||
} | ||
} | ||
} |
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,7 @@ | ||
query PageGetUpdatedAt($id: Int!) { | ||
pages { | ||
single (id: $id) { | ||
updatedAt | ||
} | ||
} | ||
} |
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,62 @@ | ||
use std::collections::HashMap; | ||
use wikijs::{page::PageError, page::PageMinimal, Api}; | ||
|
||
pub(crate) struct PageCache { | ||
pages: HashMap<u64, PageMinimal>, | ||
} | ||
|
||
#[allow(dead_code)] | ||
impl PageCache { | ||
pub(crate) fn new() -> Self { | ||
Self { | ||
pages: HashMap::new(), | ||
} | ||
} | ||
|
||
pub(crate) fn get( | ||
&mut self, | ||
api: &Api, | ||
id: u64, | ||
) -> Result<PageMinimal, PageError> { | ||
if let Some(page) = self.pages.get(&id) { | ||
let updated_at = api.page_get_updated_at(id as i64)?; | ||
if updated_at != page.updated_at { | ||
let page = api.page_get_minimal(id as i64)?; | ||
self.pages.insert(id, page.clone()); | ||
Ok(page) | ||
} else { | ||
Ok(page.clone()) | ||
} | ||
} else { | ||
let page = api.page_get_minimal(id as i64)?; | ||
self.pages.insert(id, page.clone()); | ||
Ok(page) | ||
} | ||
} | ||
|
||
pub(crate) fn evict(&mut self, id: u64) { | ||
self.pages.remove(&id); | ||
} | ||
|
||
pub(crate) fn refetch( | ||
&mut self, | ||
api: &Api, | ||
id: u64, | ||
) -> Result<PageMinimal, PageError> { | ||
self.pages.remove(&id); | ||
let page = api.page_get_minimal(id as i64)?; | ||
self.pages.insert(id, page.clone()); | ||
Ok(page) | ||
} | ||
|
||
pub(crate) fn update_content( | ||
&mut self, | ||
api: &Api, | ||
id: u64, | ||
content: String, | ||
) -> Result<(), PageError> { | ||
api.page_update_content(id as i64, content)?; | ||
self.refetch(api, id)?; | ||
Ok(()) | ||
} | ||
} |
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