37
37
import java .util .*;
38
38
39
39
/**
40
+ * The {@code CacheStore} class is responsible for managing local caches using Caffeine.
41
+ * It automatically initializes caches based on methods annotated with {@link LocalCache},
42
+ * supports retrieving caches by name, and provides functionalities to clear specific or all caches.
40
43
* <p>
41
- * CacheStore class.
44
+ * This class also supports auto-loading caches on application startup and integrates with Spring's
45
+ * application context lifecycle events.
42
46
* </p>
43
47
*
44
48
* @author hoangtien2k3
@@ -51,8 +55,13 @@ public class CacheStore implements ApplicationContextAware {
51
55
*/
52
56
private static final Logger log = LoggerFactory .getLogger (CacheStore .class );
53
57
58
+ /** Stores the caches mapped by their names. */
54
59
private static final HashMap <String , Cache <Object , Object >> caches = new HashMap <>();
60
+
61
+ /** Stores methods annotated with {@link LocalCache} that require auto-loading. */
55
62
private static final Set <Method > autoLoadMethods = new HashSet <>();
63
+
64
+ /** The base package for scanning cache-related methods. */
56
65
private static String reflectionPath ;
57
66
58
67
@ PostConstruct
@@ -61,6 +70,7 @@ private static void init() {
61
70
Reflections reflections = new Reflections (reflectionPath , Scanners .MethodsAnnotated );
62
71
Set <Method > methods =
63
72
reflections .get (Scanners .MethodsAnnotated .with (LocalCache .class ).as (Method .class ));
73
+ log .info ("Found {} methods annotated with @LocalCache" , methods .size ());
64
74
for (Method method : methods ) {
65
75
String className = method .getDeclaringClass ().getSimpleName ();
66
76
LocalCache localCache = method .getAnnotation (LocalCache .class );
@@ -115,30 +125,19 @@ public static List<String> getCaches() {
115
125
}
116
126
117
127
/**
118
- * Clear localCache by serviceName
119
- *
120
- * @param serviceName
121
- * String
122
- * @return count of cleared caches
123
- */
124
- public static int clearCachesInServiceName (String serviceName ) {
125
- return (int ) caches .entrySet ().stream ()
126
- .filter (entry -> entry .getKey ().contains (serviceName ))
127
- .peek (entry -> entry .getValue ().invalidateAll ())
128
- .count ();
129
- }
130
-
131
- /**
132
- * clear localCache by cacheName
128
+ * Clears the specified cache by name.
133
129
*
134
- * @param cacheName
135
- * String
136
- * @return count of cleared caches
130
+ * @param cacheName the name of the cache to clear
131
+ * @return the number of cleared caches (0 or 1)
137
132
*/
138
133
public static int clearCachesByName (String cacheName ) {
134
+ log .info ("Clearing cache: {}" , cacheName );
139
135
return (int ) caches .entrySet ().stream ()
140
136
.filter (entry -> entry .getKey ().equals (cacheName ))
141
- .peek (entry -> entry .getValue ().invalidateAll ())
137
+ .peek (entry -> {
138
+ entry .getValue ().invalidateAll ();
139
+ log .info ("Cache {} cleared" , entry .getKey ());
140
+ })
142
141
.count ();
143
142
}
144
143
@@ -148,36 +147,41 @@ public static int clearCachesByName(String cacheName) {
148
147
* @return count of cleared caches
149
148
*/
150
149
public static int clearAllCaches () {
151
- log .info ("Before clearing, cache size: {}" , caches . size () );
150
+ log .info ("Clearing all caches" );
152
151
int count = 0 ;
153
152
for (Map .Entry <String , Cache <Object , Object >> entry : caches .entrySet ()) {
154
153
count ++;
155
154
entry .getValue ().invalidateAll ();
155
+ log .info ("Cleared cache: {}" , entry .getKey ());
156
156
}
157
- log .info ("After clearing, cleared {} caches" , count );
158
157
return count ;
159
158
}
160
159
161
160
/**
162
- * <p>
163
- * autoLoad.
164
- * </p>
161
+ * Automatically loads caches for methods annotated with {@link LocalCache} that support auto-loading.
165
162
*
166
- * @param event
167
- * a {@link ContextRefreshedEvent} object
163
+ * @param event the application context refresh event
168
164
*/
169
165
@ Async
170
166
@ EventListener
171
167
public void autoLoad (ContextRefreshedEvent event ) {
168
+ log .info ("Received ContextRefreshedEvent: {}" , event .getApplicationContext ());
172
169
if (!autoLoadMethods .isEmpty ()) {
173
- log .info ("=====> Start auto load {} cache <=====" , autoLoadMethods .size ());
170
+ log .info ("=====> Start auto-loading {} caches <=====" , autoLoadMethods .size ());
174
171
for (Method method : autoLoadMethods ) {
175
172
CacheUtils .invokeMethod (method );
173
+ log .info ("Auto-loaded cache for method: {}" , method .getName ());
176
174
}
177
- log .info ("=====> Finish auto load cache <=====" );
175
+ log .info ("=====> Finished auto-loading caches <=====" );
178
176
}
179
177
}
180
178
179
+ /**
180
+ * Sets the application context and determines the base package for scanning cache methods.
181
+ *
182
+ * @param applicationContext the application context
183
+ * @throws BeansException if an error occurs while setting the context
184
+ */
181
185
@ Override
182
186
public void setApplicationContext (ApplicationContext applicationContext ) throws BeansException {
183
187
Class <?> mainApplicationClass = applicationContext
@@ -187,5 +191,6 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
187
191
.next ()
188
192
.getClass ();
189
193
reflectionPath = mainApplicationClass .getPackageName ();
194
+ log .info ("Set reflection path for cache scanning: {}" , reflectionPath );
190
195
}
191
196
}
0 commit comments