一、简介
1、SpringData和Redis
Redis将数据存储到内存的,速度快。可以解决请求mysql数据库过多而导致mysql崩溃的问题。
SpringData是专门用来控制Redis的工具,使用SpringData来操作Redis。
注意:在使用了Redis后,修改数据需要将Redis中的数据删除,之后再查的时候在赋值。
2、SpringDataRedis小demo
1、导入依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.7.2.RELEASE</version>
</dependency>
</dependencies>
2、配置文件applicationContext-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"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath*:*.properties" />
<!-- redis 相关配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<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:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory" />
</bean>
</beans>
3、配置文件redis-config.properties
redis.host=192.168.200.128
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true
4、demo
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-redis.xml"})
public class TestString {
@Autowired
private RedisTemplate redisTemplate;
//放数据
@Test
public void testSet(){
redisTemplate.boundValueOps("testKey").set("0708java");
}
//取数据
@Test
public void get(){
String testKey = (String)redisTemplate.boundValueOps("testKey").get();
System.out.println(testKey);
}
@Test
public void del(){
redisTemplate.delete("testKey");
}
@Test
public void hashPut(){
redisTemplate.boundHashOps("testHash").put("001","左青龙");
redisTemplate.boundHashOps("testHash").put("002","右白虎");
redisTemplate.boundHashOps("testHash").put("003","扫地僧");
redisTemplate.boundHashOps("testHash").put("004","灭霸");
}
@Test
public void hashGetOne(){
String testHash = (String)redisTemplate.boundHashOps("testHash").get("001");
System.out.println(testHash);
}
@Test
public void hashGetAll(){
Map<String,String> testHash = redisTemplate.boundHashOps("testHash").entries();
Set<Map.Entry<String, String>> entries = testHash.entrySet();
for (Map.Entry<String, String> entry : entries) {
System.out.println("key"+entry.getKey()+" value"+entry.getValue());
}
}
@Test
public void hashDel(){
redisTemplate.boundHashOps("testHash").delete("001");
}
@Test
public void hashDelAll(){
redisTemplate.delete("testHash");
}
//list
@Test
public void listLeftPush(){
redisTemplate.boundListOps("001").leftPush("赵敏");
redisTemplate.boundListOps("001").leftPush("周芷若");
redisTemplate.boundListOps("001").leftPush("小昭");
}
@Test
public void listRightPush(){
redisTemplate.boundListOps("001").rightPush("张无忌");
}
@Test
public void listGet(){
List<String> range = redisTemplate.boundListOps("001").range(0, 10);
for (String s : range) {
System.out.println(s);
}
}
@Test
public void listDel(){
redisTemplate.delete("001");
}
}
二、分布式项目
在分布式项目中。Redis就是一个缓存数据库,速度快,不会造成mysql的数据访问量过大。
1、配置文件redis-config.properties
# Redis settings
# server IP
redis.host=192.168.200.128
# 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
2、在service层中
2.1、注入RedisTemplate
2.2、操作Redis,在查询的时候会分为两种情况,第一次先查数据库,之后将查到的数据写入到Redis中,之后就可以使用Redis了。
public List<Content> findByCategoryIdFromRedis(Long categoryId) {
//1、根据分类的id到redis中取数据
List<Content> list=(List<Content>)redisTemplate.boundHashOps(Constants.CONTENT_LIST_REDIS).get(categoryId);
//2.如果redis中没有数据,到数据库中取
if(list==null){
list=findByCategoryId(categoryId);
//3、数据库中获取到数据,将数据存入redis中一份
redisTemplate.boundHashOps(Constants.CONTENT_LIST_REDIS).put(categoryId,list);
}
return list;
}
来源:oschina
链接:https://my.oschina.net/u/4400299/blog/4131541