配置文件
Maven配置
<!-- 缓存 Redis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.0</version>
</dependency>
<!--spring-data-redis-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.8.RELEASE</version>
</dependency>
redis-config.properties
jedisConnectionFactory.hostName=192.168.3.33
jedisConnectionFactory.port=6379
jedisConnectionFactory.password=
jedisConnectionFactory.database=0
poolConfig.maxIdle=300
spring-redis.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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
">
<!--加载解析配置文件,可以直接给对应的Bean注入值,注入方式为属性注入,调用set方法注入-->
<context:property-override location="classpath:config/redis-config.properties" />
<!--Jedis连接池配置-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"></bean>
<!--Jedis连接工厂对象-->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:pool-config-ref="poolConfig"/>
<!--Jedis对缓存操作的模板对象-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<!--配置数据源-->
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
</beans>
不同数据类型操作
ValueOperations:简单K-V操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-redis.xml")
public class ValueTest {
@Autowired
private RedisTemplate redisTemplate;
/***
* 增加数据测试
*/
@Test
public void testAdd(){
//boundValueOps用于操作简单的key:value类型
redisTemplate.boundValueOps("username").set("小红红");
}
/***
* 查询操作
*/
@Test
public void testGet(){
Object username = redisTemplate.boundValueOps("username").get();
System.out.println(username);
}
/***
* 删除测试
*/
@Test
public void testDelete(){
//redisTemplate.boundValueOps("username")
redisTemplate.delete("username");
}
}
SetOperations:set类型数据操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-redis.xml")
public class SetTest {
@Autowired
private RedisTemplate redisTemplate;
/***
* 增加数据测试
*/
@Test
public void testAdd(){
redisTemplate.boundSetOps("username").add("小红");
redisTemplate.boundSetOps("username").add("小黑");
redisTemplate.boundSetOps("username").add("小红");//重复过滤
redisTemplate.boundSetOps("username").add("小红");//重复过滤
}
/***
* 查询操作
*/
@Test
public void testGet(){
Set members = redisTemplate.boundSetOps("username").members();
System.out.println(members);
}
/***
* 删除测试
*/
@Test
public void testDelete(){
redisTemplate.boundSetOps("username").remove("小黑");
}
}
ListOperations:list操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-redis.xml")
public class ListTest {
@Autowired
private RedisTemplate redisTemplate;
/**
* 左压栈
* 从队列的左边开始增加数据
*/
@Test
public void testLeftAdd(){
//只添加1条数据
redisTemplate.boundListOps("Xiaohong").leftPush("花花");
//批量添加
redisTemplate.boundListOps("Xiaohong").leftPushAll("小红红","小花花","哈哈哈");
}
/***
* 左压出栈
*/
@Test
public void testLeftGet(){
Object result = redisTemplate.boundListOps("Xiaohong").leftPop();
System.out.println(result);
}
/**
* 右压栈
*/
@Test
public void testRightAdd(){
//只添加1条数据
redisTemplate.boundListOps("Xiaohong").rightPush("花花");
//批量添加
redisTemplate.boundListOps("Xiaohong").rightPushAll("小红红","小花花","哈哈哈");
}
/***
* 左压出栈
*/
@Test
public void testRightGet(){
Object result = redisTemplate.boundListOps("Xiaohong").rightPop();
System.out.println(result);
}
}
hashoperations:hash操作
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-redis.xml")
public class HashTest {
@Autowired
private RedisTemplate redisTemplate;
/***
* 右2层key
* namespace
* key
*/
@Test
public void testAdd(){
redisTemplate.boundHashOps("NameSpace").put("1","小花花");
redisTemplate.boundHashOps("NameSpace").put("2","大花花");
redisTemplate.boundHashOps("NameSpace").put("3","中花花");
redisTemplate.boundHashOps("XiaohongHAHA").put("1","中花花");
redisTemplate.boundHashOps("XiaohongHAHA").put("2","中花花");
}
/***
* 查询操作
*/
@Test
public void testGet(){
Object result1 = redisTemplate.boundHashOps("NameSpace").get("1");
System.out.println(result1);
Object result2 = redisTemplate.boundHashOps("XiaohongHAHA").get("1");
System.out.println(result2);
}
/***
* 删除操作
*/
@Test
public void testDelete(){
redisTemplate.boundHashOps("NameSpace").delete("2");
}
/***
* 查询所有
*/
@Test
public void testGetAll(){
List nameSpace = redisTemplate.boundHashOps("NameSpace").values();
for (Object o : nameSpace) {
System.out.println(o);
}
}
}