Skip to content

Commit 079962d

Browse files
committed
feat: update reactify-cache
1 parent a3eb19b commit 079962d

File tree

30 files changed

+191
-1125
lines changed

30 files changed

+191
-1125
lines changed

reactify-cache/.mvn/wrapper/maven-wrapper.properties

Lines changed: 0 additions & 19 deletions
This file was deleted.

reactify-cache/src/main/java/io/hoangtien2k3/cache/ApplicationContextProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package io.hoangtien2k3.cache;
1718

1819
import org.springframework.beans.BeansException;

reactify-cache/src/main/java/io/hoangtien2k3/cache/CacheAspect.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package io.hoangtien2k3.cache;
1718

1819
import com.github.benmanes.caffeine.cache.Cache;
20+
import java.util.Objects;
21+
import java.util.Optional;
1922
import lombok.extern.slf4j.Slf4j;
2023
import org.aspectj.lang.ProceedingJoinPoint;
2124
import org.aspectj.lang.annotation.Around;
@@ -28,9 +31,6 @@
2831
import reactor.core.publisher.Mono;
2932
import reactor.core.publisher.Signal;
3033

31-
import java.util.Objects;
32-
import java.util.Optional;
33-
3434
/**
3535
* <p>
3636
* CacheAspect class.
@@ -64,7 +64,6 @@ public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
6464
String name = ClassUtils.getUserClass(joinPoint.getTarget().getClass()).getSimpleName() + "."
6565
+ joinPoint.getSignature().getName();
6666
Cache<Object, Object> cache = CacheStore.getCache(name);
67-
// return cached mono
6867
return CacheMono.lookup(k -> Mono.justOrEmpty(cache.getIfPresent(key)).map(Signal::next), key)
6968
.onCacheMissResume((Mono<Object>) joinPoint.proceed(args))
7069
.andWriteWith((k, sig) -> Mono.fromRunnable(() -> {

reactify-cache/src/main/java/io/hoangtien2k3/cache/CacheStore.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,28 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package io.hoangtien2k3.cache;
1718

1819
import com.github.benmanes.caffeine.cache.Cache;
1920
import com.github.benmanes.caffeine.cache.Caffeine;
2021
import com.github.benmanes.caffeine.cache.Scheduler;
22+
import java.lang.reflect.Method;
23+
import java.time.Duration;
24+
import java.util.*;
25+
import javax.annotation.PostConstruct;
2126
import lombok.extern.slf4j.Slf4j;
2227
import org.reflections.Reflections;
2328
import org.reflections.scanners.Scanners;
29+
import org.springframework.beans.BeansException;
30+
import org.springframework.boot.autoconfigure.SpringBootApplication;
31+
import org.springframework.context.ApplicationContext;
32+
import org.springframework.context.ApplicationContextAware;
2433
import org.springframework.context.event.ContextRefreshedEvent;
2534
import org.springframework.context.event.EventListener;
2635
import org.springframework.scheduling.annotation.Async;
2736
import org.springframework.stereotype.Component;
2837

29-
import javax.annotation.PostConstruct;
30-
import java.lang.reflect.Method;
31-
import java.time.Duration;
32-
import java.util.HashMap;
33-
import java.util.HashSet;
34-
import java.util.Set;
35-
3638
/**
3739
* <p>
3840
* CacheStore class.
@@ -42,11 +44,11 @@
4244
*/
4345
@Slf4j
4446
@Component
45-
public class CacheStore {
47+
public class CacheStore implements ApplicationContextAware {
4648

4749
private static final HashMap<String, Cache<Object, Object>> caches = new HashMap<>();
4850
private static final Set<Method> autoLoadMethods = new HashSet<>();
49-
private static final String reflectionPath = "com.ezbuy";
51+
private static String reflectionPath;
5052

5153
@PostConstruct
5254
private static void init() {
@@ -115,4 +117,15 @@ public void autoLoad(ContextRefreshedEvent event) {
115117
log.info("Finish auto load cache");
116118
}
117119
}
120+
121+
@Override
122+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
123+
Class<?> mainApplicationClass = applicationContext
124+
.getBeansWithAnnotation(SpringBootApplication.class)
125+
.values()
126+
.iterator()
127+
.next()
128+
.getClass();
129+
reflectionPath = mainApplicationClass.getPackageName();
130+
}
118131
}

reactify-cache/src/main/java/io/hoangtien2k3/cache/CacheUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package io.hoangtien2k3.cache;
1718

19+
import java.lang.reflect.Method;
1820
import lombok.RequiredArgsConstructor;
1921
import lombok.extern.slf4j.Slf4j;
2022
import org.springframework.stereotype.Component;
2123
import reactor.core.publisher.Mono;
2224

