11package top .howiehz .halo .plugin .extra .api .service ;
22
3- import lombok .RequiredArgsConstructor ;
4- import lombok .extern .slf4j .Slf4j ;
53import org .springframework .lang .Nullable ;
6- import org .springframework .stereotype .Component ;
7- import java .util .concurrent .ConcurrentHashMap ;
8- import java .util .concurrent .atomic .AtomicReference ;
94import java .math .BigInteger ;
105
11- /**
12- * Stats Data Manager - 统计数据管理器。
13- * 分别缓存发布版本和草稿版本的字数统计。
14- */
15- @ Slf4j
16- @ Component
17- @ RequiredArgsConstructor
18- public class PostStatsDataCacheManager {
19-
20- // 文章发布版本字数统计缓存 / Total & per-post caches for release version
21- private final AtomicReference <BigInteger > totalReleasePostWordCount =
22- new AtomicReference <>(null );
23- final ConcurrentHashMap <String , BigInteger > releasePostWordCounts = new ConcurrentHashMap <>();
24- // 文章草稿版本字数统计缓存 / Total & per-post caches for draft version
25- private final AtomicReference <BigInteger > totalDraftPostWordCount = new AtomicReference <>(null );
26- final ConcurrentHashMap <String , BigInteger > draftPostWordCounts = new ConcurrentHashMap <>();
27-
28- /**
29- * 获取缓存的总字数。
30- * Get cached total word count.
31- *
32- * @param isDraft 是否为草稿版本 / Whether it's draft version
33- * @return 总字数,未缓存时返回 null / Total word count, returns null if not cached
34- */
6+ public interface PostStatsDataCacheManager {
357 @ Nullable
36- public BigInteger getCachedTotalWordCount (boolean isDraft ) {
37- return isDraft ? totalDraftPostWordCount .get () : totalReleasePostWordCount .get ();
38- }
8+ BigInteger getCachedTotalWordCount (boolean isDraft );
399
40- /**
41- * 获取缓存的文章字数。
42- * Get cached word count for a specific post.
43- *
44- * @param postName 文章名称 / Post name
45- * @param isDraft 是否为草稿版本 / Whether it's draft version
46- * @return 文章字数,未缓存时返回 null / Post word count, returns null if not cached
47- */
4810 @ Nullable
49- public BigInteger getCachedPostWordCount (String postName , boolean isDraft ) {
50- return isDraft ? draftPostWordCounts .get (postName ) : releasePostWordCounts .get (postName );
51- }
52-
53- /**
54- * 设置/更新总字数缓存。
55- * Set/Update total word count cache.
56- *
57- * @param count 总字数 / Total word count
58- * @param isDraft 是否为草稿版本 / Whether it's draft version
59- */
60- public void setTotalPostWordCount (BigInteger count , boolean isDraft ) {
61- if (isDraft ) {
62- totalDraftPostWordCount .set (count );
63- log .debug ("Set total DRAFT word count cache: {}" , count );
64- } else {
65- totalReleasePostWordCount .set (count );
66- log .debug ("Set total RELEASE word count cache: {}" , count );
67- }
68- }
11+ BigInteger getCachedPostWordCount (String postName , boolean isDraft );
6912
70- /**
71- * 设置/更新特定文章的字数缓存。
72- * Set/Update word count cache for a specific post.
73- *
74- * @param postName 文章名称 / Post name
75- * @param count 字数 / Word count
76- * @param isDraft 是否为草稿版本 / Whether it's draft version
77- */
78- public void setPostWordCount (String postName , BigInteger count , boolean isDraft ) {
79- if (isDraft ) {
80- draftPostWordCounts .put (postName , count );
81- log .debug ("Set DRAFT word count cache for post {}: {}" , postName , count );
82- } else {
83- releasePostWordCounts .put (postName , count );
84- log .debug ("Set RELEASE word count cache for post {}: {}" , postName , count );
85- }
86- }
13+ void setTotalPostWordCount (BigInteger count , boolean isDraft );
8714
88- /**
89- * 清除总字数缓存。
90- * Clear total word count cache.
91- *
92- * @param isDraft 是否为草稿版本 / Whether it's draft version
93- */
94- public void clearTotalPostWordCountCache (boolean isDraft ) {
95- if (isDraft ) {
96- totalDraftPostWordCount .set (null );
97- log .debug ("Cleared total DRAFT word count cache" );
98- } else {
99- totalReleasePostWordCount .set (null );
100- log .debug ("Cleared total RELEASE word count cache" );
101- }
102- }
15+ void setPostWordCount (String postName , BigInteger count , boolean isDraft );
10316
104- /**
105- * 清除特定文章的字数缓存。
106- * Clear word count cache for a specific post.
107- *
108- * @param postName 文章名称 / Post name
109- * @param isDraft 是否为草稿版本 / Whether it's draft version
110- */
111- public void clearPostWordCountCache (String postName , boolean isDraft ) {
112- if (postName == null || postName .trim ().isEmpty ()) {
113- log .warn (
114- "Post name is null or empty, skipping cache clear / 文章名称为空,跳过缓存清理" );
115- return ;
116- }
17+ void clearTotalPostWordCountCache (boolean isDraft );
11718
118- if (isDraft ) {
119- draftPostWordCounts .remove (postName );
120- log .debug ("Cleared DRAFT word count cache for post" );
121- } else {
122- releasePostWordCounts .remove (postName );
123- log .debug ("Cleared RELEASE word count cache for post" );
124- }
125- }
126- }
19+ void clearPostWordCountCache (String postName , boolean isDraft );
20+ }
0 commit comments