Skip to content

Commit

Permalink
feat: reactify-core
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangtien2k3 committed Nov 3, 2024
1 parent d7deb80 commit c65e753
Show file tree
Hide file tree
Showing 107 changed files with 4,068 additions and 1,092 deletions.
20 changes: 16 additions & 4 deletions reactify-cache/src/main/java/com/reactify/cache/CacheUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@

/**
* <p>
* CacheUtils class.
* The CacheUtils class provides utility methods for working with caching
* functionality in the application. It contains methods to invoke
* cached methods dynamically at runtime, allowing for efficient cache
* management and retrieval.
* </p>
*
* <p>
* This class is particularly useful for autoloading cache entries based
* on the methods annotated with {@link LocalCache} and managing their
* execution in a reactive manner using Project Reactor.
* </p>
*
* @author hoangtien2k3
Expand All @@ -36,11 +45,14 @@ public class CacheUtils {

/**
* <p>
* invokeMethod.
* Invokes the specified method and subscribes to its result, enabling
* the execution of the method in a reactive context. This method
* retrieves the bean instance of the declaring class from the Spring
* application context and calls the specified method.
* </p>
*
* @param method
* a {@link Method} object
* @param method a {@link Method} object representing the method to be
* invoked
*/
public static void invokeMethod(Method method) {
try {
Expand Down
15 changes: 7 additions & 8 deletions reactify-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
<version>3.3.5</version>
<relativePath/>
</parent>
<groupId>io.github.hoangtien2k3</groupId>
<groupId>com.ezbuy.platform</groupId>
<artifactId>reactify-core</artifactId>
<version>1.1.7</version>
<version>1.0-SNAPSHOT</version>
<name>reactify-core</name>
<description>Java library for developing backend with reactive programming</description>
<url>https://github.com/hoangtien2k3/reactify</url>

<licenses>
<license>
<name>Apache License, Version 2.0</name>
Expand Down Expand Up @@ -44,8 +43,8 @@
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

<properties>
<revision>1.0-SNAPSHOT</revision>
<java.version>21</java.version>
<micrometer.tracing.version>1.3.1</micrometer.tracing.version>
<micrometer.registry.version>1.13.1</micrometer.registry.version>
Expand All @@ -72,7 +71,6 @@
<sonar.organization>hoangtien2k3</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
Expand All @@ -84,7 +82,6 @@
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -102,6 +99,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
Expand Down Expand Up @@ -236,7 +237,6 @@
<version>${modelmapper.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -388,5 +388,4 @@
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* This annotation should be applied to methods that return a result that can
* benefit from caching.
*
* <h3>Usage Example:</h3>
* <h2>Usage Example:</h2>
*
* <pre>
* &#64;LocalCache(durationInMinute = 10, maxRecord = 500, autoCache = true)
Expand All @@ -49,7 +49,9 @@
* }
* </pre>
*
* <h3>Annotation Properties:</h3>
* <h2>Annotation Properties:</h2> <!-- Changed
* <h3>to
* <h2>-->
* <dl>
* <dt>durationInMinute</dt>
* <dd>The time period in minutes for which the cached data is valid. Default is
Expand All @@ -68,13 +70,33 @@
* This annotation is intended for use in performance-sensitive applications
* where reducing latency and resource consumption is critical.
* </p>
*
* @author hoangtien2k3
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LocalCache {
/**
* Specifies the duration (in minutes) for which the cache entry is valid.
* Default value is 120 minutes.
*
* @return the duration in minutes
*/
int durationInMinute() default 120;

/**
* Specifies the maximum number of records that can be stored in the cache.
* Default value is 1000 records.
*
* @return the maximum number of records
*/
int maxRecord() default 1000;

/**
* Indicates whether the caching should be enabled automatically. Default value
* is false.
*
* @return true if auto-cache is enabled, false otherwise
*/
boolean autoCache() default false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,72 @@

/**
* <p>
* LogPerformance class.
* Indicates that performance logging should be applied to a method or a class.
* This annotation can be used to track the execution time and other performance
* metrics of the annotated method or class, which is useful for monitoring and
* optimizing application performance.
* </p>
*
* <p>
* This annotation supports various logging configurations such as specifying
* the type of log, whether to log input parameters, output results, and a title
* for the log entry.
* </p>
*
* <p>
* <strong>Usage Example:</strong>
* </p>
*
* <pre>
* &#64;LogPerformance(logType = "INFO", actionType = "DATABASE_OPERATION", logOutput = true)
* public void fetchData() {
* // Method implementation
* }
* </pre>
*
* <p>
* <strong>Note:</strong> The actual logging mechanism must be configured
* separately in the application.
* </p>
*
* @author hoangtien2k3
* @version 1.0
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface LogPerformance {
/**
* The type of log for categorizing the log entries.
*
* @return the log type as a string
*/
String logType() default "";

/**
* The type of action that the log entry is recording.
*
* @return the action type as a string
*/
String actionType() default "";

/**
* Specifies whether to log the output or not.
*
* @return true if output logging is enabled, false otherwise
*/
boolean logOutput() default true;

/**
* Specifies whether to log the input or not.
*
* @return true if input logging is enabled, false otherwise
*/
boolean logInput() default true;

/**
* The title for the performance log entry.
*
* @return the title as a string
*/
String title() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,34 @@

/**
* <p>
* Loggable class.
* Indicates that a method should be logged when it is executed. This annotation
* can be used to track method calls, parameters, and return values, which is
* useful for debugging and monitoring application behavior.
* </p>
*
* <p>
* This annotation is typically processed by an aspect-oriented programming
* (AOP) framework that intercepts method calls and performs the logging.
* </p>
*
* <p>
* <strong>Usage Example:</strong>
* </p>
*
* <pre>
* &#64;Loggable
* public void performAction(String parameter) {
* // Method implementation
* }
* </pre>
*
* <p>
* <strong>Note:</strong> The actual logging behavior must be configured
* separately in the application, depending on the logging framework in use.
* </p>
*
* @author hoangtien2k3
* @version 1.0
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
package com.reactify.annotations.cache;

import com.github.benmanes.caffeine.cache.Cache;
import java.util.Objects;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
Expand All @@ -30,9 +28,29 @@
import reactor.core.publisher.Mono;
import reactor.core.publisher.Signal;

import java.util.Objects;
import java.util.Optional;

/**
* <p>
* CacheAspect class.
* The {@code CacheAspect} class provides caching functionality for methods
* annotated with {@link com.reactify.annotations.LocalCache}. This aspect
* intercepts method calls, checks the cache for existing results, and returns
* cached results if available. If no cached result is found, the method is
* executed and the result is stored in the cache for future calls.
* </p>
*
* <p>
* This class uses Spring AOP (Aspect-Oriented Programming) features to
* implement caching logic around method executions, providing a way to enhance
* performance by avoiding redundant computations or data retrievals.
* </p>
*
* <p>
* The class is annotated with {@link Aspect} and
* {@link Configuration}, making it a
* Spring managed bean that can intercept method calls. The logging is managed
* using the Lombok {@link Slf4j} annotation.
* </p>
*
* @author hoangtien2k3
Expand All @@ -42,19 +60,37 @@
@Slf4j
public class CacheAspect {

/**
* Constructs a new instance of {@code CacheAspect}.
*/
public CacheAspect() {}

/**
* <p>
* Pointcut that matches methods annotated with
* {@link com.reactify.annotations.LocalCache}.
* </p>
*/
@Pointcut("@annotation(com.reactify.annotations.LocalCache)")
private void processAnnotation() {}

/**
* <p>
* aroundAdvice.
* Around advice that intercepts method calls annotated with
* {@link com.reactify.annotations.LocalCache}. This method checks for a cached
* result using the generated key based on the method arguments. If a cached
* result is found, it is returned; otherwise, the method is executed, and the
* result is stored in the cache.
* </p>
*
* @param joinPoint
* a {@link ProceedingJoinPoint} object
* @return a {@link Object} object
* a {@link ProceedingJoinPoint} object representing
* the method execution context.
* @return an {@link Object} that is the result of the method
* execution or the cached result.
* @throws Throwable
* if any.
* if any exception occurs during method execution or while
* accessing the cache.
*/
@Around("processAnnotation()")
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
Expand Down
Loading

0 comments on commit c65e753

Please sign in to comment.