Spring Data Redis快速入门
1、准备工作
1.1 构建Maven工程 SpringDataRedisDemo 引入Spring相关依赖、JUnit依赖、Jedis和SpringDataRedis依赖
<!‐‐缓存‐‐>
<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>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring‐test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
1.2 在src/main/resources下创建properties文件夹,建立redis-config.properties
redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
maxIdle :最大空闲数 maxWaitMillis: 连接时的最大等待毫秒数
1.3 在src/main/resources下创建spring文件夹,创建applicationContext-redis.xml
<context:property‐placeholder location="classpath:redis‐config.properties" />
<!‐‐ redis 相关配置 ‐‐>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
</bean>
<bean id="JedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host‐name="${redis.host}" p:port="${redis.port}"
p:password="${redis.pass}" p:pool‐config‐ref="poolConfig"/>
<bean id="redisTemplate"class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory" />
</bean>
2、Redis的操作
2.1 值类型的操作
package com
.cui
.test
;
import org
.junit
.Test
;
import org
.junit
.runner
.RunWith
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.data
.redis
.core
.RedisTemplate
;
import org
.springframework
.test
.context
.ContextConfiguration
;
import org
.springframework
.test
.context
.junit4
.SpringJUnit4ClassRunner
;
@RunWith(SpringJUnit4ClassRunner
.class)
@ContextConfiguration(locations
= "classpath:applicationContext-redis.xml")
public class ValueTest {
@Autowired
private RedisTemplate redisTemplate
;
@Test
public void setValue(){
redisTemplate
.boundValueOps("name").set("王五");
}
@Test
public void getValue(){
String name
= (String
)redisTemplate
.boundValueOps("name").get();
System
.out
.println(name
);
}
@Test
public void testDelete(){
redisTemplate
.delete("name");
}
}
2.2 Set类型操作
package com
.cui
.test
;
import org
.junit
.Test
;
import org
.junit
.runner
.RunWith
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.data
.redis
.core
.RedisTemplate
;
import org
.springframework
.test
.context
.ContextConfiguration
;
import org
.springframework
.test
.context
.junit4
.SpringJUnit4ClassRunner
;
import java
.util
.Set
;
@RunWith(SpringJUnit4ClassRunner
.class)
@ContextConfiguration(locations
= "classpath:applicationContext-redis.xml")
public class SetTest {
@Autowired
private RedisTemplate redisTemplate
;
@Test
public void setValue(){
redisTemplate
.boundSetOps("nameSet").add("张三");
redisTemplate
.boundSetOps("nameSet").add("李四");
redisTemplate
.boundSetOps("nameSet").add("王五");
redisTemplate
.boundSetOps("nameSet").add("赵六");
}
@Test
public void getValue(){
Set nameSet
= redisTemplate
.boundSetOps("nameSet").members();
System
.out
.println(nameSet
);
}
@Test
public void testDelete(){
redisTemplate
.boundSetOps("nameSet").remove("张三");
}
@Test
public void testDeleteAllSet(){
redisTemplate
.delete("nameSet");
}
}
2.3 List类型操作
2.3.1 左压栈 新加入的数据排在前面
package com
.cui
.test
;
import org
.junit
.Test
;
import org
.junit
.runner
.RunWith
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.data
.redis
.core
.RedisTemplate
;
import org
.springframework
.test
.context
.ContextConfiguration
;
import org
.springframework
.test
.context
.junit4
.SpringJUnit4ClassRunner
;
import java
.util
.List
;
@RunWith(SpringJUnit4ClassRunner
.class)
@ContextConfiguration(locations
= "classpath:applicationContext-redis.xml")
public class ListTest {
@Autowired
private RedisTemplate redisTemplate
;
@Test
public void setValue1(){
redisTemplate
.boundListOps("nameList1").leftPush("张飞");
redisTemplate
.boundListOps("nameList1").leftPush("刘备");
redisTemplate
.boundListOps("nameList1").leftPush("关羽");
}
@Test
public void getValue(){
List nameList1
= redisTemplate
.boundListOps("nameList1").range(0, -1);
System
.out
.println(nameList1
);
}
2.3.2 右压栈 新加入的数据加到后面
@Test
public void setValue2(){
redisTemplate
.boundListOps("nameLit2").rightPush("德玛西亚之力");
redisTemplate
.boundListOps("nameLit2").rightPush("无极剑圣");
redisTemplate
.boundListOps("nameLit2").rightPush("诺克萨斯之手");
}
@Test
public void getValue2(){
List nameList2
= redisTemplate
.boundListOps("nameLit2").range(0, -1);
System
.out
.println(nameList2
);
}
2.3.3 根据索引查询某个元素
@Test
public void testSearchByIndex(){
String s
=(String
) redisTemplate
.boundListOps("nameList1").index(1);
System
.out
.println(s
);
}
2.3.4 移除指定个数的值
@Test
public void setAdd2(){
redisTemplate
.boundListOps("nameLit2").rightPush("无极剑圣");
redisTemplate
.boundListOps("nameLit2").rightPush("无极剑圣");
redisTemplate
.boundListOps("nameLit2").rightPush("无极剑圣");
redisTemplate
.boundListOps("nameLit2").rightPush("无极剑圣");
}
@Test
public void testRemoveByIndex(){
redisTemplate
.boundListOps("nameLit2").remove(2, "无极剑圣");
}
2.4 Hash类型操作
package com
.cui
.test
;
import org
.junit
.Test
;
import org
.junit
.runner
.RunWith
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.data
.redis
.core
.RedisTemplate
;
import org
.springframework
.test
.context
.ContextConfiguration
;
import org
.springframework
.test
.context
.junit4
.SpringJUnit4ClassRunner
;
import java
.util
.List
;
import java
.util
.Set
;
@RunWith(SpringJUnit4ClassRunner
.class)
@ContextConfiguration(locations
= "classpath:applicationContext-redis.xml")
public class HashTest {
@Autowired
private RedisTemplate redisTemplate
;
@Test
public void setValue(){
redisTemplate
.boundHashOps("nameHash").put("a", "张无忌");
redisTemplate
.boundHashOps("nameHash").put("b", "周芷若");
redisTemplate
.boundHashOps("nameHash").put("c", "赵敏");
}
@Test
public void getKeys(){
Set keys
= redisTemplate
.boundHashOps("nameHash").keys();
System
.out
.println(keys
);
}
@Test
public void getValues(){
List values
= redisTemplate
.boundHashOps("nameHash").values();
System
.out
.println(values
);
}
@Test
public void testGetValueByKey(){
Object o
= redisTemplate
.boundHashOps("nameHash").get("a");
System
.out
.println(o
);
}
@Test
public void testRemoveVaalueByKey(){
redisTemplate
.boundHashOps("nameHash").delete("a");
}
@Test
public void testDelete(){
redisTemplate
.delete("nameHash");
}
}
2.5 Zset类型操作
zset是set的升级版本,它在set的基础上增加了一格顺序属性,这一属性在添加元素的同时可以指定,每次指定后,zset会自动重新按照新的值调整顺序。可以理解为有两列的mysql表,一列存储value,一列存储分值。
package com
.cui
.test
;
import org
.junit
.Test
;
import org
.junit
.runner
.RunWith
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.data
.redis
.core
.DefaultTypedTuple
;
import org
.springframework
.data
.redis
.core
.RedisTemplate
;
import org
.springframework
.test
.context
.ContextConfiguration
;
import org
.springframework
.test
.context
.junit4
.SpringJUnit4ClassRunner
;
import java
.util
.Set
;
@RunWith(SpringJUnit4ClassRunner
.class)
@ContextConfiguration(locations
= "classpath:applicationContext-redis.xml")
public class ZsetTest {
@Autowired
private RedisTemplate redisTemplate
;
@Test
public void setValue(){
redisTemplate
.boundZSetOps("nameZset").add("小明", 35);
redisTemplate
.boundZSetOps("nameZset").add("小红", 99);
redisTemplate
.boundZSetOps("nameZset").add("小刚", 61);
}
@Test
public void getValue1(){
Set nameZset
= redisTemplate
.boundZSetOps("nameZset").range(0, -1);
System
.out
.println(nameZset
);
}
@Test
public void getValue2(){
Set nameZset
= redisTemplate
.boundZSetOps("nameZset").reverseRange(0,-1 );
System
.out
.println(nameZset
);
}
@Test
public void testAddGrade(){
redisTemplate
.boundZSetOps("nameZset").incrementScore("小明", 30);
}
@Test
public void getValueAndScore(){
Set
<ZSetOperations.TypedTuple> namezset
=
redisTemplate
.boundZSetOps("namezset").reverseRangeWithScores(0, ‐
1);
for(ZSetOperations
.TypedTuple typedTuple
:namezset
){
System
.out
.print("姓名:"+typedTuple
.getValue());
System
.out
.println("金币:"+typedTuple
.getScore());
}
}
@Test
public void testDelete(){
redisTemplate
.delete("nameZset");
}
}
TypedTuple是值与分数的封装
2.6 过期时间的设置
以值类型为例:存值时指定过期时间和时间单位
@Test
public void setValue(){
redisTemplate
.boundValueOps("name").set("王五");
redisTemplate
.boundValueOps("name").expire(5, TimeUnit
.MINUTES
);
}