当前位置:   article > 正文

springboot整合redis,spring aop实现接口缓存_springboot redis二级缓存 aop实现

springboot redis二级缓存 aop实现

pox.xml:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId><!-- redis -->
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-aop</artifactId><!-- spring aop -->
  8. </dependency>

application.yml:

  1. redis:
  2. database: 0
  3. host: localhost
  4. port: 6379
  5. password:
  6. jedis:
  7. pool:
  8. max-active: 200
  9. max-wait: -1
  10. max-idle: 10
  11. min-idle: 0
  12. timeout: 1000

redis配置类:RedisConfig.java​​​​

  1. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  2. import com.fasterxml.jackson.annotation.PropertyAccessor;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.data.redis.connection.RedisConnectionFactory;
  7. import org.springframework.data.redis.core.RedisTemplate;
  8. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  9. import org.springframework.data.redis.serializer.StringRedisSerializer;
  10. /**
  11. * redis配置类
  12. *
  13. * @author chuhongyun
  14. */
  15. @Configuration
  16. public class RedisConfig {
  17. @Bean
  18. @SuppressWarnings("all")
  19. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
  20. RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
  21. template.setConnectionFactory(factory);
  22. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
  23. ObjectMapper om = new ObjectMapper();
  24. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  25. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  26. jackson2JsonRedisSerializer.setObjectMapper(om);
  27. StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
  28. // key采用String的序列化方式
  29. template.setKeySerializer(stringRedisSerializer);
  30. // hash的key也采用String的序列化方式
  31. template.setHashKeySerializer(stringRedisSerializer);
  32. // value序列化方式采用jackson
  33. template.setValueSerializer(jackson2JsonRedisSerializer);
  34. // hash的value序列化方式采用jackson
  35. template.setHashValueSerializer(jackson2JsonRedisSerializer);
  36. template.afterPropertiesSet();
  37. return template;
  38. }
  39. }

缓存接口:RedisCache.java

  1. import java.lang.annotation.*;
  2. /**
  3. * redis 缓存接口
  4. *
  5. * @author chuhongyun
  6. */
  7. @Documented
  8. @Target(ElementType.METHOD)
  9. @Retention(RetentionPolicy.RUNTIME)
  10. public @interface RedisCache {
  11. /**
  12. * 键
  13. * @return
  14. */
  15. String key() default "";
  16. /**
  17. * 过期时间
  18. * @return
  19. */
  20. long expired() default -1;
  21. /**
  22. * 是否为查询操作
  23. * 如果为写入数据库的操作,该值需置为 false
  24. * @return
  25. */
  26. boolean read() default true;
  27. }

切面程序:RedisCacheAspect.java

  1. import com.alibaba.fastjson.JSON;
  2. import com.zhuyou.common.encryption.ShaUtil;
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4. import org.aspectj.lang.annotation.Around;
  5. import org.aspectj.lang.annotation.Aspect;
  6. import org.aspectj.lang.annotation.Pointcut;
  7. import org.aspectj.lang.reflect.MethodSignature;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.data.redis.core.RedisTemplate;
  12. import org.springframework.stereotype.Component;
  13. import java.util.concurrent.TimeUnit;
  14. /**
  15. * redis 切面程序
  16. *
  17. * @author chuhongyun
  18. */
  19. @Component
  20. @Aspect
  21. public class RedisCacheAspect {
  22. private final static Logger logger = LoggerFactory.getLogger(RedisCacheAspect.class);
  23. @Autowired
  24. private RedisTemplate redisTemplate;
  25. /**
  26. * 定义切入点,使用了 @RedisCache 的方法
  27. */
  28. @Pointcut("@annotation(RedisCache)")
  29. public void redisServicePoint() {
  30. }
  31. /**
  32. * 环绕通知,方法拦截器
  33. */
  34. @Around("redisServicePoint()")
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号