1.maven依赖导入
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
2.spring配置文件的配置
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.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/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- jedis 连接池配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="300" />
<property name="maxWaitMillis" value="3000" />
<property name="testOnBorrow" value="true" />
</bean>
<!-- jedis 连接工厂 -->
<bean id="redisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="192.168.25.128" p:port="6379" p:pool-config-ref="poolConfig"
p:database="0" />
<!-- spring data 提供 redis模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory" />
<!-- 如果不指定 Serializer -->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer">
</bean>
</property>
</bean>
</beans>
keySerializer和valueSerializer,hashKeySerializer,hashValueSerializer参考:
https://www.cnblogs.com/yueguanguanyun/p/8283127.html
3.代码中的使用
@Autowired
private RedisTemplate<String, String> redisTemplate;
// 生成激活码并将激活码放入缓存,有效期24小时
final String activeCode = RandomStringUtils.randomNumeric(32);
redisTemplate.opsForValue().set(entity.getTelephone(), activeCode, 24, TimeUnit.HOURS);
4.常用的api
tedisTemplate 集合了,所有数据类型的操作,先通过绑定类型 key,然后再进行操作:
template.boundValueOps(stringKey);
template.boundHashOps(key);
template.boundListOps(key);
template.boundSetOps(key);
template.boundZSetOps(key);
示例:
1. template.boundValueOps(stringKey).set("hello", 10,TimeUnit.SECONDS);
2. String temp = template.boundValueOps(stringKey).get();
3. template.boundListOps(listKey).leftPush("list1");
4. template.boundListOps(listKey).leftPop()
1.操作简单数据
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
public void setValue() throws Exception {
redisTemplate.boundValueOps("name").set("zhangsan");
}
@Test
public void getValue() throws Exception {
String value = redisTemplate.boundValueOps("name").get();
System.out.println(value);
}
@Test
public void delKey() throws Exception {
redisTemplate.delete("name");
}
@Test
public void setValueWithTimeout() throws Exception {
redisTemplate.opsForValue().set("name", "zhangsan", 2, TimeUnit.MINUTES);
;
}
2.操作set
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
public void setValue() throws Exception {
redisTemplate.boundSetOps("name").add("张飞");
redisTemplate.boundSetOps("name").add("刘备");
redisTemplate.boundSetOps("name").add("关羽");
}
@Test
public void getValue() throws Exception {
Set<String> set = redisTemplate.boundSetOps("name").members();
System.out.println(set);
}
@Test
public void delValue() throws Exception {
redisTemplate.boundSetOps("name").remove("张飞");
}
@Test
public void delKey() throws Exception {
redisTemplate.delete("name");
}
3.操作Map
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
public void setValue() throws Exception {
redisTemplate.boundHashOps("namehash").put("a", "张三");
redisTemplate.boundHashOps("namehash").put("b", "李四");
redisTemplate.boundHashOps("namehash").put("c", "王五");
redisTemplate.boundHashOps("namehash").put("d", "赵六");
redisTemplate.expire("namehash", 2, TimeUnit.MINUTES);
}
@Test
public void getKeys() {
Set<Object> keys = redisTemplate.boundHashOps("namehash").keys();
System.out.println(keys);
}
@Test
public void getValus() {
List<Object> values = redisTemplate.boundHashOps("namehash").values();
System.out.println(values);
}
@Test
public void searchByKey() {
Object object = redisTemplate.boundHashOps("namehash").get("a");
System.out.println(object);
}
@Test
public void delValue() throws Exception {
redisTemplate.boundHashOps("namehash").delete("a");
}
@Test
public void delKey() throws Exception {
redisTemplate.delete("namehash");
}
@Test
public void setValueWithTimeOut() throws Exception {
redisTemplate.opsForHash().put("namehash", "a", "张三");
redisTemplate.expire("namehash", 2, TimeUnit.MINUTES);
}
4.操作List
@Autowired
private RedisTemplate redisTemplate;
/*
* 右压栈 : 后添加的元素排在后边
*/
@Test
public void testSetValue1(){
redisTemplate.boundListOps("namelist1").rightPush("刘备");
redisTemplate.boundListOps("namelist1").rightPush("关羽");
redisTemplate.boundListOps("namelist1").rightPush("张飞");
}
/**
* 显示右压栈的值
*/
@Test
public void testGetValue1(){
List list = redisTemplate.boundListOps("namelist1").range(0, 10);
System.out.println(list);
}
@Test
public void delete(){
redisTemplate.delete("namelist1");
}
/**
* 左压栈
*/
@Test
public void testSetValue2(){
redisTemplate.boundListOps("namelist2").leftPush("刘备");
redisTemplate.boundListOps("namelist2").leftPush("关羽");
redisTemplate.boundListOps("namelist2").leftPush("张飞");
}
/**
* 显示左压栈的值
*/
@Test
public void testGetValue2(){
List list = redisTemplate.boundListOps("namelist2").range(0, 10);
System.out.println(list);
}
/**
* 删除值
*/
@Test
public void removeValue(){
redisTemplate.boundListOps("namelist1").remove(0, "刘备");
}