准备工作:
安装redis,我是在windows 64位机器上安装的redis,直接下载
Redis-x64-3.2.100.zip解压,
cmd cd到解压后目录运行如下命令:
redis-server.exe reids.windows.conf即可启动redis。
1、添加maven支持,在pom.xml中添加如下
<!-- redis cache related.....start -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.3.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
<!-- redis cache related.....end -->
2、配置redis连接,在applicationContext.xml中添加如下:
<!-- 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.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:pool-config-ref="poolConfig" />
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory" />
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
在程序中直接加载redisTemplate或者stringRedisTemplate就可以使用redis了,这里配置了两个template,实际上使用一个就可以,stringRedisTemplate只是默认采用redis的StringRedisSerializer来进行数据的序列化而已。
3、在程序中使用template进行数据的读写
package com.netease.lede.redis.demo;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author 张啸雷 E-mail:bjzhangxiaolei1@corp.netease.com
* @version 创建时间:2016年9月27日 下午12:04:40
* 类说明
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class AppTest {
@Autowired
RedisTemplate redisTemplate;
@Autowired
StringRedisTemplate stringRedisTemplate;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void test1() {
String key = "spring";
ListOperations<String, String> lop = redisTemplate.opsForList();
RedisSerializer<String> serializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(serializer);
redisTemplate.setValueSerializer(serializer);
// rt.setDefaultSerializer(serializer);
lop.leftPush(key, "aaa");
lop.leftPush(key, "bbb");
long size = lop.size(key); // rt.boundListOps(key).size();
assertThat(size, equalTo(6L));
}
// 测试便捷对象StringRedisTemplate
@Ignore
@Test
public void test5() {
ValueOperations<String, String> vop = stringRedisTemplate.opsForValue();
String key = "string_redis_template";
String v = "use StringRedisTemplate set k v";
vop.set(key, v);
String value = vop.get(key);
assertThat(v, equalTo(value));
}
}
分别测试了使用两种template进行数据读写。
使用redis存储对象
很多时候我们还是更倾向于直接存储java对象到redis中,那么这时候应该怎么做呢?
其实使用redisTemplate就非常简单。
首先可以在applicationContext.xml中配置序列化选项。
由于redisTemplate是泛型的,所以可以定义自己的类进去。
@Autowired
RedisTemplate<String,BaseResponse> redisTemplate;
其中BaseResponse就是自己的类,String是hashkey值
@Autowired
RedisTemplate<String,BaseResponse> redisTemplate;
BaseResponse reponse=null;
HashOperations<String,String, BaseResponse> hop=redisTemplate.opsForHash();//获取一个hash操作
redisTemplate.expire(redisKeyName, redisExpire, TimeUnit.MINUTES);//设置超时时间
reponse=hop.get(redisKeyName, query.getRedisKey());
hop.put(redisKeyName, query.getRedisKey(), reponse);
关于超时有一点想法:
1、使用redisTemplate.expire(redisKeyName, redisExpire, TimeUnit.MINUTES)进行超时设置
set和get方法都会使超时计时重置,也就是说这种超时的含义是在reids中的该值完全没有被使用多久后即失效。
2、如果要使redis中的值在某一段时间之后失效,则需要使用Date expireDate=new Date(System.currentTimeMillis()+redisExpire*60*1000);表示固定的在1分钟之后失效