PageRenderTime 49ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/jeecgframework/core/aop/EhcacheAspect.java

https://gitlab.com/spiderworts/ab_weixin_cms
Java | 119 lines | 89 code | 15 blank | 15 comment | 22 complexity | 126c49bda8be1f0009d0886094bb2cdd MD5 | raw file
  1. package org.jeecgframework.core.aop;
  2. import java.io.Serializable;
  3. import java.lang.reflect.Method;
  4. import net.sf.ehcache.Cache;
  5. import net.sf.ehcache.CacheManager;
  6. import net.sf.ehcache.Element;
  7. import org.aspectj.lang.ProceedingJoinPoint;
  8. import org.aspectj.lang.annotation.AfterReturning;
  9. import org.aspectj.lang.annotation.Around;
  10. import org.aspectj.lang.annotation.Aspect;
  11. import org.aspectj.lang.annotation.Pointcut;
  12. import org.jeecgframework.core.annotation.Ehcache;
  13. import org.springframework.stereotype.Component;
  14. import com.alibaba.fastjson.JSON;
  15. /**
  16. *
  17. * @author 张代浩
  18. *
  19. */
  20. @Component
  21. @Aspect
  22. public class EhcacheAspect {
  23. private static Cache dictCache;
  24. private static Cache eternalCache;
  25. static {
  26. if (eternalCache == null) {
  27. eternalCache = CacheManager.getInstance().getCache("eternalCache");
  28. }
  29. if (dictCache == null) {
  30. dictCache = CacheManager.getInstance().getCache("dictCache");
  31. }
  32. }
  33. @Pointcut("@annotation(org.jeecgframework.core.annotation.Ehcache)")
  34. public void simplePointcut() {
  35. }
  36. @AfterReturning(pointcut = "simplePointcut()")
  37. public void simpleAdvice() {
  38. }
  39. @Around("simplePointcut()")
  40. public Object aroundLogCalls(ProceedingJoinPoint joinPoint)
  41. throws Throwable {
  42. String targetName = joinPoint.getTarget().getClass().toString();
  43. String methodName = joinPoint.getSignature().getName();
  44. Object[] arguments = joinPoint.getArgs();
  45. //试图得到标注的Ehcache类
  46. @SuppressWarnings("unused")
  47. Method[] methods = joinPoint.getTarget().getClass().getMethods();
  48. Ehcache flag = null;
  49. for(Method m:methods){
  50. if(m.getName().equals(methodName)){
  51. Class[] tmpCs = m.getParameterTypes();
  52. if(tmpCs.length==arguments.length){
  53. flag = m.getAnnotation(Ehcache.class);
  54. break;
  55. }
  56. }
  57. }
  58. if(flag==null){
  59. return null;
  60. }
  61. //Ehcache flag =joinPoint.getTarget().getClass().getMethod(methodName).getAnnotation(Ehcache.class);
  62. Object result;
  63. String cacheKey = getCacheKey(targetName, methodName, arguments);
  64. Element element = null;
  65. if(flag.eternal()){
  66. //永久缓存
  67. element = dictCache.get(cacheKey);
  68. }else{
  69. //临时缓存
  70. element = eternalCache.get(cacheKey);
  71. }
  72. if (element == null) {
  73. if ((arguments != null) && (arguments.length != 0)) {
  74. result = joinPoint.proceed(arguments);
  75. } else {
  76. result = joinPoint.proceed();
  77. }
  78. element = new Element(cacheKey, (Serializable) result);
  79. if(flag.eternal()){
  80. //永久缓存
  81. dictCache.put(element);
  82. }else{
  83. //临时缓存
  84. eternalCache.put(element);
  85. }
  86. }
  87. return element.getValue();
  88. }
  89. /**
  90. * 获得cache key的方法,cache key是Cache中一个Element的唯一标识 cache key包括
  91. * 包名+类名+方法名,如com.co.cache.service.UserServiceImpl.getAllUser
  92. */
  93. private String getCacheKey(String targetName, String methodName,
  94. Object[] arguments) {
  95. StringBuffer sb = new StringBuffer();
  96. sb.append(targetName).append(".").append(methodName);
  97. if ((arguments != null) && (arguments.length != 0)) {
  98. for (int i = 0; i < arguments.length; i++) {
  99. sb.append(".").append(JSON.toJSONString(arguments[i]));
  100. }
  101. }
  102. return sb.toString();
  103. }
  104. }