一、pom文件引入redis依赖
<!-- redis 版本 --> <redis.version>2.9.0</redis.version> <spring.redis.version>1.8.1.RELEASE</spring.redis.version>
<!-- redis --> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${redis.version}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>${spring.redis.version}</version> </dependency> </dependencies>
二、redis的可配置话参数
redis.properties
redis.maxTotal=10 redis.maxIdle=5 redis.maxWaitMillis=2000 redis.testOnBorrow=true redis.host=127.0.0.1 redis.port=6379 redis.timeout=0 redis.password=han.sun redis.testWhileIdle=true redis.timeBetweenEvictionRunsMillis=30000 redis.numTestsPerEvictionRun=50
三、spring和redis整合
spring-redis.xm
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.eastrobot.robotdev"/> <!-- 加载配置文件 --> <bean id="annotationPropertyConfigurerRedis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="order" value="1"/> <property name="ignoreUnresolvablePlaceholders" value="true"/> <property name="locations"> <list> <value>classpath:redis.properties</value> </list> </property> </bean> <!-- 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}"/> <!-- 返回连接时,检测连接是否成功 --> <property name="testOnBorrow" value="${redis.testOnBorrow}"/> </bean> <!-- Spring-redis连接池管理工厂 --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <!-- IP地址 --> <property name="hostName" value="${redis.host}"/> <!-- 端口号 --> <property name="port" value="${redis.port}"/> <property name="password" value="${redis.password}"/> <!-- 超时时间 默认2000--> <property name="timeout" value="${redis.timeout}"/> <!-- 连接池配置引用 --> <property name="poolConfig" ref="poolConfig"/> <!-- usePool:是否使用连接池 --> <property name="usePool" value="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> <!-- 如果想设置value为对象的话,这里不能配置成StringRedisSerializer --> <!-- 否则会报错 java.lang.ClassCastException: com.eastrobot.robotdev.entity.User cannot be cast to java.lang.String --> <!-- 而是要用JdkSerializationRedisSerializer,然后javabean实现Serializable接口 --> <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.StringRedisSerializer"/> </property> <!--开启事务 --> <property name="enableTransactionSupport" value="true"/> </bean> </beans>
四、测试
private static final Logger log = LogManager.getLogger(RedisServiceTest.class); @Autowired private RedisService redisService; @Autowired private RedisTemplate redisTemplate; @Test public void getValue() { Object name = redisService.get("name"); log.info("name:{}", name); Object password = redisService.get("password"); log.info("name:{}", password); } /** * 备注,redisTemplate不可以直接操作对象实体, * 解决方案: * 1、hash类型 * 2、转json存储、与反序列化 */ @Test public void testObject() { // 直接javabean储存 // 底层会进行检查Serializable接口,进行序列化、反序列化 User user = new User("孙寒", "sunhan240", 24); ValueOperations ops = redisTemplate.opsForValue(); ops.set("sunhan", user); Object sunhan = ops.get("sunhan"); log.info(sunhan.toString()); // 使用JSON格式存储、获取 if (redisService.set("sunhan", user.toString())) { log.info("存储user成功!"); } Object o = redisService.get("sunhan"); User user1 = JSON.parseObject(o.toString(), User.class); log.info("user:{}", user1.toString()); // hash类型 redisService.hset("sunhan1", "username", user.getUsername()); redisService.hset("sunhan1", "password", user.getPassword()); redisService.hset("sunhan1", "age", user.getAge() + ""); Object username = redisService.hget("sunhan1", "username"); Object password = redisService.hget("sunhan1", "password"); Object age = redisService.hget("sunhan1", "age"); log.info("username:{} | password:{} | age:{}", username, password, age); }