-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#15 Cached by proxy (cglib) in spring
- Loading branch information
Showing
17 changed files
with
124 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package app.prime.controller; | ||
|
||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Test configuration with lazy bean init. | ||
* | ||
* @author Vladimir Ivanov ([email protected]) | ||
*/ | ||
@Configuration | ||
@ComponentScan(lazyInit = true, value = {"app.prime.controller", "app.prime.service", "app.prime.cache", "app.prime.log"}) | ||
public class TestConfig { | ||
} |
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
service/src/main/java/app/prime/cache/CacheBeanPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package app.prime.cache; | ||
|
||
import lombok.extern.log4j.Log4j2; | ||
import org.jooq.lambda.Unchecked; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.cglib.proxy.Callback; | ||
import org.springframework.cglib.proxy.Enhancer; | ||
import org.springframework.cglib.proxy.MethodInterceptor; | ||
import org.springframework.cglib.proxy.MethodProxy; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* Add cacheable proxy to bean annotated with cache. | ||
* | ||
* @author Vladimir Ivanov ([email protected]) | ||
*/ | ||
@Log4j2 | ||
@Component | ||
public class CacheBeanPostProcessor implements BeanPostProcessor { | ||
@Override | ||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { | ||
if (bean.getClass().isAnnotationPresent(WithCache.class)) { | ||
log.debug("Wrapping with cache " + bean); | ||
return proxy(bean); | ||
} | ||
return bean; | ||
} | ||
|
||
private static Object proxy(Object bean) { | ||
Callback callback = new MethodInterceptor() { | ||
private final Map<Object, Object> cache = new ConcurrentHashMap<>(); | ||
|
||
private List<Object> getCachekey(Method method, Object[] args) { | ||
List<Object> list = new ArrayList<>(); | ||
list.add(method.getName()); | ||
list.addAll(Arrays.asList(args)); | ||
return list; | ||
} | ||
|
||
@Override | ||
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) { | ||
List<Object> key = getCachekey(method, args); | ||
log.debug("Intercepted: " + method.getName()); | ||
return cache.computeIfAbsent(key, Unchecked.function(ignored -> { | ||
log.info("Calculating {}.{}({})", bean.getClass().getSimpleName(), method.getName(), Arrays.toString(args)); | ||
long start = System.nanoTime(); | ||
Object invoke = method.invoke(bean, args); | ||
long finish = System.nanoTime(); | ||
log.info("Calculating {}.{}({}) {} ms", bean.getClass().getSimpleName(), method.getName(), Arrays.toString(args), (finish - start) / 1_000L); | ||
return invoke; | ||
})); | ||
} | ||
}; | ||
return Enhancer.create(bean.getClass(), callback); | ||
} | ||
|
||
} |
5 changes: 4 additions & 1 deletion
5
...ce/src/main/java/app/prime/WithCache.java → .../main/java/app/prime/cache/WithCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
package app.prime; | ||
package app.prime.cache; | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Select cached version. | ||
* | ||
* @author Vladimir Ivanov ([email protected]) | ||
*/ | ||
@Qualifier | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface WithCache { | ||
} |
2 changes: 1 addition & 1 deletion
2
...src/main/java/app/prime/WithOutCache.java → ...in/java/app/prime/cache/WithOutCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package app.prime; | ||
package app.prime.cache; | ||
|
||
import org.springframework.beans.factory.annotation.Qualifier; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
package app.prime.service; | ||
|
||
import app.prime.WithCache; | ||
import app.prime.WithOutCache; | ||
import app.prime.cache.WithOutCache; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
@@ -14,8 +12,6 @@ | |
* | ||
* @author Vladimir Ivanov ([email protected]) | ||
*/ | ||
@Service | ||
@WithCache | ||
public class CachedPrimeFactorService implements PrimeFactorService { | ||
private final PrimeFactorService primeFactorService; | ||
private final Map<Long, List<Long>> cache; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
package app.prime.service; | ||
|
||
import app.prime.WithCache; | ||
import app.prime.WithOutCache; | ||
import app.prime.cache.WithOutCache; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
@@ -13,8 +11,6 @@ | |
* | ||
* @author Vladimir Ivanov ([email protected]) | ||
*/ | ||
@Service | ||
@WithCache | ||
public class CachedPrimeService implements PrimeService { | ||
private final PrimeService primeService; | ||
private final Map<Long, Boolean> cache; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
package app.prime.service; | ||
|
||
import app.prime.Logged; | ||
import app.prime.WithCache; | ||
import app.prime.WithOutCache; | ||
import app.prime.cache.WithCache; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
|
@@ -15,22 +14,20 @@ | |
* @author Vladimir Ivanov ([email protected]) | ||
*/ | ||
@Service | ||
@WithOutCache | ||
@WithCache | ||
public class SimplePrimeFactorService implements PrimeFactorService { | ||
final PrimeService primeService; | ||
|
||
@Autowired | ||
@WithCache | ||
public SimplePrimeFactorService(PrimeService primeService) { | ||
this.primeService = primeService; | ||
PrimeService primeService; | ||
|
||
public SimplePrimeFactorService() { | ||
} | ||
|
||
/** | ||
* List prime factors. | ||
*/ | ||
@Logged | ||
public List<Long> findPrimeFactors(final long number) { | ||
|
||
//SimplePrimeFactorService proxy = (SimplePrimeFactorService) AopContext.currentProxy(); | ||
final List<Long> primes = new ArrayList<>(); | ||
|
||
long divide = number; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package app.prime.service; | ||
|
||
import app.prime.Logged; | ||
import app.prime.WithOutCache; | ||
import app.prime.cache.WithCache; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
|
@@ -10,7 +10,7 @@ | |
* @author Vladimir Ivanov ([email protected]) | ||
*/ | ||
@Service | ||
@WithOutCache | ||
@WithCache | ||
public class SimplePrimeService implements PrimeService { | ||
/** | ||
* Check a number is prime. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters