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的整合
<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:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 引入properties配置文件 -->
<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/> <!-- redis连接池 -->
<bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxActive}"></property>
<property name="maxIdle" value="${redis.maxIdle}"></property>
<property name="maxWaitMillis" value="${redis.maxWait}"></property>
<property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
</bean> <!-- redis连接工厂 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}"></property>
<property name="port" value="${redis.port}"></property>
<property name="password" value="${redis.password}"></property>
<property name="poolConfig" ref="jedisConfig"></property>
</bean>
<!-- redis操作模板,这里采用尽量面向对象的模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<!-- 如果不配置Serializer,那么存储的时候只能使用String,如果用对象类型存储,那么会提示错误 can't cast to String!!!-->
<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="enableTransactionSupport" value="true"/>
</bean>
</beans>
3:在业务代码中使用
/**
* 使用redis的缓存机制
*/
@Autowired
private RedisTemplate redisTemplate; /**
* 分页查询所有 * @return
*/
@Override
public List<Goods> listGoods() {
List<Goods> goods = goodsDao.listGoods();
for(Goods goods1:goods){
//把每一个goods对象保存到redis中的List中。
redisTemplate.opsForList().rightPush("goodsList", goods1);
}
return goods;
}
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();
}
}