标签:
java使用redis缓存可以使用jedis框架,jedis操作简单,没有什么复杂的东西需要学习,网上资料很多,随便看看就会了.
将spring与redis缓存集成,其实也是使用jedis框架,只不过spring对它进行了一层封装,并将这层封装库命名为spring-data-redis.
下面将要使用spring-data-redis与jedis的jar包,并通过spring的aop功能,将redis缓存无缝无侵入的整合进来.
1.先下载好依赖包
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>4.1.1.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- <version>1.4.1.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.6.0</version>
- </dependency>
2.再配置spring文件
- <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
- <property name="minIdle" value="${redis.minIdle}" />
- <property name="maxIdle" value="${redis.maxIdle}" />
- <property name="maxTotal" value="${redis.maxActive}" />
- <property name="maxWaitMillis" value="${redis.maxWait}" />
- <property name="testOnBorrow" value="${redis.testOnBorrow}" />
- </bean>
-
- <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
- <property name="hostName" value="${redis.host}" />
- <property name="port" value="${redis.port}" />
- <property name="password" value="${redis.password}" />
- <property name="usePool" value="true" />
- <property name="poolConfig" ref="poolConfig" />
- </bean>
-
- <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
- <property name="connectionFactory" ref="jedisConnFactory" />
- <property name="keySerializer">
- <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
- </property>
- <property name="valueSerializer">
- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
- </property>
- <property name="hashKeySerializer">
- <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
- </property>
- <property name="hashValueSerializer">
- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
- </property>
- </bean>
3.开始编写aop代码
3.1 声明两个注解类,用于定义哪些方法将使用缓存
- @Retention(RetentionPolicy.RUNTIME)
- @Target({ElementType.METHOD})
- public @interface Cacheable {
-
- public enum KeyMode{
- DEFAULT,
- BASIC,
- ALL;
- }
-
- public String key() default "";
- public KeyMode keyMode() default KeyMode.DEFAULT;
- public int expire() default 0;
- }
- @Retention(RetentionPolicy.RUNTIME)
- @Target({ElementType.PARAMETER})
- public @interface CacheKey {}
3.2 创建一个Aop拦截器的处理类,用于拦截加了@Cacheable的方法
- @Aspect
- @Component
- public class CacheableAop {
-
- @Autowired private RedisTemplate redisTemplate;
-
- @Around("@annotation(cache)")
- public Object cached(final ProceedingJoinPoint pjp,Cacheable cache) throws Throwable {
-
- String key=getCacheKey(pjp, cache);
- ValueOperations<String, Object> valueOper=redisTemplate.opsForValue();
- Object value=valueOper.get(key);
- if(value!=null) return value;
-
- value = pjp.proceed();
- if(cache.expire()<=0) {
- valueOper.set(key, value);
- } else {
- valueOper.set(key, value,cache.expire(),TimeUnit.SECONDS);
- }
- return value;
- }
-
-
- private String getCacheKey(ProceedingJoinPoint pjp,Cacheable cache) {
-
- StringBuilder buf=new StringBuilder();
- buf.append(pjp.getSignature().getDeclaringTypeName()).append(".").append(pjp.getSignature().getName());
- if(cache.key().length()>0) {
- buf.append(".").append(cache.key());
- }
-
- Object[] args=pjp.getArgs();
- if(cache.keyMode()==KeyMode.DEFAULT) {
- Annotation[][] pas=((MethodSignature)pjp.getSignature()).getMethod().getParameterAnnotations();
- for(int i=0;i<pas.length;i++) {
- for(Annotation an:pas[i]) {
- if(an instanceof CacheKey) {
- buf.append(".").append(args[i].toString());
- break;
- }
- }
- }
- } else if(cache.keyMode()==KeyMode.BASIC) {
- for(Object arg:args) {
- if(arg instanceof String) {
- buf.append(".").append(arg);
- } else if(arg instanceof Integer || arg instanceof Long || arg instanceof Short) {
- buf.append(".").append(arg.toString());
- } else if(arg instanceof Boolean) {
- buf.append(".").append(arg.toString());
- }
- }
- } else if(cache.keyMode()==KeyMode.ALL) {
- for(Object arg:args) {
- buf.append(".").append(arg.toString());
- }
- }
-
- return buf.toString();
- }
- }
4.使用缓存示例
- @Service
- @Transactional
- public class DemoServiceImpl implements DemoService {
-
- @Autowired private DemoDao demoDao;
-
- public List<Demo> findAll() {
- return demoDao.findAll();
- }
-
-
- @Cacheable(expire=3600)
- public Demo get(@CacheKey String id) {
- return demoDao.get(id);
- }
-
- public Demo getByName(String name) {
- return demoDao.getByName(name);
- }
- }
- 若为名称相同的方法配置缓存,可以在@Cacheable中加入key属性,追加额外的key后缀
- @Cacheable还有一个KeyMode属性,用于配置哪些参数可以追加到key后缀中,
默认取值 DEFAULT:表示只有加了@CacheKey的参数才能追加到key后缀
BASIC:自动将基本类型追加到key后缀,而无需再配置@CacheKey
ALL:自动将所有参数追加到lkey后缀,而无需再配置@CacheKey
这只是一个初步整合方案,测试可行,还未在生产中使用,实际效果待验正.
spring与redis集成之aop整合方案
标签:
原文地址:http://www.cnblogs.com/lijc1990/p/4250915.html