refactor(cache): use CacheKey directly, remove unnecessary async
This commit is contained in:
parent
62e2045b35
commit
0b594ff719
42
src/cache.rs
42
src/cache.rs
@ -36,7 +36,7 @@ pub enum CacheKey {
|
||||
}
|
||||
|
||||
impl CacheKey {
|
||||
pub fn as_string(&self) -> String {
|
||||
pub(crate) fn as_string(&self) -> String {
|
||||
match self {
|
||||
CacheKey::PublishedPosts { page, per_page } => {
|
||||
format!("posts:list:{}:{}", page, per_page)
|
||||
@ -54,16 +54,16 @@ impl CacheKey {
|
||||
// ============================================================================
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub type PostListCache = Cache<String, Vec<Post>>;
|
||||
pub type PostListCache = Cache<CacheKey, Vec<Post>>;
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub type TagListCache = Cache<String, Vec<Tag>>;
|
||||
pub type TagListCache = Cache<CacheKey, Vec<Tag>>;
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub type SinglePostCache = Cache<String, Option<Post>>;
|
||||
pub type SinglePostCache = Cache<CacheKey, Option<Post>>;
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub type PostStatsCache = Cache<String, PostStats>;
|
||||
pub type PostStatsCache = Cache<CacheKey, PostStats>;
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
static POST_LIST_CACHE: LazyLock<PostListCache> = LazyLock::new(|| {
|
||||
@ -111,59 +111,59 @@ static TAG_POSTS_CACHE: LazyLock<PostListCache> = LazyLock::new(|| {
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn get_post_list(key: &CacheKey) -> Option<Vec<Post>> {
|
||||
POST_LIST_CACHE.get(&key.as_string()).await
|
||||
POST_LIST_CACHE.get(key).await
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn set_post_list(key: &CacheKey, posts: Vec<Post>) {
|
||||
let _ = POST_LIST_CACHE.insert(key.as_string(), posts).await;
|
||||
let _ = POST_LIST_CACHE.insert(key.clone(), posts).await;
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn get_tag_list() -> Option<Vec<Tag>> {
|
||||
TAG_LIST_CACHE.get(&CacheKey::AllTags.as_string()).await
|
||||
TAG_LIST_CACHE.get(&CacheKey::AllTags).await
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn set_tag_list(tags: Vec<Tag>) {
|
||||
let _ = TAG_LIST_CACHE.insert(CacheKey::AllTags.as_string(), tags).await;
|
||||
let _ = TAG_LIST_CACHE.insert(CacheKey::AllTags, tags).await;
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn get_post_by_slug(slug: &str) -> Option<Option<Post>> {
|
||||
SINGLE_POST_CACHE.get(&CacheKey::PostBySlug(slug.to_string()).as_string()).await
|
||||
SINGLE_POST_CACHE.get(&CacheKey::PostBySlug(slug.to_string())).await
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn set_post_by_slug(slug: &str, post: Option<Post>) {
|
||||
let _ = SINGLE_POST_CACHE
|
||||
.insert(CacheKey::PostBySlug(slug.to_string()).as_string(), post)
|
||||
.insert(CacheKey::PostBySlug(slug.to_string()), post)
|
||||
.await;
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn get_posts_by_tag(tag: &str) -> Option<Vec<Post>> {
|
||||
TAG_POSTS_CACHE
|
||||
.get(&CacheKey::PostsByTag(tag.to_string()).as_string())
|
||||
.get(&CacheKey::PostsByTag(tag.to_string()))
|
||||
.await
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn set_posts_by_tag(tag: &str, posts: Vec<Post>) {
|
||||
let _ = TAG_POSTS_CACHE
|
||||
.insert(CacheKey::PostsByTag(tag.to_string()).as_string(), posts)
|
||||
.insert(CacheKey::PostsByTag(tag.to_string()), posts)
|
||||
.await;
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn get_post_stats() -> Option<PostStats> {
|
||||
POST_STATS_CACHE.get(&CacheKey::PostStats.as_string()).await
|
||||
POST_STATS_CACHE.get(&CacheKey::PostStats).await
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn set_post_stats(stats: PostStats) {
|
||||
let _ = POST_STATS_CACHE
|
||||
.insert(CacheKey::PostStats.as_string(), stats)
|
||||
.insert(CacheKey::PostStats, stats)
|
||||
.await;
|
||||
}
|
||||
|
||||
@ -172,36 +172,36 @@ pub async fn set_post_stats(stats: PostStats) {
|
||||
// ============================================================================
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn invalidate_post_lists() {
|
||||
pub fn invalidate_post_lists() {
|
||||
POST_LIST_CACHE.invalidate_all();
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn invalidate_all_tags() {
|
||||
pub fn invalidate_all_tags() {
|
||||
TAG_LIST_CACHE.invalidate_all();
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn invalidate_post_by_slug(slug: &str) {
|
||||
SINGLE_POST_CACHE
|
||||
.invalidate(&CacheKey::PostBySlug(slug.to_string()).as_string())
|
||||
.invalidate(&CacheKey::PostBySlug(slug.to_string()))
|
||||
.await;
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn invalidate_posts_by_tag(tag: &str) {
|
||||
TAG_POSTS_CACHE
|
||||
.invalidate(&CacheKey::PostsByTag(tag.to_string()).as_string())
|
||||
.invalidate(&CacheKey::PostsByTag(tag.to_string()))
|
||||
.await;
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn invalidate_post_stats() {
|
||||
pub fn invalidate_post_stats() {
|
||||
POST_STATS_CACHE.invalidate_all();
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub async fn invalidate_all_post_caches() {
|
||||
pub fn invalidate_all_post_caches() {
|
||||
POST_LIST_CACHE.invalidate_all();
|
||||
TAG_LIST_CACHE.invalidate_all();
|
||||
SINGLE_POST_CACHE.invalidate_all();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user