Maven、SSM整合Redis
Maven和SSM的搭建这里就不说了,主要写Maven、SSM如何整合Redis。
整体的项目框架图:
目录
1.5 新增拦截器MethodCacheInterceptor
1.1 添加jar包
在pom.xml文件中添加Redis所需的jar包
注意,本框架使用的是spring 4.3.14.RELEASE版本,redis 2.9.0版本的,spring-redis 1.8.10.RELEASE版本的,一定要看好版本,有的版本是不支持的。
另外本框架使用的是spring面向切面引入的Redis。
<!-- spring版本号 --> <spring.version>4.3.14.RELEASE</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.2.6</mybatis.version> |
<!-- redis客户端jar --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <!-- spring-redis实现 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.10.RELEASE</version> </dependency> |
<!-- spring 切面插件 start --> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.5.4</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency> <!-- spring 切面插件 end--> |
1.2 新增一个redis.properties文件
此文件用来配置Redis。
# Redis settings redis.host=127.0.0.1 redis.port=6379 redis.pass= redis.maxIdle=400 redis.maxTotal=6000 redis.maxWaitMillis=1000 redis.blockWhenExhausted=true redis.testOnBorrow=true redis.timeout=100000 defaultCacheExpireTime=3600 |
1.3 配置一个spring-redis.xml文件
此文件用来Spring管理Redis。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- redis数据源 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 最大空闲数 --> <property name="maxIdle" value="${redis.maxIdle}" /> <!-- 最大空连接数 --> <property name="maxTotal" value="${redis.maxTotal}" /> <!-- 最大等待时间 --> <property name="maxWaitMillis" value="${redis.maxWaitMillis}" /> <!-- 连接超时时是否阻塞,false时报异常,ture阻塞直到超时, 默认true --> <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}" /> <!-- 返回连接时,检测连接是否成功 --> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <!-- Spring-redis连接池管理工厂 --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:hostName="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:timeout="${redis.timeout}" p:poolConfig-ref="poolConfig" p:usePool="true"> </bean> <!-- redis template definition --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <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> <!--开启事务 --> <property name="enableTransactionSupport" value="true"></property> </bean> <!-- 缓存拦截器配置 --> <bean id="methodCacheInterceptor" class="com.ivan.redis.MethodCacheInterceptor"> <property name="redisUtil" ref="redisUtil" /> <property name="defaultCacheExpireTime" value="${defaultCacheExpireTime}" /> <!-- 禁用缓存的类名列表 --> <property name="targetNamesList"> <list> <value></value> </list> </property> <!-- 禁用缓存的方法名列表 --> <property name="methodNamesList"> <list> <value>add</value> <value>delete</value> <value>edit</value> </list> </property> </bean> <bean id="redisUtil" class="com.ivan.redis.RedisUtil"> <property name="redisTemplate" ref="redisTemplate" /> </bean> <!--配置切面拦截方法 --> <aop:config proxy-target-class="true"> <!-- <aop:pointcut id="controllerMethodPointcut" expression="execution(* com.ivan.service.impl.*.get*(..))" /> --> <aop:pointcut id="controllerMethodPointcut" expression="execution(* com.ivan.service.impl.*.*(..))" /> <aop:advisor advice-ref="methodCacheInterceptor" pointcut-ref="controllerMethodPointcut" /> </aop:config> </beans> |
1.4 配置spring-mybatis.xml
在spring-mybatis.xml中首先引入redis.properties,然后再引入spring-redis.xml。
<!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> <value>classpath:redis.properties</value> </list> </property> <!--这个配置告诉spring,当某个placeholder无法找到时, 先不要报错,并尝试用另一个PropertyPlaceholderConfigurer来设置placeholder的值。 --> <!-- <property name="ignoreUnresolvablePlaceholders" value="true"></property> --> </bean> |
<!-- redis配置 --> <import resource="spring-redis.xml" /> |
1.5 新增拦截器MethodCacheInterceptor
package com.ivan.redis; import com.ivan.redis.RedisUtil; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import java.util.List; //Redis缓存过滤器 public class MethodCacheInterceptor implements MethodInterceptor { private RedisUtil redisUtil; private List<String> targetNamesList; // 禁用缓存的类名列表 private List<String> methodNamesList; // 禁用缓存的方法列表 private String defaultCacheExpireTime; // 缓存默认的过期时间 @Override public Object invoke(MethodInvocation invocation) throws Throwable { Object value = null; String targetName = invocation.getThis().getClass().getName(); String methodName = invocation.getMethod().getName(); if (!isAddCache(targetName, methodName)) { // 跳过缓存返回结果 return invocation.proceed(); } Object[] arguments = invocation.getArguments(); String key = getCacheKey(targetName, methodName, arguments); try { // 判断是否有缓存 if (redisUtil.exists(key)) return redisUtil.get(key); // 写入缓存 // 不存在执行数据库操作 value = invocation.proceed(); if (value != null) { final String tkey = key; final Object tvalue = value; new Thread(new Runnable() { @Override public void run() { // 将查询的对象存入redis缓存 redisUtil.set(tkey, tvalue, Long.parseLong(defaultCacheExpireTime)); } }).start(); } } catch (Exception e) { e.printStackTrace(); if (value == null) return invocation.proceed(); } return value; } // 是否加入缓存 private boolean isAddCache(String targetName, String methodName) { if (targetNamesList.contains(targetName) || targetName.contains("$$EnhancerBySpringCGLIB$$")) { redisUtil.deleteKeys("*"); return false; } for (String methName : methodNamesList) { if (methodName.contains(methName)) { redisUtil.deleteKeys("*"); return false; } } return true; } // 创建缓存key private String getCacheKey(String targetName, String methodName, Object[] arguments) { StringBuffer sbu = new StringBuffer(); sbu.append(targetName).append("_").append(methodName); if ((arguments != null) && (arguments.length != 0)) { for (int i = 0; i < arguments.length; i++) { sbu.append("_").append(arguments[i]); } } return sbu.toString(); } public void setRedisUtil(RedisUtil redisUtil) { this.redisUtil = redisUtil; } public void setTargetNamesList(List<String> targetNamesList) { this.targetNamesList = targetNamesList; } public void setMethodNamesList(List<String> methodNamesList) { this.methodNamesList = methodNamesList; } public void setDefaultCacheExpireTime(String defaultCacheExpireTime) { this.defaultCacheExpireTime = defaultCacheExpireTime; } } |
1.6 新增Redis操作类RedisUtil
package com.ivan.redis; import java.io.Serializable; import java.util.Set; import java.util.concurrent.TimeUnit; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; //Redis工具类 public class RedisUtil { private RedisTemplate<Serializable, Object> redisTemplate; // 批量删除对应的value public void remove(final String... keys) { for (String key : keys) remove(key); } // 批量删除key public void removePattern(final String pattern) { Set<Serializable> keys = redisTemplate.keys(pattern); if (keys.size() > 0) redisTemplate.delete(keys); } // 删除对应的value public void remove(final String key) { if (exists(key)) redisTemplate.delete(key); } // 删除符合要求的key by ivan public void deleteKeys(final String key) { redisTemplate.delete(redisTemplate.keys(key)); } // 判断缓存中是否有对应的value public boolean exists(final String key) { return redisTemplate.hasKey(key); } // 读取缓存 public Object get(final String key) { Object result = null; ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); result = operations.get(key); return result; } // 写入缓存 public boolean set(final String key, Object value) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } // 写入缓存 public boolean set(final String key, Object value, Long expireTime) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public void setRedisTemplate(RedisTemplate<Serializable, Object> redisTemplate) { this.redisTemplate = redisTemplate; } } |
1.7 实体类必须序列化
package com.ivan.pojo; import java.io.Serializable; //要想使用redis存对象,一定要让实体类实现Serializable接口,否则会报错 public class PojoParent implements Serializable{ private static final long serialVersionUID = 1L; } |
1.8 Mybatis必须开启缓存机制,非必须,只有配合用cache缓存的时候用
在mybatis-config.xml中添加如下语句:
<settings> <!-- 二级缓存开启 --> <setting name="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="false"/> <setting name="aggressiveLazyLoading" value="true"/> </settings> |
1.8 完整的项目打包请去我的博客地址下载:
https://download.csdn.net/download/hello_me_you/10759565