赞
踩
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId><!-- redis -->
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-aop</artifactId><!-- spring aop -->
- </dependency>
- redis:
- database: 0
- host: localhost
- port: 6379
- password:
- jedis:
- pool:
- max-active: 200
- max-wait: -1
- max-idle: 10
- min-idle: 0
- timeout: 1000
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * redis配置类 * * @author chuhongyun */ @Configuration public class RedisConfig { @Bean @SuppressWarnings("all") public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // key采用String的序列化方式 template.setKeySerializer(stringRedisSerializer); // hash的key也采用String的序列化方式 template.setHashKeySerializer(stringRedisSerializer); // value序列化方式采用jackson template.setValueSerializer(jackson2JsonRedisSerializer); // hash的value序列化方式采用jackson template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } }
import java.lang.annotation.*; /** * redis 缓存接口 * * @author chuhongyun */ @Documented @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface RedisCache { /** * 键 * @return */ String key() default ""; /** * 过期时间 * @return */ long expired() default -1; /** * 是否为查询操作 * 如果为写入数据库的操作,该值需置为 false * @return */ boolean read() default true; }
import com.alibaba.fastjson.JSON; import com.zhuyou.common.encryption.ShaUtil; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; /** * redis 切面程序 * * @author chuhongyun */ @Component @Aspect public class RedisCacheAspect { private final static Logger logger = LoggerFactory.getLogger(RedisCacheAspect.class); @Autowired private RedisTemplate redisTemplate; /** * 定义切入点,使用了 @RedisCache 的方法 */ @Pointcut("@annotation(RedisCache)") public void redisServicePoint() { } /** * 环绕通知,方法拦截器 */ @Around("redisServicePoint()")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。