23-
import java.lang.reflect.Method;
24-
2525
/**
2626
* <p>
2727
* CacheUtils class.

reactify-cache/src/main/java/io/hoangtien2k3/cache/CustomizeRemovalListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package io.hoangtien2k3.cache;
1718

1819
import com.github.benmanes.caffeine.cache.RemovalCause;
1920
import com.github.benmanes.caffeine.cache.RemovalListener;
21+
import java.lang.reflect.Method;
2022
import lombok.AllArgsConstructor;
2123
import lombok.extern.slf4j.Slf4j;
2224
import org.checkerframework.checker.nullness.qual.NonNull;
2325
import org.checkerframework.checker.nullness.qual.Nullable;
2426

25-
import java.lang.reflect.Method;
26-
2727
/**
2828
* <p>
2929
* CustomizeRemovalListener class.

reactify-cache/src/main/java/io/hoangtien2k3/cache/LocalCache.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package io.hoangtien2k3.cache;
1718

1819
import java.lang.annotation.ElementType;
@@ -21,52 +22,51 @@
2122
import java.lang.annotation.Target;
2223

2324
/**
24-
* Annotation to indicate that a method's result should be cached locally. This
25-
* can improve performance by avoiding redundant computations or repeated data
26-
* retrieval from external sources.
25+
* Marks a method for local caching to enhance performance by reducing redundant
26+
* computations or repeated data retrievals from external sources.
2727
*
2828
* <p>
29-
* The caching behavior is defined by the properties of the annotation:
29+
* This annotation enables flexible caching configurations through the following
30+
* properties:
3031
* <ul>
31-
* <li><strong>durationInMinute</strong>: The duration for which the cached
32-
* result is valid, specified in minutes.</li>
33-
* <li><strong>maxRecord</strong>: The maximum number of records that can be
34-
* stored in the cache.</li>
35-
* <li><strong>autoCache</strong>: A flag indicating whether caching should be
36-
* done automatically or manually.</li>
32+
* <li><strong>durationInMinute</strong>: Specifies the validity duration of the
33+
* cached result, in minutes.</li>
34+
* <li><strong>maxRecord</strong>: Defines the maximum number of records the
35+
* cache can store.</li>
36+
* <li><strong>autoCache</strong>: A flag that determines whether the caching
37+
* should activate automatically upon method invocation.</li>
3738
* </ul>
3839
*
3940
* <p>
40-
* This annotation should be applied to methods that return a result that can
41-
* benefit from caching.
41+
* This annotation is best applied to methods where caching can notably improve
42+
* efficiency by retaining data that does not require frequent recalculation.
4243
*
43-
* <h3>Usage Example:</h3>
44+
* <h3>Example Usage:</h3>
4445
*
4546
* <pre>
4647
* &#64;LocalCache(durationInMinute = 10, maxRecord = 500, autoCache = true)
4748
* public MyObject fetchData(String param) {
48-
* // Method implementation that retrieves data
49+
* // Implementation that fetches or computes data
4950
* }
5051
* </pre>
5152
*
5253
* <h3>Annotation Properties:</h3>
5354
* <dl>
54-
* <dt>durationInMinute</dt>
55-
* <dd>The time period in minutes for which the cached data is valid. Default is
56-
* 120 minutes.</dd>
55+
* <dt><strong>durationInMinute</strong></dt>
56+
* <dd>Defines the cache retention time in minutes. Default is 120 minutes.</dd>
5757
*
58-
* <dt>maxRecord</dt>
59-
* <dd>The maximum number of entries that can be cached. Default is 1000
60-
* entries.</dd>
58+
* <dt><strong>maxRecord</strong></dt>
59+
* <dd>Limits the number of records that can be cached simultaneously. Default
60+
* is 1000 entries.</dd>
6161
*
62-
* <dt>autoCache</dt>
63-
* <dd>If set to true, the method result will be automatically cached. Default
64-
* is false.</dd>
62+
* <dt><strong>autoCache</strong></dt>
63+
* <dd>When set to <code>true</code>, the method result is automatically cached
64+
* on execution. Default is <code>false</code>.</dd>
6565
* </dl>
6666
*
6767
* <p>
68-
* This annotation is intended for use in performance-sensitive applications
69-
* where reducing latency and resource consumption is critical.
68+
* This annotation is ideal for performance-critical applications aiming to
69+
* reduce latency and optimize resource usage through local caching.
7070
* </p>
7171
*/
7272
@Target(ElementType.METHOD)

reactify-core/src/main/java/io/hoangtien2k3/reactify/annotations/cache/CacheStore.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
import lombok.extern.slf4j.Slf4j;
2929
import org.reflections.Reflections;
3030
import org.reflections.scanners.Scanners;
31+
import org.springframework.beans.BeansException;
32+
import org.springframework.boot.autoconfigure.SpringBootApplication;
33+
import org.springframework.context.ApplicationContext;
34+
import org.springframework.context.ApplicationContextAware;
3135
import org.springframework.context.event.ContextRefreshedEvent;
3236
import org.springframework.context.event.EventListener;
3337
import org.springframework.scheduling.annotation.Async;
@@ -42,11 +46,11 @@
4246
*/
4347
@Slf4j
4448
@Component
45-
public class CacheStore {
49+
public class CacheStore implements ApplicationContextAware {
4650

4751
private static final HashMap<String, Cache<Object, Object>> caches = new HashMap<>();
4852
private static final Set<Method> autoLoadMethods = new HashSet<>();
49-
private static final String reflectionPath = "io.hoangtien2k3";
53+
private static String reflectionPath;
5054

5155
@PostConstruct
5256
private static void init() {
@@ -115,4 +119,15 @@ public void autoLoad(ContextRefreshedEvent event) {
115119
log.info("Finish auto load cache");
116120
}
117121
}
122+
123+
@Override
124+
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
125+
Class<?> mainApplicationClass = applicationContext
126+
.getBeansWithAnnotation(SpringBootApplication.class)
127+
.values()
128+
.iterator()
129+
.next()
130+
.getClass();
131+
reflectionPath = mainApplicationClass.getPackageName();
132+
}
118133
}

reactify-demo/reactify-cache-starter-demo/.gitattributes

Lines changed: 0 additions & 2 deletions
This file was deleted.

reactify-demo/reactify-cache-starter-demo/.gitignore

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)