由于我们的网站每天的访问量非常大,如果每个人访问的时候都通过数据库查询数据然后显示到页面上,这会导致服务器的压力非常大。为了解决这个问题,我们可以使用数据缓存的方式。
一、SpringDataRedis的简介
Spring-data-redis 是 spring 大家族的一部分,提供了在 spring 应用中通过简单的配置访问 redis 服务,对 reids 底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate 提供了 redis 各种操作、异常处理及序列化,支持发布订阅,并对 spring 3.1 cache 进行了实现。
spring-data-redis 针对 jedis 提供了如下功能:
1.连接池自动管理,提供了一个高度封装的“RedisTemplate”类
2.针对 jedis 客户端中大量 api 进行了归类封装,将同一类型操作封装为 operation 接口
- ValueOperations:简单K-V 操作
- SetOperations:set 类型数据操作
- ZSetOperations:zset 类型数据操作
- HashOperations:针对 map 类型的数据操作
- ListOperations:针对 list 类型的数据操作
二、入门demo
1、准备工作
创建Maven工程,引入Spring相关依赖、JUnit依赖以及Jedis和SpringDataRedis依赖,并建立配置文件
需要引入的依赖
org.springframework
spring-context
4.2.4.RELEASE
org.springframework
spring-core
4.2.4.RELEASE
org.springframework
spring-beans
4.2.4.RELEASE
org.springframework
spring-expression
4.2.4.RELEASE
org.springframework
spring-web
4.2.4.RELEASE
javax.servlet
javax.servlet-api
3.1.0
provided
org.springframework
spring-aop
4.2.4.RELEASE
org.springframework
spring-test
4.2.4.RELEASE
test
junit
junit
4.9
redis.clients
jedis
2.8.1
org.springframework.data
spring-data-redis
1.7.2.RELEASE
需要增加的配置文件
redis-config.properties的内容
# Redis settings
# server IP
redis.host=127.0.0.1
# server port
redis.port=6379
# server pass
redis.pass=
# use dbIndex
redis.database=0
# \u63A7\u5236\u4E00\u4E2Apool\u6700\u591A\u6709\u591A\u5C11\u4E2A\u72B6\u6001\u4E3Aidle(\u7A7A\u95F2\u7684)\u7684jedis\u5B9E\u4F8B
redis.maxIdle=300
# \u8868\u793A\u5F53borrow(\u5F15\u5165)\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u6700\u5927\u7684\u7B49\u5F85\u65F6\u95F4\uFF0C\u5982\u679C\u8D85\u8FC7\u7B49\u5F85\u65F6\u95F4(\u6BEB\u79D2)\uFF0C\u5219\u76F4\u63A5\u629B\u51FAJedisConnectionException\uFF1B
redis.maxWait=3000
# \u5728borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Cvalidate\u64CD\u4F5C\uFF1B\u5982\u679C\u4E3Atrue\uFF0C\u5219\u5F97\u5230\u7684jedis\u5B9E\u4F8B\u5747\u662F\u53EF\u7528\u7684
redis.testOnBorrow=true
配置文件中的属性含义:
host:redis服务的IP地址
port:redis服务的端口号,默认为6379
pass:redis服务的密码
maxIdle:最大空闲数
maxWaitMillis:连接时的最大等待毫秒数
testOnBorrow:在提取一个 jedis 实例时,是否提前进行验证操作;如果为 true,则得到的 jedis 实例均是可用的
applicationContext-redis.xml的内容
2、针对值进行缓存操作
package test;
import static org.junit.Assert.*;
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:spring/applicationContext-redis.xml")
public class TestValue {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存储值
*/
@Test
public void setValue(){
redisTemplate.boundValueOps("name").set("Rose");
}
/**
* 获取值
*/
@Test
public void getValue() {
String str = (String) redisTemplate.boundValueOps("name").get();
System.out.println(str);
}
/**
* 删除值
*/
@Test
public void deleteValue() {
redisTemplate.delete("name");
}
}
3、针对set集合的操作
package test;
import java.util.Set;
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:spring/applicationContext-redis.xml")
public class TestSet {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存储set集合类型的数据
*/
@Test
public void setValue(){
redisTemplate.boundSetOps("nameset").add("曹操");
redisTemplate.boundSetOps("nameset").add("刘备");
redisTemplate.boundSetOps("nameset").add("孙权");
}
/**
* 读取set集合类型的数据
*/
@Test
public void getValue(){
//set集合存储数据,没有顺序
Set set = redisTemplate.boundSetOps("nameset").members();
System.out.println(set);
}
/**
* 删除set集合中的一条记录
*/
@Test
public void removeValue(){
redisTemplate.boundSetOps("nameset").remove("孙权");
}
/**
* 删除整个set集合
*/
@Test
public void deleteValue(){
redisTemplate.delete("nameset");
}
}
4、针对List集合的操作
package test;
import java.util.List;
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:spring/applicationContext-redis.xml")
public class TestList {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存储值:左压栈
* 从左往右添加元素,后添加的元素排在前面
* 结果:[张飞, 关羽, 刘备]
*/
@Test
public void testSetValue1(){
redisTemplate.boundListOps("namelist1").leftPush("刘备");
redisTemplate.boundListOps("namelist1").leftPush("关羽");
redisTemplate.boundListOps("namelist1").leftPush("张飞");
}
/**
* 存储值:右压栈
* 从右向左添加元素,后添加的元素排在后面
* 结果:[刘备, 关羽, 张飞]
*/
@Test
public void testSetValue2(){
redisTemplate.boundListOps("namelist2").rightPush("刘备");
redisTemplate.boundListOps("namelist2").rightPush("关羽");
redisTemplate.boundListOps("namelist2").rightPush("张飞");
}
/**
* 查询list集合中所有的值
*/
@Test
public void getValue(){
//根据索引的范围查询值,如果需要查询所有的值,需要将结束索引设置非常大
List list1 = redisTemplate.boundListOps("namelist1").range(0, 10);
List list2 = redisTemplate.boundListOps("namelist2").range(0, 10);
System.out.println(list1);
System.out.println(list2);
}
/**
* 根据索引查询元素
*/
@Test
public void searchValueByIndex(){
String str = (String) redisTemplate.boundListOps("namelist1").index(1);
System.out.println(str);
}
/**
* 移除集合中的元素
* remove方法中的参数:
* 参数1:移除元素的个数(集合中可能有许多相同的元素)
* 参数2:要移除的元素
*/
@Test
public void removeValue(){
redisTemplate.boundListOps("namelist1").remove(1, "张飞");
}
/**
* 删除整个集合
*/
@Test
public void deleteValue(){
redisTemplate.delete("namelist1");
redisTemplate.delete("namelist2");
}
}
5、针对hash类型的操作
package test;
import java.util.List;
import java.util.Set;
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:spring/applicationContext-redis.xml")
public class TestHash {
@Autowired
private RedisTemplate redisTemplate;
/**
* 存储值(其实就是存储的map集合类型的值)
*/
@Test
public void setValue(){
redisTemplate.boundHashOps("namehash").put("大哥", "刘备");
redisTemplate.boundHashOps("namehash").put("二哥", "关羽");
redisTemplate.boundHashOps("namehash").put("三弟", "张飞");
}
/**
* 获取所有的key
*/
@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 searchValueByKey(){
String str = (String) redisTemplate.boundHashOps("namehash").get("大哥");
System.out.println(str);
}
/**
* 根据键删除对应的值
*/
@Test
public void removeValue(){
redisTemplate.boundHashOps("namehash").delete("三弟");
}
/**
* 删除整个集合
*/
@Test
public void deleteValue(){
redisTemplate.delete("namehash");
}
}