1.什么是spring data redis?
Spring-data-redis是spring大家族的一部分,提供了在srping应用中通过简单的配置访问redis服务,对reids底层开发包(Jedis, JRedis, and RJC)进行了高度封装,RedisTemplate提供了redis各种操作、异常处理及序列化,支持发布订阅,并对spring 3.1 cache进行了实现。
spring-data-redis针对jedis提供了如下功能:
1)连接池自动管理,提供了一个高度封装的“RedisTemplate”类
2)针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
boundValueOps:string简单K-V操作
boundSetOps:set类型数据操作
boundZSetOps:zset类型数据操作
boundHashOps:针对map类型的数据操作
boundListOps:针对list类型的数据操作
2.Spring Data Redis入门小Demo
1)构建Maven工程SpringDataRedisDemo
2)引入Spring相关依赖、引入JUnit依赖、Jedis和SpringDataRedis依赖
<dependencies> <!-- 缓存 --> <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> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies>
3)在src/main/resources下创建properties文件夹,新建redis-config.properties文件
redis.host=127.0.0.1 redis.port=6379 redis.pass=1234 redis.database=0 redis.maxIdle=300 redis.maxWait=3000 redis.testOnBorrow=true
maxIdle :最大空闲数
maxWaitMillis:连接时的最大等待毫秒数
testOnBorrow:在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的;
4)在src/main/resources下创建spring文件夹 ,创建applicationContext-redis.xml
<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/*.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>
5)在测试目录中新建一个包,包中新建一个类RedisTest,添加测试运行环境
package com.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:spring/applicationContext-redis.xml") public class RedisTest { @Autowired private RedisTemplate redisTemplate; //这里对redis进行操作 }
6)string类型的操作
@Test public void test1(){ //string类型存值 redisTemplate.boundValueOps("name").set("456615"); } @Test public void test2(){ //string类型取值 String name = (String) redisTemplate.boundValueOps("name").get(); System.out.println(name); } @Test public void test3(){ //string类型删除 redisTemplate.delete("name"); }
7)set类型操作
@Test public void test4(){ //set类型存值 redisTemplate.boundSetOps("nameset").add("张三"); redisTemplate.boundSetOps("nameset").add("张三2"); redisTemplate.boundSetOps("nameset").add("张三3"); redisTemplate.boundSetOps("nameset").add("张三4"); } @Test public void test5(){ //set类型取值 Set nameset = redisTemplate.boundSetOps("nameset").members(); System.out.println(nameset); } @Test public void test6(){ //set类型删除某一个值 redisTemplate.boundSetOps("nameset").remove("张三2"); } @Test public void test7(){ //set类型全部删除 redisTemplate.delete("nameset"); }
9)list类型的操作
@Test public void test8(){ //list存值 redisTemplate.boundListOps("name1").leftPush("张三"); redisTemplate.boundListOps("name1").leftPush("张三3"); redisTemplate.boundListOps("name1").leftPush("张三2"); redisTemplate.boundListOps("name1").leftPush("张三4"); } @Test public void test9(){ //list取值 List name1 = redisTemplate.boundListOps("name1").range(0, 10); System.out.println(name1); } @Test public void test10(){ //list取某一个值 Object name1 = redisTemplate.boundListOps("name1").index(1); System.out.println(name1); } @Test public void test11(){ //list移除 redisTemplate.boundListOps("name1").remove(1,"张三2"); }
10)Hash类型操作
@Test public void test12(){ //Hash类型存值 redisTemplate.boundHashOps("namehash").put("a", "唐僧"); redisTemplate.boundHashOps("namehash").put("b", "悟空"); redisTemplate.boundHashOps("namehash").put("c", "八戒"); redisTemplate.boundHashOps("namehash").put("d", "沙僧"); } @Test public void test13(){ //Hash类型取出所有的键 Set namehash = redisTemplate.boundHashOps("namehash").keys(); System.out.println(namehash); } @Test public void test14(){ //Hash类型取出所有的值 List namehash = redisTemplate.boundHashOps("namehash").values(); System.out.println(namehash); } @Test public void test15(){ //Hash类型根据key取值 Object object = redisTemplate.boundHashOps("namehash").get("b"); System.out.println(object); } @Test public void test16(){ //Hash类型根据key删除 redisTemplate.boundHashOps("namehash").delete("c"); }
3.项目的整合
用的比较多的就是把广告存到缓存里面去,首页有大量的广告图片需要显示,每次操作数据库很浪费资源。
思路就是把图片的分类id存到缓存中,页面加载的时候就把这些图片查询出来。使用map的方式,key是缓存类型。
1)查询操作:当进行查询的时候,先去redis中查找有没有这个id,如果有就从这里取,如果没有就从数据库中取,取了之后返回给页面,与此同时要放到redis中。
2)后台增加图片:把和其分组的id相关的缓存清空即可,因为查询的时候会再次添加到缓存。
3)后台更新图片:如果没有修改分组id,就把其分组的id相关的缓存清空即可;如果修改了分组id,就需要把它之前所在的分组的缓存清空,还要把修改后所在的分组的缓存清空,这样数据才能同步。