diff --git a/src/cache.rs b/src/cache.rs index 0b852d7..87f3189 100644 --- a/src/cache.rs +++ b/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>; +pub type PostListCache = Cache>; #[cfg(feature = "server")] -pub type TagListCache = Cache>; +pub type TagListCache = Cache>; #[cfg(feature = "server")] -pub type SinglePostCache = Cache>; +pub type SinglePostCache = Cache>; #[cfg(feature = "server")] -pub type PostStatsCache = Cache; +pub type PostStatsCache = Cache; #[cfg(feature = "server")] static POST_LIST_CACHE: LazyLock = LazyLock::new(|| { @@ -111,59 +111,59 @@ static TAG_POSTS_CACHE: LazyLock = LazyLock::new(|| { #[cfg(feature = "server")] pub async fn get_post_list(key: &CacheKey) -> Option> { - 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) { - 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> { - 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) { - 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> { - 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) { 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> { 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) { 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 { - 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();