maven使用:
<!--redis jar包--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.6.2.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-ehcache</artifactId> <version>1.0.0</version> </dependency>
1:redis的连接信息
#访问地址 redis.host=127.0.0.1 #访问端口 redis.port=6379 #注意,如果没有password,此处不设置值,但这一项要保留 redis.password= #最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。 redis.maxIdle=300 #连接池的最大数据库连接数。设为0表示无限制 redis.maxActive=600 #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。 redis.maxWait=1000 #在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的; redis.testOnBorrow=true
2:spring和redis的整合
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:util="http://www.springframework.org/schema/util" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:context="http://www.springframework.org/schema/context" 8 xmlns:task="http://www.springframework.org/schema/task" 9 xsi:schemaLocation="http://www.springframework.org/schema/beans 10 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 11 http://www.springframework.org/schema/util 12 http://www.springframework.org/schema/util/spring-util-4.3.xsd 13 http://www.springframework.org/schema/mvc 14 http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 15 http://www.springframework.org/schema/aop 16 http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 17 http://www.springframework.org/schema/context 18 http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 19 20 21 <!-- 引入properties配置文件 --> 22 <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/> 23 24 <!-- redis连接池 --> 25 <bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig"> 26 <property name="maxTotal" value="${redis.maxActive}"></property> 27 <property name="maxIdle" value="${redis.maxIdle}"></property> 28 <property name="maxWaitMillis" value="${redis.maxWait}"></property> 29 <property name="testOnBorrow" value="${redis.testOnBorrow}"></property> 30 </bean> 31 32 <!-- redis连接工厂 --> 33 <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> 34 <property name="hostName" value="${redis.host}"></property> 35 <property name="port" value="${redis.port}"></property> 36 <property name="password" value="${redis.password}"></property> 37 <property name="poolConfig" ref="jedisConfig"></property> 38 </bean> 39 <!-- redis操作模板,这里采用尽量面向对象的模板 --> 40 <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> 41 <property name="connectionFactory" ref="connectionFactory"/> 42 <!-- 如果不配置Serializer,那么存储的时候只能使用String,如果用对象类型存储,那么会提示错误 can't cast to String!!!--> 43 <property name="keySerializer"> 44 <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> 45 </property> 46 <property name="valueSerializer"> 47 <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> 48 </property> 49 <!--开启事务--> 50 <property name="enableTransactionSupport" value="true"/> 51 </bean> 52 </beans>
3:在业务代码中使用
1 /** 2 * 使用redis的缓存机制 3 */ 4 @Autowired 5 private RedisTemplate redisTemplate; 6 7 /** 8 * 分页查询所有 9 10 * @return 11 */ 12 @Override 13 public List<Goods> listGoods() { 14 List<Goods> goods = goodsDao.listGoods(); 15 for(Goods goods1:goods){ 16 //把每一个goods对象保存到redis中的List中。 17 redisTemplate.opsForList().rightPush("goodsList", goods1); 18 } 19 return goods; 20 }
4:测试
@Test public void testListGoods(){ List<Goods> goods = redisTemplate.opsForList().range( "goodsList", 0, -1); if(goods.size()==0){ List<Goods> goods1 = goodsService.listGoods(); for (Goods goods2:goods1){ System.out.println(goods2); } }else { // System.out.println(goods); for(Goods goods1:goods){ System.out.println(goods1); } } }
可以看到已经保存到redis的数据库中了。
5:编写一个RedisTemplate的公用类
package com.betteryanwo.util; import org.springframework.data.redis.core.*; import java.util.List; import java.util.Map; import java.util.Set; /** * Create by 六 * Date:18-7-16 * RedisTemplate的公用类 */ public class RedisTemplateUtil { private RedisTemplate redisTemplate; public RedisTemplateUtil(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } public void set(String key, Object value) { ValueOperations valueOperations = redisTemplate.opsForValue(); valueOperations.set(key, value); //BoundValueOperations的理解对保存的值做一些细微的操作 // BoundValueOperations boundValueOperations = redisTemplate.boundValueOps(key); } public Object get(String key) { return redisTemplate.opsForValue().get(key); } public void setList(String key, List<?> value) { //Operation 操作。 ListOperations listOperations = redisTemplate.opsForList(); listOperations.leftPush(key, value); } public Object getList(String key) { return redisTemplate.opsForList().leftPop(key); } public void setSet(String key, Set<?> value) { SetOperations setOperations = redisTemplate.opsForSet(); setOperations.add(key, value); } public Object getSet(String key) { return redisTemplate.opsForSet().members(key); } public void setHash(String key, Map<String, ?> value) { HashOperations hashOperations = redisTemplate.opsForHash(); hashOperations.putAll(key, value); } public Object getHash(String key) { return redisTemplate.opsForHash().entries(key); } public void delete(String key) { redisTemplate.delete(key); } public void clearAll() { redisTemplate.multi(); } }