一、简介
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 # u63A7u5236u4E00u4E2Apoolu6700u591Au6709u591Au5C11u4E2Au72B6u6001u4E3Aidle(u7A7Au95F2u7684)u7684jedisu5B9Eu4F8B redis.maxIdle=300 # u8868u793Au5F53borrow(u5F15u5165)u4E00u4E2Ajedisu5B9Eu4F8Bu65F6uFF0Cu6700u5927u7684u7B49u5F85u65F6u95F4uFF0Cu5982u679Cu8D85u8FC7u7B49u5F85u65F6u95F4(u6BEBu79D2)uFF0Cu5219u76F4u63A5u629Bu51FAJedisConnectionExceptionuFF1B redis.maxWait=3000 # u5728borrowu4E00u4E2Ajedisu5B9Eu4F8Bu65F6uFF0Cu662Fu5426u63D0u524Du8FDBu884Cvalidateu64CDu4F5CuFF1Bu5982u679Cu4E3AtrueuFF0Cu5219u5F97u5230u7684jedisu5B9Eu4F8Bu5747u662Fu53EFu7528u7684 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; }